4

我正在尝试旋转手机摄像头(8 兆像素)拍摄的大图像。只是来自源位图的旋转矩阵的 Bitmap.createBitmap 会遇到 OutOfMemoryError。这就是为什么我试图分割成旋转的部分,然后将它们再次组合成单个旋转图像以避免在内存中保存 2 个大位图,但即使在我的三星 S3 上也需要长达 10 秒。还有其他方法可以做到这一点吗?

   public static void rotateImage(File file)
            throws FileNotFoundException {
        long time = System.currentTimeMillis();
        List<File> fileList = new ArrayList<File>();

        // For the number of rows and columns
        int rowsCols = 3;

        Matrix matrix = new Matrix();
        matrix.postRotate(-90);

        int chunkHeight, chunkWidth;

        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;

        BitmapFactory.decodeStream(new FileInputStream(file), null, options);

        int height = options.outHeight;
        int width = options.outWidth;

        chunkHeight = height / rowsCols;
        chunkWidth = width / rowsCols;

        BitmapFactory.Options o2 = new BitmapFactory.Options();
        o2.inSampleSize = 1;
        o2.inPurgeable = true;
        o2.inInputShareable = true;
        o2.inPreferredConfig = Bitmap.Config.ARGB_8888;

        Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file),
                null, o2);

        // Cut the small rotated pieces and save to the files
        int yCoord = 0;
        for (int y = 0; y < rowsCols; y++) {
            int xCoord = 0;
            for (int x = 0; x < rowsCols; x++) {
                split(chunkHeight, chunkWidth, yCoord, x, xCoord, y, bitmap,
                        matrix, fileList);
                xCoord += chunkWidth;
            }
            yCoord += chunkHeight;
        }
        bitmap.recycle();
        Bitmap rotatedBitmap = Bitmap.createBitmap(height, width,
                Bitmap.Config.ARGB_8888);
        Canvas canvas = new Canvas(rotatedBitmap);

        // Combine the small pieces into the one output canvas
        int xCoord;
        xCoord = 0;
        for (int x = 0; x < rowsCols; x++) {  
            yCoord = 0;
            for (int y = 0; y < rowsCols; y++) {
                combineIntoCanvas(o2, canvas, xCoord, yCoord, fileList);
                yCoord -= chunkWidth;
            }
            xCoord += chunkHeight;
        }

        //Save the bitmap to the file
        rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 90,
                new FileOutputStream(file, false));
        rotatedBitmap.recycle();
        Log.add("combined in " + (System.currentTimeMillis() - time));
    }

    private static void combineIntoCanvas(BitmapFactory.Options o2,
            Canvas canvas, int xCoord, int yCoord, List<File> fileList)
            throws FileNotFoundException {
        File file = fileList.get(0);
        Bitmap bitmap = BitmapFactory.decodeStream(new FileInputStream(file),
                null, o2);
        canvas.drawBitmap(bitmap, xCoord, yCoord, null);
        bitmap.recycle();
        file.delete();
        fileList.remove(0);
    }

    private static void split(int chunkHeight, int chunkWidth, int yCoord,
            int x, int xCoord, int y, Bitmap bitmap, Matrix matrix,
            List<File> fileList) throws FileNotFoundException {

        Bitmap pieceOfBitmap = Bitmap.createBitmap(bitmap, xCoord, yCoord,
                chunkWidth, chunkHeight, matrix, true);
        File splitFile = new File(Globals.IMAGE_BASE_GALLERY_FOLDER() + "temp"
                + x + "_" + y + "." + "jpg");
        fileList.add(splitFile);

        pieceOfBitmap.compress(Bitmap.CompressFormat.PNG, 0,
                new FileOutputStream(splitFile, false));
        pieceOfBitmap.recycle();
    }
4

3 回答 3

1

当您尝试拍摄从相机拍摄的照片时。照片尺寸太大,如果开始直接播放图像,则会出现 OutOfMemory。所以最好将图像更改为样本大小,然后尝试使用您的代码旋转。尽量控制使用的位图数量。在这里,当您这样做时,优化代码将对您有很大帮助,因为 OutOfMemory 是位图的常见问题。这篇文章的代码将帮助您使用 samplesize 解码图像文件。此链接可以帮助您

于 2012-10-18T10:34:15.823 回答
0

对于像我这样的其他人:

您可以让相机执行旋转, https://stackoverflow.com/a/16010289/755804

于 2013-04-15T10:55:11.890 回答
0

最简单的方法就是使用OpenGL来显示和操作这么大的图像。1. 纹理内存不包括在应用程序内存限制中 2. 在所有现代设备上,此过程由 GPU 3 加速。它使许多其他工作变得容易 - 缩放、移动、动画等。

于 2013-04-15T11:22:01.843 回答