查看基于 url android 中的 EXIF 数据的图片

Viewing picture based on EXIF data from url in android

我正在尝试通过以下代码在 android 应用程序中查看来自 url 的图像:

img = (ImageView) view.findViewById(R.id.img);

new LoadImage().execute("http://localhost" + file_name);

它工作得很好,但它忽略了图像的 EXIF 数据,所以我的图像被旋转了。如何根据 EXIF 数据查看图像?

调用 fixOrientation 来修复你的图像方向

public static int getExifRotation(String imgPath) {
    try {
        ExifInterface exif = new ExifInterface(imgPath);
        String rotationAmount = exif
                .getAttribute(ExifInterface.TAG_ORIENTATION);
        if (!TextUtils.isEmpty(rotationAmount)) {
            int rotationParam = Integer.parseInt(rotationAmount);
            switch (rotationParam) {
                case ExifInterface.ORIENTATION_NORMAL:
                    return 0;
                case ExifInterface.ORIENTATION_ROTATE_90:
                    return 90;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    return 180;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    return 270;
                default:
                    return 0;
            }
        } else {
            return 0;
        }
    } catch (Exception ex) {
        return 0;
    }
}

public static Bitmap fixOrientation(String filePath, Bitmap bm) {
    int orientation = getExifRotation(filePath);
    if (orientation == 0 || orientation % 360 == 0) {
        //it is already right orientation, no need to rotate
        return bm;
    }
    Matrix matrix = new Matrix();
    matrix.postRotate(orientation);
    return Bitmap.createBitmap(bm, 0, 0, bm.getWidth(), bm.getHeight(),
            matrix, true);
}

我建议您使用像 Glide 或 Fresco 这样的现代图像加载器,而不是直接使用 AsyncTask 处理图像。