保存前Android旋转图片 [英] Android Rotate Picture before saving

查看:18
本文介绍了保存前Android旋转图片的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 之前,您可以从已转换的原始文件中创建一个新的 Bitmap矩阵.为此,您将使用以下方法:

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆