图片时的ImageView其定位使用来自相机或画廊拍摄得到改变,有时垂直拉伸的机器人 [英] Picture taken from camera or gallery when using in imageview its orientation getting changed, and sometimes vertically stretched in Android

查看:128
本文介绍了图片时的ImageView其定位使用来自相机或画廊拍摄得到改变,有时垂直拉伸的机器人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以,在我的应用程序,我不得不从相机或进口从画廊捕捉图像,并用它在我的ImageView在我的活动。一切都很好,我从两个并能够在ImageView的设置没有任何异常获取图像。但有时图像没有得到正确缩放并获得垂直拉伸或定向changed.Please帮助我。

下面是我的code脱code形象,developer.google.com

 公共静态位图德codeSampledBitmapFromResource(文件photoFile,诠释reqWidth,诠释reqHeight){

    //第一代code与inJustDe codeBounds = true来检查尺寸
    最后BitmapFactory.Options选项=新BitmapFactory.Options();
    options.inJustDe codeBounds = TRUE;
    尝试 {
        BitmapFactory.de codeStream(新的FileInputStream(photoFile),空,
                选项​​);

        //计算inSampleSize
        options.inSampleSize = calculateInSampleSize(选项,reqWidth,
                reqHeight);

        与inSampleSize集//德code位图
        options.inJustDe codeBounds = FALSE;

        返回BitmapFactory.de codeStream(新的FileInputStream(photoFile)
                空,期权);

    }赶上(FileNotFoundException异常E){
        // TODO自动生成的catch块
        e.printStackTrace();
        返回null;
    }
}

公共静态INT calculateInSampleSize(BitmapFactory.Options选项,
        INT reqWidth,诠释reqHeight){
    //原始高度和宽度的图像
    最终诠释身高= options.outHeight;
    最终诠释宽度= options.outWidth;
    INT inSampleSize = 1;

    如果(高度> reqHeight ||宽度GT; reqWidth){

        //计算高度和宽度的比例来要求的高度和
        // 宽度
        最终诠释heightRatio = Math.round((浮点)高度
                /(浮点)reqHeight);
        最终诠释widthRatio = Math.round((浮点)宽/(浮点)reqWidth);

        //选择最小比例为inSampleSize值,这将
        // 保证
        //与两个尺寸大于或等于所述最终图像
        //请求的高度和宽度。
        inSampleSize = heightRatio< widthRatio? heightRatio:widthRatio;
    }

    返回inSampleSize;
}
 

解决方案

图像有不同的取向,因此根据方位上ImageView的推杆时旋转。您可以从图像的属性检查照片的方向。 要设置适当的方式,你可以使用下面的code图像...

  INT腐烂= getCameraPhotoOrientation(这一点,乌里,picturePath);
         如果(腐!= 0)
         位图=新RotateOrientation()RotateOrientationCall(位图,腐烂)。
 

getCameraPhotoOrientation 方式: -

 公共静态INT getCameraPhotoOrientation(上下文的背景下,开放的我们imageUri,字符串的ImagePath){
     INT旋转= 0;
     尝试 {
         context.getContentResolver()有NotifyChange(imageUri,空)。
         文件镜像文件=新的文件(的ImagePath);
         ExifInterface EXIF​​ =新ExifInterface(
                 imageFile.getAbsolutePath());
         INT方向= exif.getAttributeInt(
                 ExifInterface.TAG_ORIENTATION,
                 ExifInterface.ORIENTATION_NORMAL);

         开关(方向){
         案例ExifInterface.ORIENTATION_ROTATE_270:
             旋转= 270;
             打破;
         案例ExifInterface.ORIENTATION_ROTATE_180:
             旋转= 180;
             打破;
         案例ExifInterface.ORIENTATION_ROTATE_90:
             旋转= 90;
             打破;
         }


         Log.d(TAG,退出的方向:+方向);
     }赶上(例外五){
         e.printStackTrace();
     }
    返回旋转;
 }
 

添加 RotateOrientation 等级符合方向旋转类。

 公共类RotateOrientation {

位图RotateOrientationCall(位图SRC,浮度)
        {


        字模=新的Matrix();
        matrix.postRotate(度);
       位图bmOut = Bitmap.createBitmap(SRC,0,0,src.getWidth(),src.getHeight(),矩阵,真);
      返回bmOut;

      }
          }
 

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

Here is my code to decode image, from developer.google.com

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. 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:-

 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.

 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其定位使用来自相机或画廊拍摄得到改变,有时垂直拉伸的机器人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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