在 imageview 中使用时从相机或图库拍摄的图片其方向会改变,有时在 Android 中垂直拉伸 [英] Picture taken from camera or gallery when using in imageview its orientation getting changed, and sometimes vertically stretched in Android

查看:28
本文介绍了在 imageview 中使用时从相机或图库拍摄的图片其方向会改变,有时在 Android 中垂直拉伸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用程序中,我必须从相机捕获图像或从图库导入,然后在活动的图像视图中显示它.一切都很好,我正在从两者获取图像,并且能够毫无例外地在 imageview 上设置它.但有时图像无法正确缩放并被垂直拉伸或方向改变.请帮帮我.

In my application I have to capture image from camera or import from gallery, show it in imageview in activity. Everything is fine, I am getting image from both and able to set it on imageview without any exception. But sometimes image is not getting scaled properly and gets vertically stretched or with orientation changed. Please help me out.

这是我解码来自官方android文档的图像的代码:

Here is my code to decode image referred from official android documentation:

public static Bitmap decodeSampledBitmapFromResource(File photoFile, int reqWidth, int reqHeight) {

    // First decode with inJustDecodeBounds=true to check dimensions
    final BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    try {
        BitmapFactory.decodeStream(new FileInputStream(photoFile), null,
                options);

        // Calculate inSampleSize
        options.inSampleSize = calculateInSampleSize(options, reqWidth,
                reqHeight);

        // Decode bitmap with inSampleSize set
        options.inJustDecodeBounds = false;

        return BitmapFactory.decodeStream(new FileInputStream(photoFile),
                null, options);

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
        return null;
    }
}

public static int calculateInSampleSize(BitmapFactory.Options options,
        int reqWidth, int reqHeight) {
    // Raw height and width of image
    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {

        // Calculate ratios of height and width to requested height and
        // width
        final int heightRatio = Math.round((float) height
                / (float) reqHeight);
        final int widthRatio = Math.round((float) width / (float) reqWidth);

        // Choose the smallest ratio as inSampleSize value, this will
        // guarantee
        // a final image with both dimensions larger than or equal to the
        // requested height and width.
        inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio;
    }

    return inSampleSize;
}

推荐答案

图片有不同的方向,所以放在 imageview 时会根据方向旋转.您可以从图像的属性中检查照片的方向.要以正确的方式设置图像,您可以使用以下代码...

The images has different orientations so it rotates according to the orientation when putting on imageview. You can check the orientation of the photo from properties of image. To set the image in proper manner you can use the following code...

     int rot=getCameraPhotoOrientation(this,Uri,picturePath);
         if(rot!=0)
         bitmap=new RotateOrientation().RotateOrientationCall(bitmap,rot);

getCameraPhotoOrientation 方法:-

 public static int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
     int rotate = 0;
     try {
         context.getContentResolver().notifyChange(imageUri, null);
         File imageFile = new File(imagePath);
         ExifInterface exif = new ExifInterface(
                 imageFile.getAbsolutePath());
         int orientation = exif.getAttributeInt(
                 ExifInterface.TAG_ORIENTATION,
                 ExifInterface.ORIENTATION_NORMAL);

         switch (orientation) {
         case ExifInterface.ORIENTATION_ROTATE_270:
             rotate = 270;
             break;
         case ExifInterface.ORIENTATION_ROTATE_180:
             rotate = 180;
             break;
         case ExifInterface.ORIENTATION_ROTATE_90:
             rotate = 90;
             break;
         }


         Log.d(TAG, "Exit orientation: " + orientation);
     } catch (Exception e) {
         e.printStackTrace();
     }
    return rotate;
 }

添加RotateOrientation 类以根据方向旋转类.

Add RotateOrientation class to rotate class according to orientation.

 public class RotateOrientation  {

Bitmap RotateOrientationCall(Bitmap src,float degree)
        {


        Matrix matrix=new Matrix();
        matrix.postRotate(degree);
       Bitmap bmOut = Bitmap.createBitmap(src, 0, 0, src.getWidth(), src.getHeight(), matrix, true);
      return bmOut;

      }
          }

这篇关于在 imageview 中使用时从相机或图库拍摄的图片其方向会改变,有时在 Android 中垂直拉伸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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