Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/204.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 从文件中修改图像并保存_Android_Image Processing_Bitmap - Fatal编程技术网

Android 从文件中修改图像并保存

Android 从文件中修改图像并保存,android,image-processing,bitmap,Android,Image Processing,Bitmap,我需要将图像从一个文件加载到另一个图像视图,在上面绘制一些东西并保存到同一个文件中。所有地雷修改都在名为mCanvasView的单独视图中绘制。这是我的保存代码: private void saveResult() { OutputStream stream = null; try { Bitmap result; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)

我需要将图像从一个文件加载到另一个图像视图,在上面绘制一些东西并保存到同一个文件中。所有地雷修改都在名为
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。我做错了什么

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


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

查看此图片了解照片的方向对性能没有任何实际的评论,但您可能希望检查我正在通过intent使用默认相机。@Lingviston这说明了如何获取有关发生旋转的信息,使用这些信息,你可以适当地旋转你的图像。看起来所有这些转换都是相当消耗内存的操作。我不明白他们把这么简单的手术弄得这么难。无法将我的覆盖直接“绘制”到具有初始图像的文件?首先,您确定具有初始图像的文件包含正确定向的图像吗?其次,我建议捕获位图,根据上面的链接旋转它(如果需要),应用覆盖,然后将其保存到文件捕获和编辑是独立的操作。它们是通过应用程序的不同部分完成的。