Android图库导入

发布于 2025-01-02 06:37:11 字数 400 浏览 3 评论 0原文

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

private void addImageFromGallery() {

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

}

I am using gallery picker for picking an image from gallery. The photos taken by the camera in portrait mode is shown in the gallery as straight. But when I import the photos, i get the photo as rotated (landscape). Only gallery is showing this picture as straight. How to manage this problem? I want all photos as its actual orientation.
Thanks in advance

private void addImageFromGallery() {

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

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

疾风者 2025-01-09 06:37:11

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

Got the answer. The orientation is saved with the image in EXIF format. We have to read the Orientation tag of the data for each image..

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

The rotation value can be used to correct a photo’s orientation as follows:

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);
情话已封尘 2025-01-09 06:37:11

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

while setting it to the imageview, check if the width of the image is greater than height or not and rotate it to 90 if needed

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文