2016-12-22 50 views

回答

0

您可以通過使用ExitInterface解決這個問題,如下圖所示:

private void setPic(Uri contentUri) throws IOException { 
     // Get the dimensions of the View 
     int targetW = uploadedImage.getWidth(); 
     int targetH = uploadedImage.getHeight(); 

     // Get the dimensions of the bitmap 
     BitmapFactory.Options bmOptions = new BitmapFactory.Options(); 
     bmOptions.inJustDecodeBounds = true; 
     BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 

     // Decode the image file into a Bitmap sized to fill the View 
     bmOptions.inJustDecodeBounds = false; 
     bmOptions.inSampleSize = calculateInSampleSize(bmOptions, targetW, targetH); 

     Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions); 
     bitmap = rotateImageIfRequired(bitmap, contentUri); 
     bitmap = ThumbnailUtils.extractThumbnail(bitmap, 750, 750); 
     uploadedImage.setImageBitmap(bitmap); 
    } 

使用此函數將位圖是基於在手機的道路上進行解碼,然後它被調用rotateImageIfRequired()它將決定圖像在設置爲佈局背景之前應旋轉的方式。在這種情況下uploadedImage是你的佈局。

private Bitmap rotateImageIfRequired(Bitmap img, Uri contentUri) throws IOException { 
     ExifInterface ei = new ExifInterface(contentUri.getPath()); 
     int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

     switch (orientation) { 
      case ExifInterface.ORIENTATION_ROTATE_90: 
       return rotateImage(img, 90); 
      case ExifInterface.ORIENTATION_ROTATE_180: 
       return rotateImage(img, 180); 
      case ExifInterface.ORIENTATION_ROTATE_270: 
       return rotateImage(img, 270); 
      default: 
       return img; 
     } 
    } 

該功能將決定圖像在解碼之後和設置之前的哪個方位。

private static Bitmap rotateImage(Bitmap img, int degree) { 
     Matrix matrix = new Matrix(); 
     matrix.postRotate(degree); 
     Bitmap rotatedImg = Bitmap.createBitmap(img, 0, 0, img.getWidth(), img.getHeight(), matrix, true); 
     img.recycle(); 
     return rotatedImg; 
    } 

此功能旋轉圖像並返回新圖像。

希望這會有所幫助。

+0

做了這個幫助嗎? –

相關問題