【问题标题】:Android camera image randomly rotatedAndroid相机图像随机旋转
【发布时间】:2013-01-14 15:05:28
【问题描述】:

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

【问题讨论】:

标签: android random camera image-rotation


【解决方案1】:

试试这个。

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-07-03
    • 1970-01-01
    • 2014-01-20
    • 2014-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多