2013-12-09 17 views
29

我已經通過一些鏈接來獲取從默認圖像庫中選擇的圖像的正確圖像方向以便在所有設備中工作標準exif標籤總是返回0如何獲取從默認圖像庫中選擇的圖像的正確方向

EXIF orientation tag value always 0 for image taken with portrait camera app android

Exif orientation tag returns 0

Exif data TAG_ORIENTATION always 0

http://mobisocial.stanford.edu/news/2011/08/rotating-images-in-android/

如何獲得可在所有設備上工作的確切解決方案?

+0

我有答案。 http://stackoverflow.com/questions/29971319/image-orientation-android/32747566#32747566 –

+0

這是我遇到過的一個很棒的單線解決方案:> https://stackoverflow.com/a/34241250/8033090它可以花一秒鐘來加載,但我只是在圖片視圖的後面加入了一些文字,上面寫着「加載圖片」,圖片加載時會覆蓋文字。 –

回答

2

對我來說ExifInterface相當奏效這樣的:

ExifInterface exifInterface = new ExifInterface(imagePath); 
degree = Integer.parseInt(exifInterface.getAttribute(ExifInterface.TAG_ORIENTATION)); 

,或者你可以嘗試使用MediaStore這樣獲得圖像的細節:

String[] orientationColumn = {MediaStore.Images.Media.ORIENTATION}; 
Cursor cur = managedQuery(imageUri, orientationColumn, null, null, null); 
int orientation = -1; 
if (cur != null && cur.moveToFirst()) { 
    orientation = cur.getInt(cur.getColumnIndex(orientationColumn[0])); 
} 

類似的解決方案:ExifInterface always returns 1

希望它有助於.. :)

90

如果圖像(照片)是由您製作的程序拍攝的,則必須使用正確的旋轉值設置Parameters.setRotation。

這取決於相機驅動器在保存前旋轉圖像或將旋轉值保存爲exif TAG_ORIENTATION。

因此,如果TAG_ORIENTATION爲null或零,圖像的方向是正確的,否則您必須根據TAG_ORIENTATION中的值旋轉圖像。

CODE

得到EXIF方向:

ExifInterface exif = null; 
try { 
    exif = new ExifInterface(path); 
} catch (IOException e) { 
    e.printStackTrace(); 
} 
int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
             ExifInterface.ORIENTATION_UNDEFINED); 

獲取位圖旋轉:

Bitmap bmRotated = rotateBitmap(bitmap, orientation); 

方法旋轉位圖:

public static Bitmap rotateBitmap(Bitmap bitmap, int orientation) { 

    Matrix matrix = new Matrix(); 
    switch (orientation) { 
     case ExifInterface.ORIENTATION_NORMAL: 
      return bitmap; 
     case ExifInterface.ORIENTATION_FLIP_HORIZONTAL: 
      matrix.setScale(-1, 1); 
      break; 
     case ExifInterface.ORIENTATION_ROTATE_180: 
      matrix.setRotate(180); 
      break; 
     case ExifInterface.ORIENTATION_FLIP_VERTICAL: 
      matrix.setRotate(180); 
      matrix.postScale(-1, 1); 
      break; 
     case ExifInterface.ORIENTATION_TRANSPOSE: 
      matrix.setRotate(90); 
      matrix.postScale(-1, 1); 
      break; 
     case ExifInterface.ORIENTATION_ROTATE_90: 
      matrix.setRotate(90); 
      break; 
     case ExifInterface.ORIENTATION_TRANSVERSE: 
      matrix.setRotate(-90); 
      matrix.postScale(-1, 1); 
      break; 
     case ExifInterface.ORIENTATION_ROTATE_270: 
      matrix.setRotate(-90); 
      break; 
     default: 
      return bitmap; 
    } 
    try { 
     Bitmap bmRotated = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
     bitmap.recycle(); 
     return bmRotated; 
    } 
    catch (OutOfMemoryError e) { 
     e.printStackTrace(); 
     return null; 
    } 
} 
+0

如何從所選圖像的圖庫中獲取圖像方向請引導我 – ramsraj111

+0

我在答覆中添加了一些代碼。 – ramaral

+20

使用exif總是隻返回0 – ramsraj111

2

我遵循最後一個答案,我努力創建一個系統來管理圖片,旋轉,調整大小,緩存和加載到ImageViews,我可以告訴它是一個地獄。即使完成所有操作,崩潰有時會在某些設備中導致OutOfMemory。答案是正確的,但在Android中管理位圖很困難。

我的觀點是不要重新發明輪子,它有一個完美的設計。 Google本身鼓勵您使用Glide。它工作在一條線上,超級簡單易用,體積和功能數量都很輕巧,它管理EXIF默認爲,它使用內存就像魅力一樣。這簡直是​​妖術編碼;)

我不知道如果Picasso還管理EXIF,但有一個快速的介紹他們兩個:

https://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

我的建議是:不要浪費你的時間並使用它們。您可以在一行中解決您的問題:

Glide.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); 
+3

如果您在大多數設備上翻轉的前置攝像頭圖像存在問題,則此功能無效。Glide無法修復方向。 – NMP

+1

我使用滑動並且它不處理基於exif標記的方向 –

+0

真的使用哪個版本?我很確定它過去解決了我的問題。 –

1

對於那些誰沿着這個帖子來了,一定要使用exifinterface從在2016年12月推出了Android支持庫:

compile "com.android.support:exifinterface:25.1.0" // or newer 

詳細這個庫可以在根據Android開發者博客文章中找到:Introducing the ExifInterface Support Library

,還包括一個示例代碼來處理存儲在EXIF界面旋轉信息:

int rotation = 0; 
int orientation = exifInterface.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 

switch (orientation) { 
    case ExifInterface.ORIENTATION_ROTATE_90: 
    rotation = 90; 
    break; 
    case ExifInterface.ORIENTATION_ROTATE_180: 
    rotation = 180; 
    break; 
    case ExifInterface.ORIENTATION_ROTATE_270: 
    rotation = 270; 
    break; 
} 
相關問題