Android的旋转照片前节约 [英] Android Rotate Picture before saving

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

问题描述

我刚刚完成我的相机的活动,它的奇妙保存数据。 我要做的照片拍摄后:

 保护无效savePictureData(){
    尝试 {
        FileOutputStream中FS =新的FileOutputStream(this.photo);
        fs.write(this.lastCamData);
        fs.close(); //好,太好了!文件只是写入SD卡

        // ---------------------
        // ---------------------
        // TODO在这里:不要保存刚才的文件,但旋转图像,然后保存!
        // ---------------------
        // ---------------------


        意图数据=新的意图(); //只是一个简单的意向返回一些数据...
        data.putExtra(picture_name,this.fname);
        data.putExtra(BYTE_DATA,this.lastCamData);
        this.setResult(SAVED_TOOK_PICTURE,数据);
        this.finish();
    }赶上(IOException异常E){
        e.printStackTrace();
        this.IOError();
    }

}
 

我想已经是在code以上给出评论。我不想只是保存到文件,但被旋转并保存图像!谢谢!

// 修改:什么我目前达(工程,但仍然运行到内存中的问题与大型图片)

 字节[] pictureBytes;
位图thePicture = BitmapFactory.de codeByteArray(this.lastCamData,0,this.lastCamData.length);
矩阵M =新的Matrix();
m.postRotate(90);
thePicture = Bitmap.createBitmap(thePicture,0,0,thePicture.getWidth(),thePicture.getHeight()中,m,真);

ByteArrayOutputStream BOS =新ByteArrayOutputStream();
thePicture.com preSS(比较pressFormat.JPEG,100,BOS);
pictureBytes = bos.toByteArray();

FileOutputStream中FS =新的FileOutputStream(this.photo);
fs.write(pictureBytes);
fs.close();
意图数据=新的意图();
data.putExtra(picture_name,this.fname);
data.putExtra(BYTE_DATA,pictureBytes);
this.setResult(SAVED_TOOK_PICTURE,数据);
this.finish();
 

解决方案

在你创建你的的FileOutputStream 您可以创建一个新的位图从已使用矩阵改变了原来的。要做到这一点,你会使用这个方法:

  createBitmap(位图源,诠释的x,INT Y,INT宽度,高度INT,矩阵M,布尔过滤器)
 

M 定义了一个矩阵,将转你的原始位。

有关如何做到这一点看这个问题的一个例子: <一href="http://stackoverflow.com/questions/4166917/android-how-to-rotate-a-bitmap-on-a-center-point">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();

解决方案

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)

Where the m defines a matrix that will transpose your original bitmap.

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天全站免登陆