旋转大图像会导致OutOfMemoryError

时间:2012-09-26 07:21:41

标签: android image-processing

我正在尝试旋转从相机拍摄的图像并保存而不缩小而不显示在视图中,但是当我尝试像这样加载时

Bitmap bmp = BitmapFactory.decodeFile(tmpImageFilePath);

我得到OutOfMemoryError。我旋转图像没有问题,但如何加载它并保存原始尺寸?我已经用Google搜索了一些技术来解决这个问题,但它们都不适合我。

当我尝试这个变种时:

 File f = new File(tmpImageFilePath);
                long filesize = f.length();
                ExifInterface exif = new ExifInterface(f.getPath());
                int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

                int angle = 0;

                if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
                    angle = 90;
                }
                else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
                    angle = 180;
                }
                else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
                    angle = 270;
                }

                Matrix mat = new Matrix();
                mat.postRotate(angle);
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 8;
                Bitmap bmp = BitmapFactory.decodeFile(tmpImageFilePath, options);    
                Bitmap correctBmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), mat, true);
                bmp.recycle();
                try {
                    FileOutputStream out = new FileOutputStream(f);
                    correctBmp.compress(Bitmap.CompressFormat.JPEG, 90, out);
                } catch (Exception e) {
                    e.printStackTrace();
                }

我的形象较少。

0 个答案:

没有答案
相关问题