【问题标题】:Image gets rotate though the orientation is 0 using exif while selecting from gallery图像在从图库中选择时使用 exif 旋转,尽管方向为 0
【发布时间】:2016-11-10 08:48:04
【问题描述】:

我正在从图库中选择一张图片并将其显示在图片视图中。图像被选中,但在三星手机上,它有旋转图像的问题,为了解决我正在检查图像是否旋转使用 EXIF 接口,如果旋转改变它的角度。

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

当我调试图像的方向为 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 - 像这样使用选定的文件?我试过了,但这在小米设备上不起作用,所以我更改了代码。其他设备还有其他方法吗?

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

【问题讨论】:

  • “rotatedBitmap = rotateImage(bitmap, 90);”中的“位图”在哪里从哪里来?
  • 该代码不适用于小米。行。但它适用于三星吗?你不清楚。
  • 在三星上工作,我可以直接看到图像。但是在小米中,光标返回 null 所以我无法选择图像。 @greenapps
  • 如前所述:您应该使用不同的ExifInterface 类,它可以从文件的输入流中读取文件内容。 Uri uri = (Uri)data.getData();。因此,您可以使用 InputStream is = getContentResolver().openInputStream(uri); 阅读 stackoverflow,正如 CommonsWare 在您可以使用哪个类之前所说的那样。
  • 我没明白。你能举个例子吗? @greenapps

标签: android image bitmap rotation exif


【解决方案1】:
loadImageFromFile(destination.getAbsolutePath());

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

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

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

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

【讨论】:

  • 我不知道,我今天发布了这个问题。但我也在为相机做同样的事情,它也在为它工作。那么如何解决三星设备的这个问题。 @greenapps
  • 发布的代码一开始就无法使用。不在单个设备上。因为位图不包含 exif 信息。
  • 如果您想使用画廊应用程序选择的 jpg 文件中的 exif 信息确定方向,请直接使用该文件。不要先将文件转换为位图,然后再重新保存到没有 exif 信息的其他文件。
  • 如果您需要在图像视图中显示位图并且想要旋转该位图并需要 exif 信息,则使用原始文件从中提取 exif 信息。不是来自您从该位图创建的文件。
  • I dont know, I have posted the question todday.。好吧,我们可以称之为谎言。 stackoverflow.com/questions/40172441/…你挣扎了这么久,确实有不回答问题就离开现场的习惯。
猜你喜欢
  • 2012-11-10
  • 1970-01-01
  • 2013-10-27
  • 2016-07-13
  • 1970-01-01
  • 2014-01-03
  • 2012-10-23
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多