android - 从图库中选择时,使用 exif 旋转图像,尽管方向为 0

标签 android image bitmap rotation exif

我正在从图库中选择一张图像并将其显示在 ImageView 上。图像已被选中,但在三星手机上存在旋转图像的问题,因此为了解决这个问题,我正在使用 EXIF 接口(interface)检查图像是否旋转,如果旋转则更改其角度。

但这对于某些图像不起作用。有些图像我可以直接看到,但有些图像如果是直的,那么它们也会旋转。

当我调试图像的方向为 0 时,它处于默认情况并将正常位图应用于旋转位图。我看到的图像仍然是旋转的。不明白为什么会发生这种情况..

private void onSelectFromGalleryResult(Intent data) {

    Bitmap bm=null;
    if (data != null) {
        try {
            bm = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    ByteArrayOutputStream bytes = new ByteArrayOutputStream();

    bm = Bitmap.createScaledBitmap(bm,512,512, true);

    bm.compress(Bitmap.CompressFormat.PNG,100, bytes);
    File destination = new File(Environment.getExternalStorageDirectory(),
            System.currentTimeMillis() + ".png");
    FileOutputStream fo;
    try {
        destination.createNewFile();
        fo = new FileOutputStream(destination);
        fo.write(bytes.toByteArray());
        fo.close();
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

    loadImageFromFile(destination.getAbsolutePath());

}



public void loadImageFromFile(String imageFile){

    try {
        ExifInterface ei = new ExifInterface(imageFile);
        int orientation = ei.getAttributeInt(ExifInterface.TAG_ORIENTATION,
                ExifInterface.ORIENTATION_UNDEFINED);

            Bitmap bitmap = BitmapFactory.decodeFile(imageFile);

            Bitmap rotatedBitmap = null;
            Log.e("orientation",String.valueOf(orientation)+" check");
            switch (orientation) {
                case ExifInterface.ORIENTATION_ROTATE_90:
                    rotatedBitmap = rotateImage(bitmap, 90);
                    Log.e("orientation",String.valueOf(orientation)+" check");
                    break;
                case ExifInterface.ORIENTATION_ROTATE_180:
                    rotatedBitmap = rotateImage(bitmap, 180);
                    Log.e("orientation",String.valueOf(orientation)+" check");
                    break;
                case ExifInterface.ORIENTATION_ROTATE_270:
                    rotatedBitmap = rotateImage(bitmap, 270);
                    Log.e("orientation",String.valueOf(orientation)+" check");
                    break;
                case ExifInterface.ORIENTATION_NORMAL:
                    rotatedBitmap = bitmap;
                    Log.e("orientation",String.valueOf(orientation)+" check");
                    break;

                default:
                    rotatedBitmap = bitmap;
                    Log.e("orientation",String.valueOf(orientation)+" check");
                    break;
            }

        if(rotatedBitmap != null)
        {
            profile_image.setImageBitmap(rotatedBitmap);
            selectedBitmap = rotatedBitmap;

            ByteArrayOutputStream stream = new ByteArrayOutputStream();
            selectedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream); //replace 100 with desired quality percentage.
            byte[] byteArray = stream.toByteArray();

                File tempFile = File.createTempFile("temp",null, getCacheDir());
                FileOutputStream fos = new FileOutputStream(tempFile);
                fos.write(byteArray);

                mProfileImage = tempFile;
        }

    }
    catch (IOException ex) {
      //  UiUtils.showAlert(getString(R.string.error),NewGroupAcvitity.this);
    }
}

public static Bitmap rotateImage(Bitmap source, float angle) {
    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    return Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix,
            true);
}

编辑:

-

    @SuppressWarnings("deprecation")
-    private void onSelectFromGalleryResult(Intent data) {
-
-        Uri uri = (Uri)data.getData();
-        String[] filePathColumn = { MediaStore.Images.Media.DATA };
-        Cursor cursor = getContentResolver().query(uri,filePathColumn, null, null, null);
-        if(cursor != null) {
-            cursor.moveToFirst();
-            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
-            String picturePath = cursor.getString(columnIndex);
-            cursor.close();
-
-            loadImageFromFile(picturePath);
-        }
-    }

@greenapps - 像这样使用选定的文件吗?我尝试了这个,但这在小米设备上不起作用,所以我更改了代码。其他设备还有其他方法吗?

这里出了什么问题?有人可以帮忙吗?谢谢。

最佳答案

loadImageFromFile(destination.getAbsolutePath());

您尝试加载的图像最初来自您压缩为 jpg 并保存到文件的位图。 destination 是该文件的 File 对象。

位图不包含 exif 信息。因此你的 jpg 文件也不会包含 exif。

所以在上面使用ExifInterface是没有用的。

Sid 我以前看过这段代码。并讲述了同样的故事。也许我告诉的甚至是你。

关于android - 从图库中选择时,使用 exif 旋转图像,尽管方向为 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40523315/

相关文章:

android - 是否可以为 Xcode Target 等 Android 项目创建多个目标

ios - 从 Swift 应用程序上传图像文件

html - IMG 标签上的奇怪边框

Android Canvas 使用位图撤消和重做

android - LibGDX - 来自自定义类的 Sprite 未显示

php - C2DM实现PHP代码

android - 处理绘图/绘画/保存多个位图

java - Android 位图 - 内存不足

php - android中发送json对象数据到远程mysql数据库

android - 带有图像的 UrlEncodedFormEntity 时内存不足