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

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


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

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;}



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


FirstyouneedtocreateanExifInterface


ExifInterface exif = new ExifInterface(filename);


You can then grab the orientation of the image

  
orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);Here's what the orientation values mean: http://sylvana.net/jpegcrop/exif_orientation.html
So, the most important values are 3, 6 and 8. If the orientation is 6, for example, you can rotate the image like this:


Matrix matrix = new Matrix();matrix.postRotate(90);rotatedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, true);


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).


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.


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;}




更多相关文章

  1. 有关android安全性的问题--代码混淆
  2. Android NDK so crash,定位目标代码使用
  3. [置顶] android俄罗斯方块完整代码
  4. 某个蝰蛇音效的卡刷包代码分析
  5. Android自动生成代码工具整理
  6. Android不常用代码(1)
  7. Android获取网页源代码
  8. android中执行线程的部分代码
  9. android 学习笔记有用代码片段(3)

随机推荐

  1. Android(安卓)开机视频
  2. Android(安卓)CursorAdapter
  3. android recovery模式流程
  4. Android(安卓)四大组件流程、Handler、As
  5. (ios实现)用c/c++混合编程方式为ios/andr
  6. Kotlin Android
  7. Android(安卓)N Settings 架构剖析
  8. 正确获得android设备的IP地址
  9. Android的Proxy/Delegate Application框
  10. [软件]安卓手机 kindle app 手工导入mobi