簡體   English   中英

在imageview中使用時從相機或圖庫拍攝的照片,其方向發生變化,有時在Android中垂直拉伸

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

在我的應用程序中,我必須從相機捕獲圖像或從庫中導入,在活動中的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;

      }
          }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM