ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2019-10-06 21:28:03  阅读:193  来源: 互联网

标签:android-gallery android android-camera android-imageview


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

这是我的代码解码从官方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;
}

解决方法:

图像具有不同的方向,因此在进行imageview时它会根据方向旋转.您可以从图像属性检查照片的方向.
要以正确的方式设置图像,您可以使用以下代码…

     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类以根据方向旋转类.

 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;

      }
          }

标签:android-gallery,android,android-camera,android-imageview
来源: https://codeday.me/bug/20191006/1862566.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有