我正在成功裁剪从Camera预览生成的Bitmap,我将生成的小位图保存到sdcard。在创建大位图和裁剪位图时,我将它们配置为Bitmap.Config.ARGB_8888。但是当我从sdcard加载该图像时,Bitmap的配置更改为RGB_565。我需要的是ARGB_8888。这种情况发生在pre-ICS设备上(我不知道该怎么说HoneyComb,因为我没有):
在我创建裁剪位图后,有什么奇怪的,我正在查询其配置,我在日志中看到的是ARGB_8888。
在我测试过的所有ICS和JellyBean设备上,我没有看到这一点,因为加载的Bitmap始终配置为ARGB_8888。
下面是我在给定摄像机字节数组的情况下裁剪和配置图像的方法:
@SuppressLint("SimpleDateFormat")
@Override
protected String doInBackground(byte[]... params) {
/***
* Will return the file name where we created the image
* */
SimpleDateFormat sdf = new SimpleDateFormat("yyyy_MM_dd_hh_mm_ss");
String fileName = sdf.format(new Date()) + ".jpeg";
File cameraRoot = Utils.getFileCacheDir(context, WIMSConstants.FileCacheDirs.CAMERA_CAPTURED);
if (!cameraRoot.exists() || !cameraRoot.isDirectory()) {
if (!cameraRoot.mkdir()) {
Log.e("WIMSOcrCamera", "Unable to create folder name " + cameraRoot.getAbsolutePath());
return null;
}
}
/**
* The target file name where to write
* **/
File targetFile = new File(cameraRoot, fileName);
FileOutputStream fos = null;
try {
byte[] cameraByteArray = params[0];
Options opts = new Options();
opts.inPreferredConfig = Bitmap.Config.ARGB_8888;
Bitmap bitmap = BitmapFactory.decodeByteArray(cameraByteArray, 0, cameraByteArray.length, opts);
/**
* Below log prints 'ARGB_8888' no matter the OS I am running
* */
Log.d(TAG_LOG, "Big camera image config: " + bitmap.getConfig());
fos = new FileOutputStream(targetFile);
/**
* screenDims is a two array giving the full size image (full screen size)
* **/
int fullSizeWidth = screenDims[0];
int fullSizeHeight = screenDims[1];
/**
* viewPortrect is a Rect that defines the area from the screen that I would like to crop.
*
* rectLeft, rectTop, rectWidth and rectHeight are the desired dimensions of the cropped image relative to the above created bitmap (bitmap).
* */
int rectLeft = bitmap.getWidth() * viewPortRect.left / fullSizeWidth;
int rectTop = bitmap.getHeight() * viewPortRect.top / fullSizeHeight;
int rectWidth = bitmap.getWidth() * viewPortRect.width() / fullSizeWidth;
int rectHeight = bitmap.getHeight() * viewPortRect.height() / fullSizeHeight;
int[] array = new int[rectWidth * rectHeight];
bitmap.getPixels(array, 0, rectWidth, rectLeft, rectTop, rectWidth, rectHeight);
bitmap.recycle();
/**
* Hurray! we have the cropped image int[] pixel data
* **/
bitmap = Bitmap.createBitmap(array, rectWidth, rectHeight, Config.ARGB_8888);
/**
* Bitmap.Config.ARGB_8888.equals(bitmap.getConfig()) ALWAYS evaluates to true no matter the device OS I am running.
* **/
Log.d(TAG_LOG, "generated smaller file ... config: " + bitmap.getConfig() + "; is onfig.ARGB_8888 equals: " + (Bitmap.Config.ARGB_8888.equals(bitmap.getConfig())));
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
bitmap.recycle();
Log.d(TAG_LOG, "DONE creating the cropped image ...");
return targetFile.getAbsolutePath();
} catch (IOException ex) {
Log.e(TAG_LOG, null, ex);
} catch (Throwable th) {
Log.e(TAG_LOG, null, th);
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException ignored) {
}
}
}
return null;
}
从SDCard加载图像,给定其fileURI(来自服务):
Bitmap bmp = MediaStore.Images.Media.getBitmap(getContentResolver(), fileUri);
/**
* "RGB_565" for pre-ICS always
* **/
Log.d(LOG_TAG, "bitmap config: " + bmp.getConfig());
这是一个已知问题吗?除了通过意图将裁剪后的位图传递给最终接收器之外,还有其他解决办法吗?
谢谢!
答案 0 :(得分:1)
将CompressFormat.JPEG
更改为CompressFormat.PNG
。