安装前Android旋转图片

时间:2012-03-07 17:08:47

标签: android image file io camera

我刚刚完成了我的相机活动,它可以很好地保存数据。 我拍摄照片后的所作所为:

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();

3 个答案:

答案 0 :(得分:20)

从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();
    }

}

答案 1 :(得分:4)

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

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

答案 2 :(得分:1)

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);
}