2016-08-01 36 views
0

在我的應用程序中圖像在豎屏模式下從相機中單擊時發生旋轉,這種情況只發生在三星設備上,其餘情況正常。我實現了在堆棧溢出研究後,下面的代碼:僅在Samasung設備中從相機單擊時圖像旋轉

ExifInterface ei = new ExifInterface(imgFile.getPath()); 
int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_UNDEFINED); 

switch (orientation) { 
case ExifInterface.ORIENTATION_UNDEFINED: 
mBitmap = rotateImage(bitmap, 90); 
break; 
} 

此代碼可以幫助我解決在三星的這個問題,但現在是由攝像頭,可以在其他設備越來越旋轉點擊圖像時,由於這段代碼。

請讓我知道我該如何解決這個問題。

+0

至於單證雲:'getAttributeInt' 「返回指定標籤的整數值,如果圖片文件中沒有這樣的標籤或者該值不能被解析爲整數,返回defaultValue。「,這意味着標籤沒有被定義,並且ExifInterface.ORIENTATION_UNDEFINED(0)被返回,或者你的開關沒有處理ORIENTATION_(某些)情況。添加一個默認子句,然後記錄'orientation'返回。 – Bonatti

+0

好的非常感謝..會做到這一點..我已經檢查方向的價值,然後在開關它說0 .. – Keshav1234

+0

還請注意,這個「問題」是舊的,[自2012年以來](http://stackoverflow.com/問題/ 13245556/exif-orientation-tag-value-always-0-for-image-taken-portrait-camera-app-and)知道Samsung不會正確保存Exif數據。 [即使是Exif標準](https://en.wikipedia.org/wiki/Exchangeable_image_file_format#Problems)也會產生不兼容情況。由於Samsung不尊重Exif界面,您可以自己編輯該設置(如果您要求Camera Intent,請使用設備方向)或檢查位圖高度/寬度,然後相應地翻轉圖像。 – Bonatti

回答

0

如果您確信這只是三星設備的問題,您可以檢查設備製造商並將其添加到您的if(...)條件中。 This庫可以有很大的幫助。

也看看賈裏德Rummler的回答this問題:

但如果這是一個設備特定問題,可能在其他設備上也發生或可能在較新的三星設備的操作系統更新,最終被糾正。保持良好的檢查。

0

通返回度的位圖轉換,

try { 
     ExifInterface exif = new ExifInterface(imgPath); 
     String rotationAmount = exif 
       .getAttribute(ExifInterface.TAG_ORIENTATION); 
     if (!TextUtils.isEmpty(rotationAmount)) { 
      int rotationParam = Integer.parseInt(rotationAmount); 
      switch (rotationParam) { 
      case ExifInterface.ORIENTATION_NORMAL: 
       return 0; 
      case ExifInterface.ORIENTATION_ROTATE_90: 
       return 90; 
      case ExifInterface.ORIENTATION_ROTATE_180: 
       return 180; 
      case ExifInterface.ORIENTATION_ROTATE_270: 
       return 270; 
      default: 
       return 0; 
      } 
     } else { 
      return 0; 
     } 
    } catch (Exception ex) { 
     return 0; 
    } 
+0

我總是得到旋轉量爲0所以它總是被旋轉 – Keshav1234

+0

檢查它的更新代碼,獲取基於圖像的旋轉,首先獲取圖像位置,然後找到位圖旋轉要求。 – prakash

0

使用下面類

String path="path of your image"; 

imageview.setImageBitmap(ExifUtil.rotateBitmap(path, BitmapFactory.decodeFile(path))); 

ExifUtil.java

public class ExifUtil { 
    /** 
    * @see http://sylvana.net/jpegcrop/exif_orientation.html 
    */ 
    public static Bitmap rotateBitmap(String src, Bitmap bitmap) { 
     try { 
      int orientation = getExifOrientation(src); 

      if (orientation == 1) { 
       return bitmap; 
      } 

      Matrix matrix = new Matrix(); 
      switch (orientation) { 
      case 2: 
       matrix.setScale(-1, 1); 
       break; 
      case 3: 
       matrix.setRotate(180); 
       break; 
      case 4: 
       matrix.setRotate(180); 
       matrix.postScale(-1, 1); 
       break; 
      case 5: 
       matrix.setRotate(90); 
       matrix.postScale(-1, 1); 
       break; 
      case 6: 
       matrix.setRotate(90); 
       break; 
      case 7: 
       matrix.setRotate(-90); 
       matrix.postScale(-1, 1); 
       break; 
      case 8: 
       matrix.setRotate(-90); 
       break; 
      default: 
       return bitmap; 
      } 

      try { 
       Bitmap oriented = Bitmap.createBitmap(bitmap, 0, 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true); 
       bitmap.recycle(); 
       return oriented; 
      } catch (OutOfMemoryError e) { 
       e.printStackTrace(); 
       return bitmap; 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 

     return bitmap; 
    } 

    private static int getExifOrientation(String src) throws IOException { 
     int orientation = 1; 

     try { 
      /** 
      * if your are targeting only api level >= 5 
      * ExifInterface exif = new ExifInterface(src); 
      * orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1); 
      */ 
      if (Build.VERSION.SDK_INT >= 5) { 
       Class<?> exifClass = Class.forName("android.media.ExifInterface"); 
       Constructor<?> exifConstructor = exifClass.getConstructor(new Class[] { String.class }); 
       Object exifInstance = exifConstructor.newInstance(new Object[] { src }); 
       Method getAttributeInt = exifClass.getMethod("getAttributeInt", new Class[] { String.class, int.class }); 
       Field tagOrientationField = exifClass.getField("TAG_ORIENTATION"); 
       String tagOrientation = (String) tagOrientationField.get(null); 
       orientation = (Integer) getAttributeInt.invoke(exifInstance, new Object[] { tagOrientation, 1}); 
      } 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } catch (SecurityException e) { 
      e.printStackTrace(); 
     } catch (NoSuchMethodException e) { 
      e.printStackTrace(); 
     } catch (IllegalArgumentException e) { 
      e.printStackTrace(); 
     } catch (InstantiationException e) { 
      e.printStackTrace(); 
     } catch (IllegalAccessException e) { 
      e.printStackTrace(); 
     } catch (InvocationTargetException e) { 
      e.printStackTrace(); 
     } catch (NoSuchFieldException e) { 
      e.printStackTrace(); 
     } 

     return orientation; 
    } 
} 
+0

是否解決了您的問題? –