0

我需要将图像从文件加载到 ImageView 上绘制一些东西并保存到同一个文件中。我的所有修改都绘制在名为mCanvasView. 这是我的保存代码:

    private void saveResult() {
    OutputStream stream = null;
    try {
        Bitmap result;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inMutable = true;
            result = BitmapFactory.decodeFile(mFilePath, options);
        } else {
            Bitmap bitmap = BitmapFactory.decodeFile(mFilePath);
            result = bitmap.copy(Bitmap.Config.ARGB_8888, true);
            bitmap.recycle();
        }
        float resultWidth = result.getWidth();
        float resultHeight = result.getHeight();
        Canvas canvas = new Canvas(result);
        Bitmap overlay = mCanvasView.getDrawingCache();
        Matrix matrix = new Matrix();
        matrix.setScale(resultWidth / overlay.getWidth(), resultHeight
                / overlay.getHeight());
        canvas.drawBitmap(overlay, matrix, new Paint());
        stream = new FileOutputStream(mFilePath);
        result.compress(CompressFormat.PNG, 100, stream);
    } catch (Exception e) {
        e.printStackTrace();
        leaveActivityWithMissingData();
    } finally {
        IOUtils.closeQuietly(stream);
    }
}

首先 - 它真的很慢。我不明白为什么。其次,如果以纵向模式拍摄图像,则保存的图像将被旋转。为什么?我不应用任何旋转。

我已经像这样修改了我的代码以使用来自 EXIF 的旋转数据:

    OutputStream stream = null;
    try {
        Bitmap original;
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inMutable = true;
            original = BitmapFactory.decodeFile(mFilePath, options);
        } else {
            Bitmap bitmap = BitmapFactory.decodeFile(mFilePath);
            original = bitmap.copy(Bitmap.Config.ARGB_8888, true);
            bitmap.recycle();
        }
        int resultWidth = original.getWidth();
        int resultHeight = original.getHeight();
        int rotation = Utils.getRotation(mFilePath);
        if (rotation % 180 != 0) {
            resultHeight = original.getWidth();
            resultWidth = original.getHeight();
        }
        Bitmap result = Bitmap.createBitmap(resultWidth, resultHeight,
                Config.ARGB_8888);
        Canvas canvas = new Canvas(result);
        Matrix rotationMatrix = new Matrix();
        rotationMatrix.setRotate(rotation, resultWidth / 2,
                resultHeight / 2);
        canvas.drawBitmap(original, rotationMatrix, new Paint());
        original.recycle();
        Bitmap overlay = mCanvasView.getDrawingCache();
        Matrix scaleMatrix = new Matrix();
        scaleMatrix.setScale(resultWidth / overlay.getWidth(), resultHeight
                / overlay.getHeight());
        canvas.drawBitmap(overlay, scaleMatrix, new Paint());
        overlay.recycle();

        stream = new FileOutputStream(mFilePath);
        result.compress(CompressFormat.PNG, 100, stream);
        result.recycle();
    } catch (Exception e) {
        e.printStackTrace();
        leaveActivityWithMissingData();
    } finally {
        IOUtils.closeQuietly(stream);
    }

但这会导致每次通话都出现 OOM。我究竟做错了什么?

4

1 回答 1

0

检查可能有助于了解性能问题。
另外,不要忘记stream在完成压缩后关闭你的。

至于旋转 - 可能是您的相机设置原始预览模式setDisplayOrientation(90)或类似?然后,您可以随时通过应用旋转以匹配图像以进行预览matrix.postRotate(90);

于 2013-11-01T18:25:00.743 回答