2016-08-01 33 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

是否解决了您的问题? –