使用相机的意图,为什么截取的图片在android的一些设备被旋转 [英] why image captured using camera intent gets rotated on some devices in android

查看:209
本文介绍了使用相机的意图,为什么截取的图片在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度和东芝茁壮成长(片)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.

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

switch(orientation) {
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotateImage(bitmap, 90);
        break;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotateImage(bitmap, 180);
        break;
    // etc.
}

请看>>> <一href="http://stackoverflow.com/questions/4166917/android-how-to-rotate-a-bitmap-on-a-center-point">THIS答&LT;&LT;&LT;学习如何旋转位图

Look at >>> THIS ANSWER <<< to learn how to rotate a Bitmap.

如果这不工作,使用方法:

If this doesnt work, use :

int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
                                       ExifInterface.ORIENTATION_UNDEFINED)

这篇关于使用相机的意图,为什么截取的图片在android的一些设备被旋转的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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