2012-02-02 43 views
3

我正在使用圖庫選擇器從圖庫中選取圖片。照相機以縱向模式拍攝的照片在畫廊中顯示爲筆直。但是,當我導入照片時,我將照片旋轉(橫向)。只有畫廊顯示這張照片是筆直的。如何管理這個問題?我想要所有的照片作爲其實際的方向。 預先感謝Android圖庫導入

private void addImageFromGallery() { 

    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), 
      GALLERY_CODE); 

} 
+0

畫廊知道手機的確切方位同時拍照。所以它旋轉圖片。但我不明白。 – 2012-02-02 05:54:09

+0

** [本教程可以幫助您](http://startandroiddevelopment.blogspot.in/2013/10/importing-image-from-gallery.html)** – 2013-10-24 10:15:37

回答

11

得到的回答其旋轉到90。方向與EXIF格式的圖像一起保存。我們必須讀取每個圖像數據的方向標籤..

public static float rotationForImage(Context context, Uri uri) { 
     if (uri.getScheme().equals("content")) { 
     String[] projection = { Images.ImageColumns.ORIENTATION }; 
     Cursor c = context.getContentResolver().query(
       uri, projection, null, null, null); 
     if (c.moveToFirst()) { 
      return c.getInt(0); 
     } 
    } else if (uri.getScheme().equals("file")) { 
     try { 
      ExifInterface exif = new ExifInterface(uri.getPath()); 
      int rotation = (int)exifOrientationToDegrees(
        exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
          ExifInterface.ORIENTATION_NORMAL)); 
      return rotation; 
     } catch (IOException e) { 
      Log.e(TAG, "Error checking exif", e); 
     } 
    } 
     return 0f; 
    } 

    private static float exifOrientationToDegrees(int exifOrientation) { 
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { 
     return 90; 
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { 
     return 180; 
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { 
     return 270; 
    } 
    return 0; 
} 
} 

旋轉值可以用來校正照片的方向如下:

Matrix matrix = new Matrix(); 
float rotation = PhotoTaker.rotationForImage(context, uri); 
if (rotation != 0f) { 
     matrix.preRotate(rotation); 
} 

Bitmap resizedBitmap = Bitmap.createBitmap(
sourceBitmap, 0, 0, width, height, matrix, true); 
+0

偉大..正在尋找此.. – 2012-02-06 08:16:06

+0

@AndroSelva::) – 2012-02-06 09:27:48

+0

偉大的工作...位圖resizedBitmap = Bitmap.createBitmap(sourceBitmap,0,0,sourceBitmap.getWidth(),sourceBitmap.getHeight (),matrix,true); – Underdog 2012-08-23 02:51:12

0

而將其設置爲ImageView的,檢查是否圖象的寬度大於高度或不和如果需要的話

+0

這是不可能的,因爲我們正在獲取所有圖像比率640X480。 – 2012-02-02 07:59:58

+0

如果它在橫向,它會像480x640一樣嗎? – 2012-02-02 09:40:53

+1

得到答案...方向標籤與每張圖像一起保存。檢查此鏈接http://mobisocial.stanford.edu/news/2011/08/rotating-images-in-android/ – 2012-02-06 04:19:19