我正在尝试解码CMYK颜色格式的jpeg.BitmapFactory返回null.我google了一下,但没有运气.是否有可能Android团队没有正确的工作支持各种颜色格式?
我使用的是Android 2.2.
非常感谢.
我在那里看过很多帖子?但我找不到正确答案.
我尝试做一些事情:
@Override
public void onPictureTaken(byte[] paramArrayOfByte, Camera paramCamera) {
try {
Bitmap bitmap = BitmapFactory.decodeByteArray(paramArrayOfByte, 0,
paramArrayOfByte.length);
int width = bitmap.getWidth();
int height = bitmap.getHeight();
FileOutputStream os = new ileOutputStream(Singleton.mPushFilePath);
Matrix matrix = new Matrix();
matrix.postRotate(90);
Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,
height, matrix, false);
resizedBitmap.compress(Bitmap.CompressFormat.JPEG, 95, os);
os.close();
...
Run Code Online (Sandbox Code Playgroud)
有没有办法旋转图片,而不使用BitmapFactory?我想要旋转图片而不会损失质量!
编辑:当我在txt文件中保存这些字节时,当我将其保存为png文件时,它会显示图像,但它在这里不起作用为什么......?
我正在使用此代码从doInBackground()上的字节数组创建图像
String base64data=StringEscapeUtils.unescapeJava(IOUtils.toString(resp.getEntity().getContent()));
base64data=base64data.substring(1,base64data.length()-1);
JSONObject obj=new JSONObject(base64data);
JSONArray array=obj.getJSONArray("EMRTable");
JSONObject childobj=array.getJSONObject(0);
results=childobj.getString("DocumentInternalFormat");
Run Code Online (Sandbox Code Playgroud)
和onPostExecute
if(jsondata!=null) {
receiveData(jsondata);
}
Run Code Online (Sandbox Code Playgroud)
logcat中没有错误,即使它没有异常..但图像没有显示.我也是这样做的
String data=(String)object;
data=data.trim();
byte[] base64converted=Base64.decode(data,Base64.DEFAULT);
ImageView image=new ImageView(context);
image.setImageBitmap(bmp);
setContentView(image);
Run Code Online (Sandbox Code Playgroud)
但结果相同的图像没有显示,但没有异常或错误,问题是什么...
注释行是当我尝试将这些字节存储到文本文件中时,当我拉动文件时,它会显示带有Windows默认图像查看器的图像.
关于将Canvas的内容放入Bitmap,我遇到了一些困难.当我尝试这样做时,文件的文件大小约为5.80KB但看起来完全是空的(每个像素都是'#000').
画布绘制了一系列由手写形成的相互连接的线条.下面是我对视图的onDraw.(我知道它阻止了UI线程/不良做法等等,但是我只需要让它工作)
谢谢.
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
if (IsTouchDown) {
// Calculate the points
Path currentPath = new Path();
boolean IsFirst = true;
for(Point point : currentPoints){
if(IsFirst){
IsFirst = false;
currentPath.moveTo(point.x, point.y);
} else {
currentPath.lineTo(point.x, point.y);
}
}
// Draw the path of points
canvas.drawPath(currentPath, pen);
// Attempt to make the bitmap and write it to a file.
Bitmap toDisk = null;
try {
// TODO: Get the size of …Run Code Online (Sandbox Code Playgroud) 我正在处理GeoTiff/PNG太大而无法在代码中整体处理的文件。
有没有可能在bitmapfactory中解码文件的特定区域(例如,由两个x,y坐标指定)?尚未找到任何类似的东西http://developer.android.com/reference/android/graphics/BitmapFactory.html(Android开发者参考)。
谢谢!
有了kcoppock的提示,我已经设置了以下解决方案。
虽然我想知道为什么rect需要初始化Rect(left, bottom, right, top)而不是Rect(left, top, right, bottom)...
示例调用:
Bitmap myBitmap = loadBitmapRegion(context, R.drawable.heightmap,
0.08f, 0.32f, 0.13f, 0.27f);
Run Code Online (Sandbox Code Playgroud)
功能:
public static Bitmap loadBitmapRegion(
Context context, int resourceID,
float regionLeft, float regionTop,
float regionRight, float regionBottom) {
// Get input stream for resource
InputStream is = context.getResources().openRawResource(resourceID);
// Set options
BitmapFactory.Options opt = new BitmapFactory.Options();
//opt.inPreferredConfig = Bitmap.Config.ARGB_8888; //standard
// Create decoder
BitmapRegionDecoder decoder = null;
try { …Run Code Online (Sandbox Code Playgroud) 我从相机获取预览数据.它采用NV21格式.我想将预览保存到SD卡,即位图.我的代码是获取图像并保存它,但在图库中它没有捕获预览.它只是黑色矩形,这是代码.
public void processImage() {
Bitmap bitmap = null;
if (flag == true) {
flag = false;
if (mCamera != null) {
Camera.Parameters parameters = mCamera.getParameters();
int imageFormat = parameters.getPreviewFormat();
if (imageFormat == ImageFormat.NV21) {
Toast.makeText(mContext, "Format: NV21", Toast.LENGTH_SHORT)
.show();
int w = parameters.getPreviewSize().width;
int h = parameters.getPreviewSize().height;
YuvImage yuvImage = new YuvImage(mData, imageFormat, w, h,
null);
Rect rect = new Rect(0, 0, w, h);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yuvImage.compressToJpeg(rect, 100, baos);
byte[] jData = baos.toByteArray();
bitmap = BitmapFactory.decodeByteArray(jData, …Run Code Online (Sandbox Code Playgroud) 在这里,我想从字符串URL转换图像。尽管有一个包含图像的URL,但它返回null。我在下面共享了代码。
private byte[] convertImageToByteArray(String imgPath)
{
byte[] byteArray = null;
Bitmap bmp = BitmapFactory.decodeFile(imgPath);
if(bmp != null)
{
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//bmp.compress(Bitmap.CompressFormat.JPEG, 100, stream);
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
try
{
stream.close();
}
catch (IOException e)
{
e.printStackTrace();
}
} catch (Exception e) {
e.printStackTrace();
}
}
else
{
try {
Bitmap bmpDefault = BitmapFactory.decodeResource(getResources(), R.drawable.na);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
//bmpDefault.compress(Bitmap.CompressFormat.JPEG, 100, stream);
bmpDefault.compress(Bitmap.CompressFormat.PNG, 100, stream);
byteArray = stream.toByteArray();
}
catch (Exception e)
{ …Run Code Online (Sandbox Code Playgroud) 我正在尝试下载图像,然后将其显示到我的imageView组件.
要下载,我必须使用Asyntask并显示进度条以通知用户.问题是在经过循环以获得计算的进度值后,我从inputStream得到0.
Log.d("is", "" + inputStream.available()); // ---> will have a value
byte[] buffer = new byte[contentLenght];
while ((read = inputStream.read(buffer)) != -1) {
counter += read;
publishProgress(counter);
outputStream.write(buffer,0,read);
}
Log.d("is", "" + inputStream.available()); // -----> will return 0
bmp = BitmapFactory.decodeStream(inputStream); // bmp will be empty
Run Code Online (Sandbox Code Playgroud)
有没有办法获得进度条的计算值,而不是在输入流的末尾得到0值?
我在这里使用Asyntask.
BMP将有一个价值,我这样做的时候
imageView.setImageBitmap(bmp);,将工作只当我删除的循环,只是通话 bmp = BitmapFactory.decodeStream(inputStream);
但是如果我在这之前放一个循环
bmp = BitmapFactory.decodeStream(inputStream);
Run Code Online (Sandbox Code Playgroud)
imageView将不显示任何内容
这是我的完整Asynctask代码包括网络连接
int progressCounter;
int contentLenght;
int counter;
@Override
protected void onPreExecute() {
super.onPreExecute();
progressBar.setVisibility(View.VISIBLE);
}
@Override …Run Code Online (Sandbox Code Playgroud) 我有一个关于BitMapFactory.decodeFile的问题.
在我的应用程序中,我希望用户能够从他/她的设备中选择图像或拍照.然后必须在ImageView中显示
这是代码片段:
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage, filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
MyImage image = new MyImage();
image.setTitle("Test");
image.setDescription("test choose a photo from gallery and add it to " + "list view");
image.setDatetime(System.currentTimeMillis());
image.setPath(picturePath);
Run Code Online (Sandbox Code Playgroud)
我得到了这个例外:
BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: /storage/emulated/0/WhatsApp/Media/WhatsApp Images/IMG-20170302-WA0012.jpg: open failed: EACCES (Permission denied)
Run Code Online (Sandbox Code Playgroud)
怎么解决它.请帮帮我.谢谢你...
由于在添加图像时我有很多这些(内存不足),我想知道Bitmap对象支持的最大大小是什么,以及如何有效地管理它们.