简体   繁体   English

在imageview中使用时从相机或图库拍摄的照片,其方向发生变化,有时在Android中垂直拉伸

[英]Picture taken from camera or gallery when using in imageview its orientation getting changed, and sometimes vertically stretched in Android

In my application I have to capture image from camera or import from gallery, show it in imageview in activity. 在我的应用程序中,我必须从相机捕获图像或从库中导入,在活动中的imageview中显示它。 Everything is fine, I am getting image from both and able to set it on imageview without any exception. 一切都很好,我从两者都获得图像,并能够毫无例外地在imageview上设置它。 But sometimes image is not getting scaled properly and gets vertically stretched or with orientation changed. 但有时图像不能正确缩放并垂直拉伸或方向改变。 Please help me out. 请帮帮我。

Here is my code to decode image referred from official android documentation: 这是我的代码解码从官方Android文档引用的图像:

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

The images has different orientations so it rotates according to the orientation when putting on imageview. 图像具有不同的方向,因此在进行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);

The getCameraPhotoOrientation Method:- 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;
 }

Add RotateOrientation class to rotate class according to orientation. 添加RotateOrientation类以根据方向旋转类。

 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;

      }
          }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在Android中使用摄像头:当照片是由摄像头拍摄而未从图库中选择时,为什么Cursor为null? - Working with camera in Android: Why is Cursor null when the picture is taken by camera and not selected from gallery? Android - 从图库/相机获取图像时,Dialog不会刷新ImageView - Android - Dialog not refresing ImageView when getting image from gallery/ camera 如何在图库中的imageView上设置图像以及在Android中由相机拍摄的图像? - How to set an image on imageView from the gallery and image taken by camera in Android? 从ImageView中显示的相机拍摄的照片始终是水平的Android - Picture taken from camera displayed in ImageView is always horizontal Android 在Android相机拍摄的imageview照片中显示图片 - display picture in imageview photo taken by camera in android 从图库中获取并上传时,ImageView 上的图像方向已更改 - Image orientation is changed on ImageView When fetching from Gallery and upload 图片在imageview android中拉伸 - the picture is stretched in imageview android Android相机预览在预览中拉伸,而不是在拍摄照片之后 - Android Camera Preview Stretched in Preview, Not After Picture Taken 检查imageview中的图片是否从相机或图库中捕获 - Check if picture in imageview was captured from camera or gallery 将图库图片显示到 ImageView Android 中时出现黑屏 - Getting Black Screen when displaying Gallery picture into ImageView Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM