0

我发现了一个非常有趣的问题。拍完相机照片后(我将设备保持在纵向模式,而不是旋转),给定的照片有时会旋转,但并非总是如此。我知道,有些设备总是给出旋转的照片,但它可以用 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());
        }
}

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

4

1 回答 1

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");
    }
于 2013-09-23T08:51:02.120 回答