如何检索缩略图的旋转角度

6
我正在创建一款展示所有图片的“画廊”应用程序,以网格形式显示。

问题在于:某些图片显示方向不正确。

以下是检索缩略图的代码:

final String[] projection = { MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Thumbnails.IMAGE_ID };
//query the thumbnails provider
Cursor thumbnailsCursor = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null,
            null, null);
if (thumbnailsCursor.moveToFirst()) {
        do {
            //get the thumbnail path
            fullPath = thumbnailsCursor.getString(fullPathColumnIndex);
            thumbnailUri = Uri.parse(fullPath);
            //add the uri to the list
            thumbnailsList.add(thumbnailUri);
}  while (thumbnailsCursor.moveToNext());
thumbnailsCursor.close();

BaseAdaptergetView()方法中,我使用了Picasso图像加载库来显示缩略图,但有时方向不正确。

Picasso.with(context).load(new File(photoItem.thumbnail.getPath())).noFade().into(holder.photoImageView);

我曾试图查询真实的照片数据并检索其方向,但过程太慢(需要几秒钟),而且显示的图片太大。

问题解决了吗? - Sanket Kachhela
5个回答

13

通过图像的ID,您可以查询MediaStore以获取原始图像的路径,然后从中获取方向的exif。

这个过程非常快,通常只需要几毫秒。

 Cursor cursor =
          CustomApplication
              .getContext()
              .getContentResolver()
              .query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                  new String[] {MediaStore.Images.Media.DATA}, MediaStore.Images.Media._ID + "=?",
                  new String[] {"" + PHOTO_ID}, null);
      if (cursor != null && cursor.getCount() > 0) {
        cursor.moveToFirst();
        String fullPath = cursor.getString(0);
        cursor.close();
        ExifInterface exif = new ExifInterface(fullPath);
        int exifOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, ExifInterface.ORIENTATION_NORMAL);
        //ROTATE AND FLIP YOUR THUMBNAIL AS NEEDED BASED ON exifOrientation
      }

2

这里是正确的解决方案,您可以从MediaStore查询方向数据,因此使用MediaStore.Images.Media.ORIENTATION获取方向度数。

 final String[] projection = { MediaStore.Images.Thumbnails.DATA, MediaStore.Images.Thumbnails.IMAGE_ID, MediaStore.Images.Media.ORIENTATION };

 Cursor thumbnailsCursor = context.getContentResolver().query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null);
if (thumbnailsCursor.moveToFirst()) {
do {
  int orientationColumnIndex    =   thumbnailsCursor.getColumnIndex(MediaStore.Images.Media.ORIENTATION);
  int orientation = cur.getInt(orientationColumnIndex);
//do what you want to do with orientation value
}while (thumbnailsCursor.moveToNext());
 thumbnailsCursor.close();

将会给你提供方向的角度。


1
你可以如下使用ExifInterface类:
int originalOrientation = ExifInterface.ORIENTATION_NORMAL;

try {
    ExifInterface exif = new ExifInterface(photoItem.thumbnail.getPath().toString());
    originalOrientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
} catch (IOException e) {
    e.printStackTrace();
}

然后您可以检查它是否被旋转,并进行任何所需的逻辑(例如向Picasso请求传递旋转角度)。
if (originalOrientation == ExifInterface.ORIENTATION_ROTATE_90 || originalOrientation == ExifInterface.ORIENTATION_ROTATE_270) {
    //do something
}

这是我使用的方法,我从未注意到任何性能影响。


1
我发现您可以在picasso库中为加载任务应用自定义转换。这里有solution的参考。在那里,transform方法使用MATRIX,与其他答案相同。我还没有检查过。

-1
public static float getOrientationValue(String location) {
    ExifInterface exif = null;
    try {
        exif = new ExifInterface(new File(location).getAbsolutePath());

    } catch (IOException e) {
        e.printStackTrace();
    }
    int orientation = exif.getAttributeInt(ExifInterface.TAG_ORIENTATION,
        ExifInterface.ORIENTATION_NORMAL);
    int rotate = 0;
    switch (orientation) {
    case ExifInterface.ORIENTATION_ROTATE_270:
        rotate += 90;
    case ExifInterface.ORIENTATION_ROTATE_180:
        rotate += 90;
    case ExifInterface.ORIENTATION_ROTATE_90:
        rotate += 90;
    }
    return rotate;
    }

或者

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

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接