首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >ExifInterface总是返回旋转0

ExifInterface总是返回旋转0
EN

Stack Overflow用户
提问于 2018-02-20 13:14:48
回答 1查看 1.5K关注 0票数 0

--我从一个定制的相机中得到了一张旋转的图片,我保存这个图像以获得在ExifInterface上使用它的路径,并使它符合我在ExifInterface上使用的正确方向,但是它总是返回值为0的值,有什么问题呢?(我运行一个模拟器)

谢谢

这里是我的OnPictureTaken方法:

代码语言:javascript
复制
public async void OnPictureTaken(byte[] data, Android.Hardware.Camera camera)
    {
        var targetWidth = 1080;
        var cameraParams = camera.GetParameters();
        var ratio = ((decimal)cameraParams.PictureSize.Height) / cameraParams.PictureSize.Width;

        var w = cameraParams.PictureSize.Width >= targetWidth
            ? targetWidth
            : cameraParams.PictureSize.Width;

        var h = cameraParams.PictureSize.Width >= targetWidth
            ? (int)Math.Floor(targetWidth * ratio)
            : cameraParams.PictureSize.Height;

        Java.IO.File sdDir = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
        Java.IO.File pictureFileDir = new Java.IO.File(sdDir, "RDCCameraImages");

        if (!pictureFileDir.Exists() && !pictureFileDir.Mkdirs())
        {
            return;
        }
        String photoFile = "Picture_"   + "aaa.jpg";
        String imageFilePath = pictureFileDir.Path + Java.IO.File.Separator + photoFile;
        Java.IO.File pictureFile = new Java.IO.File(imageFilePath);

        FileOutputStream fos = new FileOutputStream(pictureFile);
        fos.Write(data);
        fos.Close();

        ExifInterface exif;

        ExifInterface ei = new ExifInterface(pictureFile.AbsolutePath);
        int orientation = ei.GetAttributeInt(ExifInterface.TagOrientation, (int)Android.Media.Orientation.Normal);//return 0

        var imageStream = new System.IO.MemoryStream();
        using (var bm = BitmapFactory.DecodeByteArray(data, 0, data.Length))
        using (var scaled = Bitmap.CreateScaledBitmap(bm, w, h, false))
        {
            Bitmap rotatedBitmap = null;
            switch (orientation)
            {

                case (int)Android.Media.Orientation.Rotate90:
                    rotatedBitmap = rotateImage(scaled, 90);
                    break;

                case (int)Android.Media.Orientation.Rotate180:
                    rotatedBitmap = rotateImage(scaled, 180);
                    break;

                case (int)Android.Media.Orientation.Rotate270:
                    rotatedBitmap = rotateImage(scaled, 270);
                    break;

                case (int)Android.Media.Orientation.Normal:
                    rotatedBitmap = scaled;
                    break;
                default:
                    rotatedBitmap = scaled;
                    break;
            }
            await scaled.CompressAsync(Bitmap.CompressFormat.Jpeg, 90, imageStream);
            await imageStream.FlushAsync();
        }
   }  
   public static Bitmap rotateImage(Bitmap source, float angle)
    {
        Matrix matrix = new Matrix();
        matrix.PostRotate(angle);
        return Bitmap.CreateBitmap(source, 0, 0, source.Width, source.Height,
                                   matrix, true);
    } 
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-02-21 08:24:48

ExifInterface,为了使它成为我在ExifInterface上使用的正确方向,但是它总是以0的值返回,会有什么问题呢?

ExifInterface不是那样工作的。在您的代码中,您刚刚创建了一个新的图片,并保存在您的设备中,如果我正确理解它,您希望ExifInterface自动检测方向。嗯,没有那么聪明,ExifInterface只能使用ExifInterface.SetAttribute获取我们以前保存到该映像中的信息。

代码语言:javascript
复制
ExifInterface ei = new ExifInterface(pictureFile.AbsolutePath);
//set the attribute
ei.SetAttribute(ExifInterface.TagOrientation, "90");
//save the attributes
ei.SaveAttributes();

//For Test I created a totally new ExifInterface
ExifInterface ei2 = new ExifInterface(pictureFile.AbsolutePath);
//we get the Information of previously saved orientation
var orientationInfo = ei2.GetAttribute(ExifInterface.TagOrientation);
//orientationInfo=90

如何获得相机的方位?

您可以使用CameraInfo获取当前摄像机的方向:

代码语言:javascript
复制
int cameraId=0;
CameraInfo cameraInfo=null;
for (int i = 0; i < Camera.NumberOfCameras; i++)
{
    CameraInfo info = new CameraInfo();
    Camera.GetCameraInfo(i, info);
    if (info.Facing == CameraInfo.CameraFacingBack)
    {
                cameraId = i;
                cameraInfo = info;
    }
}
//Open Camera
mCamera = Camera.Open(cameraId);
if (cameraInfo != null)
{
     var orientation = cameraInfo.Orientation;
}
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/48886165

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档