【问题标题】:Android Rotate Picture before saving保存前Android旋转图片
【发布时间】:2012-03-25 06:00:42
【问题描述】:

我刚刚完成了我的相机活动,它可以很好地保存数据。 拍完照片后我会做什么:

protected void savePictureData() {
    try {
        FileOutputStream fs = new FileOutputStream(this.photo);
        fs.write(this.lastCamData);
        fs.close(); //okay, wonderful! file is just written to the sdcard

        //---------------------
        //---------------------
        //TODO in here: dont save just the file but ROTATE the image and then save it!
        //---------------------
        //---------------------


        Intent data = new Intent(); //just a simple intent returning some data...
        data.putExtra("picture_name", this.fname);
        data.putExtra("byte_data", this.lastCamData);
        this.setResult(SAVED_TOOK_PICTURE, data);
        this.finish(); 
    } catch (IOException e) {
        e.printStackTrace();
        this.IOError();
    }

}

我想要的已经是上面代码中给出的注释。我不希望图像只是保存到文件中,而是要旋转然后保存!谢谢!

//编辑:我目前在做什么(工作但仍然遇到大图像的内存问题)

byte[] pictureBytes;
Bitmap thePicture = BitmapFactory.decodeByteArray(this.lastCamData, 0, this.lastCamData.length);
Matrix m = new Matrix();
m.postRotate(90);
thePicture = Bitmap.createBitmap(thePicture, 0, 0, thePicture.getWidth(), thePicture.getHeight(), m, true);

ByteArrayOutputStream bos = new ByteArrayOutputStream();
thePicture.compress(CompressFormat.JPEG, 100, bos);
pictureBytes = bos.toByteArray();

FileOutputStream fs = new FileOutputStream(this.photo);
fs.write(pictureBytes);
fs.close();
Intent data = new Intent();
data.putExtra("picture_name", this.fname);
data.putExtra("byte_data", pictureBytes);
this.setResult(SAVED_TOOK_PICTURE, data);
this.finish();

【问题讨论】:

  • 不,不是真的,更多的是关于如何旋转和保存之后
  • 好吧,我的解决方案似乎有效 - 清理了项目...
  • 那么哪一个正在工作,有问题的编辑或接受的答案建议?
  • 当有足够的可用内存时编辑工作(正常情况应该是什么);)

标签: android image file io camera


【解决方案1】:

从sd卡读取路径并粘贴以下代码...旋转后将替换现有照片..

注意:Exif 不适用于大多数设备,它返回的数据不正确,因此最好在保存到任何您想要的角度之前对旋转进行硬编码,只需更改 postRotate 中的角度值。

    String photopath = tempphoto.getPath().toString();
    Bitmap bmp = BitmapFactory.decodeFile(photopath);

    Matrix matrix = new Matrix();
    matrix.postRotate(90);
    bmp = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(), bmp.getHeight(), matrix, true);

    FileOutputStream fOut;
    try {
        fOut = new FileOutputStream(tempphoto);
        bmp.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
        fOut.flush();
        fOut.close();

    } catch (FileNotFoundException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

}

【讨论】:

  • 我们程序员处理照片的问题来自于位图类!始终,您将图像传递给位图类,这会将照片旋转 90 度。然后解决方案是我们总是使用位图类,我们需要显示照片,我们需要应用'matrix.postRotate(90);'创建旋转位图。
【解决方案2】:

在创建FileOutputStream 之前,您可以从已使用Matrix 转换的原始Bitmap 创建一个新的Bitmap。为此,您将使用以下方法:

createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)

m 定义了一个将转置原始位图的矩阵。

有关如何执行此操作的示例,请查看此问题: Android: How to rotate a bitmap on a center point

【讨论】:

  • 嘿,看看我的编辑。我试过,但它不像这样工作......:/
  • 我正在尝试类似的方法。但是每次我复制位图时都会出现异常:内存不足...
  • @JoubertVasconcelos,那么你需要缩小图像。
【解决方案3】:
bitmap = RotateBitmap(bitmap, 90);

public static Bitmap RotateBitmap(Bitmap source, float angle)
{
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-01-20
    • 1970-01-01
    • 1970-01-01
    • 2017-05-25
    相关资源
    最近更新 更多