简体   繁体   English

Xamarin Android 无法从旋转图像中读取 EXIF 数据以检查图像是否旋转

[英]Xamarin Android can't read EXIF data from the rotated image to check whether the image rotated or not

Description描述

To check whether the image is rotated or not by reading the EXIF meta data based on the orientation rotate then create the new bitmap and set to ImageView control.要通过基于方向旋转读取 EXIF 元数据来检查图像是否旋转,然后创建新的 bitmap 并设置为 ImageView 控件。

Actual Image EXIF data:实际图像 EXIF 数据: 在此处输入图像描述

[C#] [C#]

    protected override void OnCreate(Bundle savedInstanceState)
        {
            base.OnCreate(savedInstanceState);
            Xamarin.Essentials.Platform.Init(this, savedInstanceState);
            ImageView image = new ImageView(this);
            var bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.test);
            ExifInterface originalMetadata;
            using (var stream = new MemoryStream())
            {
                bitmap.Compress(Bitmap.CompressFormat.Jpeg, 0, stream);
                originalMetadata = new ExifInterface(stream);
            }

            int orientation = GetRotation(originalMetadata);

            Matrix matrix = new Matrix();
            if (orientation != 0)
            {
                matrix.PreRotate(orientation);
                bitmap = Bitmap.CreateBitmap(bitmap, 0, 0, bitmap.Width, bitmap.Height, matrix, true);
            }
            image.SetImageBitmap(bitmap);

            // Set our view from the "main" layout resource
            SetContentView(Resource.Layout.activity_main);
        }

Expected Behavior预期行为

To read the rotated image ExifData to get the orientation.读取旋转图像 ExifData 以获取方向。 Based on the orientation rotate then create the new bitmap and set to ImageView control.根据方向旋转,然后创建新的 bitmap 并设置为 ImageView 控件。

Actual Behavior实际行为

Getting the ExifData orientation value as 0.将 ExifData 方向值设为 0。

Basic Information基本信息

Tested device are Huawei PRA, Infinix Pro测试设备为Huawei PRA、Infinix Pro

Environment环境

Visual Studio 2019视觉工作室 2019

Screenshots截图

加载的图像 图片

Sample Link示例链接

ImageDemo.zip ImageDemo.zip

Is there any solution to resolve this problem?有什么办法可以解决这个问题吗?

var bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.test); var bitmap = BitmapFactory.DecodeResource(Resources, Resource.Drawable.test);

Now you have a bitmap.现在你有一个 bitmap。

But bitmaps do not contain an exif header or orientation information.但位图不包含 exif header 或方向信息。

You compress that bitmap to a stream and then let an Exifinterface read from that stream.您将 bitmap 压缩为 stream,然后让 Exifinterface 从该 stream 读取。

bitmap.Compress(Bitmap.CompressFormat.Jpeg, 0, stream); bitmap.Compress(Bitmap.CompressFormat.Jpeg, 0, 流);

originalMetadata = new ExifInterface(stream); originalMetadata = new ExifInterface(stream);

But as the bitmap did not contain exif info or orientation info the stream does not have them too.但是由于 bitmap 不包含 exif 信息或方向信息,因此 stream 也没有它们。

So no valid exif info and no orientation information.所以没有有效的 exif 信息和方向信息。 Nothing.没有什么。

Instead you should read exif info from the original resource file.相反,您应该从原始资源文件中读取 exif 信息。

First,as blackapps said above, after you decode the picture for Bitmap,the Exif message will be lost.首先,正如上面blackapps所说,在您将图片解码为Bitmap后,Exif消息将丢失。

Second, Resources/ in android usually refers to resources.二、android中的Resources/通常指资源。 ExifInterface only loads files off of the filesystem, not resources. ExifInterface只从文件系统中加载文件,而不是资源。 After all, it would be completely pointless for a developer to want to use ExifInterface on a resource.毕竟,开发人员想要在资源上使用ExifInterface是完全没有意义的。 Resources are read-only, so you cannot use ExifInterface to modify EXIF headers in a resource.资源是只读的,因此您不能使用ExifInterface修改资源中的 EXIF 标头。 And rather than wasting users' time, CPU, and battery reading in a read-only image file to extract metadata, developers would do that ahead of time, putting that data somewhere else.与其浪费用户的时间、CPU 和电池读取只读图像文件来提取元数据,开发人员会提前这样做,将这些数据放在其他地方。

So you could move the test.jpg into Assets folder,and then use your method to get its EXIF data:因此,您可以将test.jpg移至Assets文件夹,然后使用您的方法获取其 EXIF 数据:

var stream = Assets.Open("test.jpg");
originalMetadata = new ExifInterface(stream);
int orientation = GetRotation(originalMetadata);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM