保存旋转的位图

时间:2015-02-11 11:10:27

标签: android matrix bitmap camera orientation

我从相机拍摄照片,我只是想以正确的方向保存它。

我知道他们有很多关于这个主题的帖子,但即使在阅读完所有答案之后,我仍然坚持要保存我的旋转位图。

所以这里是我的代码:

Bitmap bitmap = BitmapFactory.decodeFile(pictureFile
            .getAbsolutePath());
    Matrix matrix = new Matrix();
    matrix.postRotate(finalOrientation);
    Bitmap scaledBitmap = Bitmap.createScaledBitmap(bitmap, bitmap.getWidth(), bitmap.getHeight(), true);
    Bitmap rotatedBitmap = Bitmap.createBitmap(scaledBitmap, 0, 0, scaledBitmap.getWidth(), scaledBitmap.getHeight(), matrix, true);

    findViewById(R.id.image_preview).setVisibility(View.VISIBLE);
    ((ImageView) findViewById(R.id.image_preview)).setImageBitmap(rotatedBitmap);
    findViewById(R.id.surfaceView).setVisibility(View.GONE);

    if(outputStream != null)
    {
        rotatedBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);

        try {
            outputStream.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

正如您所见,我在图像视图中显示我的位图图像,以确保它处于正确的方向,并且确实如此。但只有当我保存(压缩)它才会返回原始状态时。

提前谢谢!

1 个答案:

答案 0 :(得分:0)

不是答案 ,但我想发布一些可能会对正在发生的事情有所了解的代码,并且它不符合评论:< / p>

ExifInterface exif;
int rotate = 0;
try {
       exif = new ExifInterface (file.getAbsolutePath());
       int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
       switch (exifOrientation) {
       case ExifInterface.ORIENTATION_ROTATE_90: rotate = 1 /*90*/; break;
       case ExifInterface.ORIENTATION_ROTATE_180: rotate = 2 /*180*/; break;
       case ExifInterface.ORIENTATION_ROTATE_270: rotate = 3 /*270*/; break;
       }
} catch (IOException e1) {
       // TODO Auto-generated catch block
       e1.printStackTrace();
}
Log.d(TAG,"~~~~~~ exifOrientation: rotate="+rotate);
相关问题