Android 保存前旋转图片 [英] Android Rotate Picture before saving
问题描述
我刚刚完成了我的相机活动,它非常好地保存了数据.拍完照片后我做了什么:
I just finished my camera activity and it's wonderfully saving the data. What I do after the picture is taken:
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();
}
}
我想要的已经是上面代码中给出的注释.我不希望图像只是保存到文件中,而是要旋转然后保存!谢谢!
What I want to is already as comment given in the code above. I dont want the image just to be saved to file but to be rotated and then saved! Thanks!
//编辑:我目前在做什么(工作但仍然遇到大图像的内存问题)
//EDIT: What I am currently up to (Works but still runs into memory issues with large images)
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();
推荐答案
在创建 FileOutputStream
之前,您可以从使用矩阵
.为此,您将使用此方法:
Before you create your FileOutputStream
you can create a new Bitmap
from the original that has been transformed using a Matrix
. To do that you would use this method:
createBitmap(Bitmap source, int x, int y, int width, int height, Matrix m, boolean filter)
其中 m
定义了一个矩阵,用于转置原始位图.
Where the m
defines a matrix that will transpose your original bitmap.
有关如何执行此操作的示例,请查看此问题:Android:如何在中心点上旋转位图
For an example on how to do this look at this question: Android: How to rotate a bitmap on a center point
这篇关于Android 保存前旋转图片的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!