Android相机图像随机旋转

时间:2013-01-14 15:05:28

标签: android random camera image-rotation

我发现了一个非常有趣的问题。拍摄照片后(我将设备保持在纵向模式,而不是旋转),给定的照片有时会旋转,但并非总是如此。我知道,有些设备总是提供旋转照片,但可以使用exif或mediastore信息进行旋转。但在这种情况下,exif和mediastore表示方向为0,但图像是旋转的。最令人沮丧的是这完全是随机的。代码非常简单:

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, currentFileUri);
startActivityForResult(intent, RequestCodeCollection.CAMERA_IMAGE_CAPTURE);

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        try {
            oldImageExifInterface = new ExifInterface(currentFileUri.getPath());
        }
}

有人见过这个问题吗?我在操作系统更新(4.1.1)之后体验过Galaxy Nexus

1 个答案:

答案 0 :(得分:0)

试试这个。

try {
        File f = new File(imagePath);
        ExifInterface exif = new ExifInterface(f.getPath());
        int orientation = exif.getAttributeInt(
                ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_NORMAL);

        int angle = 0;

        if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
            angle = 90;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_180) {
            angle = 180;
        } else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
            angle = 270;
        }

        Matrix mat = new Matrix();
        mat.postRotate(angle);
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = 2;

        Bitmap bmp = BitmapFactory.decodeStream(new FileInputStream(f),
                null, options);
        bitmap = Bitmap.createBitmap(bmp, 0, 0, bmp.getWidth(),
                bmp.getHeight(), mat, true);
        ByteArrayOutputStream outstudentstreamOutputStream = new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 100,
                outstudentstreamOutputStream);
        imageView.setImageBitmap(correctBmp);

    } catch (IOException e) {
        Log.w("TAG", "-- Error in setting image");
    } catch (OutOfMemoryError oom) {
        Log.w("TAG", "-- OOM Error in setting image");
    }