2012-02-02 57 views
3

我正在使用图库选择器从图库中选取图片。照相机以纵向模式拍摄的照片在画廊中显示为笔直。但是,当我导入照片时,我将照片旋转(横向)。只有画廊显示这张照片是笔直的。如何管理这个问题?我想要所有的照片作为其实际的方向。 预先感谢Android图库导入

private void addImageFromGallery() { 

    Intent intent = new Intent(); 
    intent.setType("image/*"); 
    intent.setAction(Intent.ACTION_GET_CONTENT); 
    startActivityForResult(Intent.createChooser(intent, "Select Picture"), 
      GALLERY_CODE); 

} 
+0

画廊知道手机的确切方位同时拍照。所以它旋转图片。但我不明白。 – 2012-02-02 05:54:09

+0

** [本教程可以帮助您](http://startandroiddevelopment.blogspot.in/2013/10/importing-image-from-gallery.html)** – 2013-10-24 10:15:37

回答

11

得到的回答其旋转到90。方向与EXIF格式的图像一起保存。我们必须读取每个图像数据的方向标签..

public static float rotationForImage(Context context, Uri uri) { 
     if (uri.getScheme().equals("content")) { 
     String[] projection = { Images.ImageColumns.ORIENTATION }; 
     Cursor c = context.getContentResolver().query(
       uri, projection, null, null, null); 
     if (c.moveToFirst()) { 
      return c.getInt(0); 
     } 
    } else if (uri.getScheme().equals("file")) { 
     try { 
      ExifInterface exif = new ExifInterface(uri.getPath()); 
      int rotation = (int)exifOrientationToDegrees(
        exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 
          ExifInterface.ORIENTATION_NORMAL)); 
      return rotation; 
     } catch (IOException e) { 
      Log.e(TAG, "Error checking exif", e); 
     } 
    } 
     return 0f; 
    } 

    private static float exifOrientationToDegrees(int exifOrientation) { 
    if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_90) { 
     return 90; 
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_180) { 
     return 180; 
    } else if (exifOrientation == ExifInterface.ORIENTATION_ROTATE_270) { 
     return 270; 
    } 
    return 0; 
} 
} 

旋转值可以用来校正照片的方向如下:

Matrix matrix = new Matrix(); 
float rotation = PhotoTaker.rotationForImage(context, uri); 
if (rotation != 0f) { 
     matrix.preRotate(rotation); 
} 

Bitmap resizedBitmap = Bitmap.createBitmap(
sourceBitmap, 0, 0, width, height, matrix, true); 
+0

伟大..正在寻找此.. – 2012-02-06 08:16:06

+0

@AndroSelva::) – 2012-02-06 09:27:48

+0

伟大的工作...位图resizedBitmap = Bitmap.createBitmap(sourceBitmap,0,0,sourceBitmap.getWidth(),sourceBitmap.getHeight (),matrix,true); – Underdog 2012-08-23 02:51:12

0

而将其设置为ImageView的,检查是否图象的宽度大于高度或不和如果需要的话

+0

这是不可能的,因为我们正在获取所有图像比率640X480。 – 2012-02-02 07:59:58

+0

如果它在横向,它会像480x640一样吗? – 2012-02-02 09:40:53

+1

得到答案...方向标签与每张图像一起保存。检查此链接http://mobisocial.stanford.edu/news/2011/08/rotating-images-in-android/ – 2012-02-06 04:19:19