Android EXIF

找了些sample代码,暂且记录下来,方便今后参考:

以下参考代码不保证正确性,只是提供个思路。


(1) http://stackoverflow.com/questions/12726860/android-how-to-detect-the-image-orientation-portrait-or-landscape-picked-fro

  1. <span style="font-size: 14px;">public int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){ 
  2.     int rotate = 0
  3.     try
  4.         context.getContentResolver().notifyChange(imageUri, null); 
  5.         File imageFile = new File(imagePath); 
  6.  
  7.         ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath()); 
  8.         int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL); 
  9.  
  10.         switch (orientation) { 
  11.         case ExifInterface.ORIENTATION_ROTATE_270: 
  12.             rotate = 270
  13.             break
  14.         case ExifInterface.ORIENTATION_ROTATE_180: 
  15.             rotate = 180
  16.             break
  17.         case ExifInterface.ORIENTATION_ROTATE_90: 
  18.             rotate = 90
  19.             break
  20.         } 
  21.  
  22.         Log.i("RotateImage", "Exif orientation: " + orientation); 
  23.         Log.i("RotateImage", "Rotate value: " + rotate); 
  24.     } catch (Exception e) { 
  25.         e.printStackTrace(); 
  26.     } 
  27.     return rotate; 
  28. }</span> 
<span style="font-size:14px;">public int getCameraPhotoOrientation(Context context, Uri imageUri, String imagePath){
    int rotate = 0;
    try {
        context.getContentResolver().notifyChange(imageUri, null);
        File imageFile = new File(imagePath);

        ExifInterface exif = new ExifInterface(imageFile.getAbsolutePath());
        int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);

        switch (orientation) {
        case ExifInterface.ORIENTATION_ROTATE_270:
            rotate = 270;
            break;
        case ExifInterface.ORIENTATION_ROTATE_180:
            rotate = 180;
            break;
        case ExifInterface.ORIENTATION_ROTATE_90:
            rotate = 90;
            break;
        }

        Log.i("RotateImage", "Exif orientation: " + orientation);
        Log.i("RotateImage", "Rotate value: " + rotate);
    } catch (Exception e) {
        e.printStackTrace();
    }
    return rotate;
}</span>



(2) http://stackoverflow.com/questions/3647993/android-bitmaps-loaded-from-gallery-are-rotated-in-imageview


First you need to create an ExifInterface


  1. <span style="font-family: Arial, Helvetica, sans-serif; font-size: 14px;">ExifInterface exif = new ExifInterface(filename);</span> 
<span style="font-family:Arial, Helvetica, sans-serif;font-size:14px;">ExifInterface exif = new ExifInterface(filename);</span>


You  can then grab the orientation of the image

  1.  
</pre><div class="dp-highlighter bg_java"><div class="bar"><div class="tools"><strong>[java]</strong> <a target=_blank class="ViewSource" title="view plain" href="http://blog.csdn.net/happy08god/article/details/11535767#">view plain</a><a target=_blank class="CopyToClipboard" title="copy" href="http://blog.csdn.net/happy08god/article/details/11535767#">copy</a><a target=_blank class="PrintSource" title="print" href="http://blog.csdn.net/happy08god/article/details/11535767#">print</a><a target=_blank class="About" title="?" href="http://blog.csdn.net/happy08god/article/details/11535767#">?</a></div></div><ol class="dp-j"><li class="alt"><span><span><span style=</span><span class="string">"font-size: 14px;"</span><span>>orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, </span><span class="number">1</span><span>);  </span></span></li><li><span>Here's what the orientation values mean: http:<span class="comment">//sylvana.net/jpegcrop/exif_orientation.html</span><span>  </span></span></li><li class="alt"><span>  </span></li><li><span></span>  </span></li></ol></div><pre style="display: none;" class="java" name="code"><span style="font-size:14px;">orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Here's what the orientation values mean: http://sylvana.net/jpegcrop/exif_orientation.html

</span>
  1. <span style="font-size: 14px;"
  2. </span> 
<span style="font-size:14px;">
</span>
  1. <span style="font-size: 14px;"
  2. So, the most important values are 3, 6 and 8. If the orientation is 6, for example, you can rotate the image like this
  3. </span> 
<span style="font-size:14px;">
So, the most important values are 3, 6 and 8. If the orientation is 6, for example, you can rotate the image like this:
</span>


  1. <span style="font-size: 14px;">Matrix matrix = new Matrix(); 
  2. matrix.postRotate(90); 
  3. rotatedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, true);</span> 
<span style="font-size:14px;">Matrix matrix = new Matrix();
matrix.postRotate(90);
rotatedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, true);</span>


This is a full solution (found in the Hackbook example from the Facebook SDK). It has the advantage of not needing access to the file itself. This is extremely useful if you are loading an image from the content resolver thingy (e.g. if your app is responding to a share-photo intent).


  1. public static int getOrientation(Context context, Uri photoUri) { 
  2.     /* it's on the external media. */ 
  3.     Cursor cursor = context.getContentResolver().query(photoUri, 
  4.             new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null); 
  5.  
  6.     if (cursor.getCount() != 1) { 
  7.         return -1
  8.     } 
  9.  
  10.     cursor.moveToFirst(); 
  11.     return cursor.getInt(0); 
public static int getOrientation(Context context, Uri photoUri) {
    /* it's on the external media. */
    Cursor cursor = context.getContentResolver().query(photoUri,
            new String[] { MediaStore.Images.ImageColumns.ORIENTATION }, null, null, null);

    if (cursor.getCount() != 1) {
        return -1;
    }

    cursor.moveToFirst();
    return cursor.getInt(0);
}


And then you can get a rotated Bitmap as follows. This code also scales down the image (badly unfortunately) to MAX_IMAGE_DIMENSION. Otherwise you may run out of memory.


  1. public static Bitmap getCorrectlyOrientedImage(Context context, Uri photoUri) throws IOException { 
  2.     InputStream is = context.getContentResolver().openInputStream(photoUri); 
  3.     BitmapFactory.Options dbo = new BitmapFactory.Options(); 
  4.     dbo.inJustDecodeBounds = true
  5.     BitmapFactory.decodeStream(is, null, dbo); 
  6.     is.close(); 
  7.  
  8.     int rotatedWidth, rotatedHeight; 
  9.     int orientation = getOrientation(context, photoUri); 
  10.  
  11.     if (orientation == 90 || orientation == 270) { 
  12.         rotatedWidth = dbo.outHeight; 
  13.         rotatedHeight = dbo.outWidth; 
  14.     } else
  15.         rotatedWidth = dbo.outWidth; 
  16.         rotatedHeight = dbo.outHeight; 
  17.     } 
  18.  
  19.     Bitmap srcBitmap; 
  20.     is = context.getContentResolver().openInputStream(photoUri); 
  21.     if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) { 
  22.         float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION); 
  23.         float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION); 
  24.         float maxRatio = Math.max(widthRatio, heightRatio); 
  25.  
  26.         // Create the bitmap from file 
  27.         BitmapFactory.Options options = new BitmapFactory.Options(); 
  28.         options.inSampleSize = (int) maxRatio; 
  29.         srcBitmap = BitmapFactory.decodeStream(is, null, options); 
  30.     } else
  31.         srcBitmap = BitmapFactory.decodeStream(is); 
  32.     } 
  33.     is.close(); 
  34.  
  35.     /*
  36.      * if the orientation is not 0 (or -1, which means we don't know), we
  37.      * have to do a rotation.
  38.      */ 
  39.     if (orientation > 0) { 
  40.         Matrix matrix = new Matrix(); 
  41.         matrix.postRotate(orientation); 
  42.  
  43.         srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(), 
  44.                 srcBitmap.getHeight(), matrix, true); 
  45.     } 
  46.  
  47.     return srcBitmap; 
public static Bitmap getCorrectlyOrientedImage(Context context, Uri photoUri) throws IOException {
    InputStream is = context.getContentResolver().openInputStream(photoUri);
    BitmapFactory.Options dbo = new BitmapFactory.Options();
    dbo.inJustDecodeBounds = true;
    BitmapFactory.decodeStream(is, null, dbo);
    is.close();

    int rotatedWidth, rotatedHeight;
    int orientation = getOrientation(context, photoUri);

    if (orientation == 90 || orientation == 270) {
        rotatedWidth = dbo.outHeight;
        rotatedHeight = dbo.outWidth;
    } else {
        rotatedWidth = dbo.outWidth;
        rotatedHeight = dbo.outHeight;
    }

    Bitmap srcBitmap;
    is = context.getContentResolver().openInputStream(photoUri);
    if (rotatedWidth > MAX_IMAGE_DIMENSION || rotatedHeight > MAX_IMAGE_DIMENSION) {
        float widthRatio = ((float) rotatedWidth) / ((float) MAX_IMAGE_DIMENSION);
        float heightRatio = ((float) rotatedHeight) / ((float) MAX_IMAGE_DIMENSION);
        float maxRatio = Math.max(widthRatio, heightRatio);

        // Create the bitmap from file
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inSampleSize = (int) maxRatio;
        srcBitmap = BitmapFactory.decodeStream(is, null, options);
    } else {
        srcBitmap = BitmapFactory.decodeStream(is);
    }
    is.close();

    /*
     * if the orientation is not 0 (or -1, which means we don't know), we
     * have to do a rotation.
     */
    if (orientation > 0) {
        Matrix matrix = new Matrix();
        matrix.postRotate(orientation);

        srcBitmap = Bitmap.createBitmap(srcBitmap, 0, 0, srcBitmap.getWidth(),
                srcBitmap.getHeight(), matrix, true);
    }

    return srcBitmap;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值