【问题标题】:Android Gallery ImportAndroid 图库导入
【发布时间】:2012-02-24 19:53:24
【问题描述】:

我正在使用图库选择器从图库中选择图像。相机在纵向模式下拍摄的照片在图库中显示为直线。但是当我导入照片时,我得到的照片是旋转的(风景)。只有画廊显示这张照片是直的。如何管理这个问题?我希望所有照片都作为它的实际方向。 提前致谢

private void addImageFromGallery() {

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

}

【问题讨论】:

标签: android android-camera android-gallery


【解决方案1】:

得到了答案。方向以 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);

【讨论】:

  • 工作得很好...位图 resizedBitmap = Bitmap.createBitmap(sourceBitmap, 0, 0, sourceBitmap.getWidth(), sourceBitmap.getHeight(), matrix, true);
  • 为什么rotationForImage会返回一个浮点数?一个 int 还不够吗?
【解决方案2】:

将其设置为imageview时,检查图像的宽度是否大于高度,如果需要将其旋转到90

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2014-09-14
  • 2011-01-15
  • 2014-02-02
  • 2014-02-24
  • 2017-12-25
  • 2014-10-03
  • 1970-01-01
  • 2013-08-11
相关资源
最近更新 更多