0

我成功裁剪了从相机预览生成的位图,并将生成的小位图保存到 sdcard。在创建大位图和裁剪位图时,我将它们配置为Bitmap.Config.ARGB_8888。但是当我从 sdcard 加载该图像时,位图的配置更改为 RGB_565。我需要它是 ARGB_8888。这发生在 ICS 之前的设备上(我不知道该怎么说 HoneyComb,因为我没有):

  1. 摩托罗拉 Defy (2.3.4)
  2. 索尼爱立信 S (2.3.4)
  3. 索尼爱立信现场随身听 (2.3.4)
  4. 三星 Galaxy Mini (2.2.1)
  5. 三星角宿 (2.2)

奇怪的是,在我创建裁剪的位图之后,我正在查询它的配置,我在日志中看到的是 ARGB_8888。

在我测试过的所有 ICS 和 JellyBean 设备上,我没有看到这一点,因为加载的位图始终配置为 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());

这是一个已知的问题?除了通过意图将裁剪的位图传递给最终接收器之外,还有其他解决方法吗?

谢谢!

4

1 回答 1

2

将 更改CompressFormat.JPEGCompressFormat.PNG

于 2013-06-20T12:57:45.030 回答