首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >修改文件中的图像并保存

修改文件中的图像并保存
EN

Stack Overflow用户
提问于 2013-11-02 01:55:01
回答 1查看 788关注 0票数 0

我需要加载图像从文件到ImageView,在上面绘制一些东西,并保存到相同的文件。所有的矿山修改都绘制在单独的视图中,称为mCanvasView。下面是我的保存代码:

代码语言:javascript
运行
复制
    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中的旋转数据:

代码语言:javascript
运行
复制
    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。我做错了什么?

EN

回答 1

Stack Overflow用户

发布于 2013-11-02 02:25:01

检查this可能有助于了解性能问题。

另外,别忘了在完成压缩后关闭你的stream

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/19732466

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档