为什么使用相机意图捕获的图像在Android中的某些设备上旋转 [英] why image captured using camera intent gets rotated on some devices in android

查看:233
本文介绍了为什么使用相机意图捕获的图像在Android中的某些设备上旋转的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在捕获图片并将其设置为图片视图。

I'm capturing image and setting it to image view.

public void captureImage() {
    Intent intentCamera = new Intent("android.media.action.IMAGE_CAPTURE");
    File filePhoto = new File(Environment.getExternalStorageDirectory(), "Pic.jpg");
    imageUri = Uri.fromFile(filePhoto);
    MyApplicationGlobal.imageUri = imageUri.getPath();
    intentCamera.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
    startActivityForResult(intentCamera, TAKE_PICTURE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intentFromCamera) {
    super.onActivityResult(requestCode, resultCode, intentFromCamera);
    if (resultCode == RESULT_OK && requestCode == TAKE_PICTURE) {
        if (intentFromCamera != null) {
            Bundle extras = intentFromCamera.getExtras();
            if (extras.containsKey("data")) {
                bitmap = (Bitmap) extras.get("data");
            } else {
                bitmap = getBitmapFromUri();
            }
        } else {
            bitmap = getBitmapFromUri();
        }
        // imageView.setImageBitmap(bitmap);
        imageView.setImageURI(imageUri);
    } else {
    }
}

public Bitmap getBitmapFromUri() {
    getContentResolver().notifyChange(imageUri, null);
    ContentResolver cr = getContentResolver();
    Bitmap bitmap;
    try {
        bitmap = android.provider.MediaStore.Images.Media.getBitmap(cr, imageUri);
        return bitmap;
    } catch (Exception e) {
        e.printStackTrace();
        return null;
    }
}

但问题是,旋转。例如在三星设备上,它的工作原理,索尼Experia图像旋转90度和东芝thrive(tab)180度

But problem is, the image on some devices everytime it gets rotated. e.g. on Samsung device it works good, on Sony Experia image gets rotated by 90 degree and on Toshiba thrive(tab) by 180 degree

推荐答案

大多数手机相机都是横向的,这意味着如果你以纵向拍摄照片,结果照片将旋转90度。在这种情况下,照相机软件应该在照片应该被查看的方向填充EXIF数据。

Most phone cameras are landscape, meaning if you take the photo in portrait, the resulting photos will be rotated 90 degrees. In this case, the camera software should populate the EXIF data with the orientation that the photo should be viewed in.

请注意,下面的解决方案取决于相机软件/设备制造商

Note that below solution depends on the camera software/device manufacturer populating the EXIF data, so it will work in most cases, but it is not a 100% reliable solution.

ExifInterface ei = new ExifInterface(photoPath);
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
        ExifInterface.ORIENTATION_UNDEFINED);

switch(orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotateImage(bitmap, 90);
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotateImage(bitmap, 180);
        break;
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotateImage(bitmap, 270);
        break;
    case ExifInterface.ORIENTATION_NORMAL:
    default:
        break;
}

这里是 rotateImage 方法:

public static Bitmap rotateImage(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix,
            true);
}

这篇关于为什么使用相机意图捕获的图像在Android中的某些设备上旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆