相机捕获图像旋转到纵向 - Xamarin android [英] Camera capture image rotate to portrait - Xamarin android

查看:28
本文介绍了相机捕获图像旋转到纵向 - Xamarin android的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个 xamarin.android 项目,它有一个自定义相机预览.每当我初始化相机时,它都会以横向默认方式打开.所以我像这样从 SurfaceChanged 方法改变了方向.

I have a xamarin.android project which have a custom camera Preview. Whenever I initialize the camera , it will open as landscape default. So I changed the orientation from SurfaceChanged method like this.

private int setCameraDisplayOrientation(Activity mContext, int v)
        {
            var degrees = 0;
            var orientation =0;

            Display display = mContext.GetSystemService(Context.WindowService).JavaCast<IWindowManager>().DefaultDisplay;

            Camera.CameraInfo info = new Camera.CameraInfo();
            Camera.GetCameraInfo(v, info);
            var rotation = windowManager.DefaultDisplay.Rotation;

            DisplayMetrics dm = new DisplayMetrics();
            display.GetMetrics(dm);
            int width = dm.WidthPixels;
            int height = dm.HeightPixels;

            //Natural orientation is portrait
            if ((rotation == SurfaceOrientation.Rotation0 || rotation == SurfaceOrientation.Rotation180) && height > width ||
                (rotation == SurfaceOrientation.Rotation90 || rotation == SurfaceOrientation.Rotation270) && width > height)
            {
                switch (rotation)
                {
                    case SurfaceOrientation.Rotation0:
                        degrees = 90;
                        break;
                    case SurfaceOrientation.Rotation90:
                        degrees = 0;
                        break;
                    case SurfaceOrientation.Rotation180:
                        degrees = 270;
                        break;
                    case SurfaceOrientation.Rotation270:
                        degrees = 180;
                        break;
                }
            }
            
            return (degrees);
        }

它工作正常.它将纵向排列前后摄像头.

It works fine. It will arrange both front and back camera in portrait.

问题

当我拍摄照片时,在某些设备中,它会以横向模式存储在某些设备中,它会存储纵向.无论如何,我都希望图像处于纵向模式.为了做到这一点,我尝试获取图像的 Exif 数据并相应地将其旋转到纵向模式.但是在某些设备中,例如三星、VIVO,方向值会变为0".我不知道如何处理这个值.如果我 PreRotate 90 ,那么一些设备会解决这个问题,而另一些则会向上保存照片.

When I capture photo, in some devices, it will store in landscape mode in some devices it will store portrait. I want the image to be in portrait mode no matter what. In order do that I tried to get the Exif data of image and rotate it to portrait mode accordingly. But in some devices like samsung, VIVO the orientation value gets as "0". I don't know what to do with that value. If I PreRotate 90 , then some devices will solve this issue, while other will save the photo upwards.

**Managing Rotation**

 Android.Graphics.Bitmap loadAndResizeBitmap(string filePath)
    {

        Android.Graphics.Bitmap resizedBitmap = Android.Graphics.BitmapFactory.DecodeFile(filePath);
        ExifInterface exif = null;
        try
        {
            exif = new ExifInterface(filePath);
            string orientation = exif.GetAttribute(ExifInterface.TagOrientation);

            Android.Graphics.Matrix matrix = new Android.Graphics.Matrix();
            switch (orientation)
            {
                case "1":
                    matrix.PreRotate(-90);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                    matrix.Dispose();
                    matrix = null;
                    break;
                case "3":
                    matrix.PreRotate(180);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                    matrix.Dispose();
                    matrix = null;
                    break;
                case "4":
                    matrix.PreRotate(180);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                    matrix.Dispose();
                    matrix = null;
                    break;
                case "5":
                    matrix.PreRotate(90);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                    matrix.Dispose();
                    matrix = null;
                    break;
                case "6": // portrait
                    matrix.PreRotate(90);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                    matrix.Dispose();
                    matrix = null;
                    break;
                case "7":
                    matrix.PreRotate(-90);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                    matrix.Dispose();
                    matrix = null;
                    break;
                case "8":
                    matrix.PreRotate(-90);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                    matrix.Dispose();
                    matrix = null;
                    break;
                case "0":
                    matrix.PreRotate(-90);
                    resizedBitmap = Android.Graphics.Bitmap.CreateBitmap(resizedBitmap, 0, 0, resizedBitmap.Width, resizedBitmap.Height, matrix, false);
                    matrix.Dispose();
                    matrix = null;
                    break;
            }

            return resizedBitmap;

        }

        catch (IOException ex)
        {
            return null;
        }

    }

我从 Xamarin.Andoid 图像旋转.但不知何故,我不能中继它.会有什么问题?我应该传递 Stream 而不是文件路径吗?是不是由于表面视图旋转?无论在任何设备上,如何制作拍摄的图像肖像?任何帮助表示赞赏.

I got this Idea from Xamarin.Andoid Image rotation. But somehow I cant relay on it. What will be the problem? Should I pass Stream instead of file path? Does it due to the surfaceview rotation? How can I make the captured image portrait no matter what on any device? Any help is appreciated.

推荐答案

仅适用于 Xamarin.Android,使用依赖服务在共享项目中调用

Only for Xamarin.Android, call it in shared project using dependency service

由此获取图像的旋转角度.

Get the Angle of rotation of image by this.

public int GetImageRotation(string filePath)
        {
            try
            {
                ExifInterface ei = new ExifInterface(filePath);
                Orientation orientation = (Orientation)ei.GetAttributeInt(ExifInterface.TagOrientation, (int)Orientation.Undefined);
                switch (orientation)
                {
                    case Orientation.Rotate90:
                        return 90;
                    case Orientation.Rotate180:
                        return 180;
                    case Orientation.Rotate270:
                        return 270;
                    default:
                        return 0;
                }
            }
            catch(Exception ex)
            {
                return 0;
            }
        }

现在根据您获得的角度值旋转图像.

Now Rotate the image according to the angle value you get.

我对流而不是实际文件执行操作 &以Byte数组的形式返回.

I perform operation on stream rather than the actual file & return in the form of Byte array.

public byte[] RotateImage(System.IO.Stream imageStream, string filePath)
        {
            int rotationDegrees = GetImageRotation(filePath)
            byte[] byteArray = new byte[imageStream.Length];
            try
            {
                imageStream.Read(byteArray, 0, (int)imageStream.Length);

                Bitmap originalImage = BitmapFactory.DecodeByteArray(byteArray, 0, byteArray.Length);
                Matrix matrix = new Matrix();
                matrix.PostRotate((float)rotationDegrees);

                Bitmap rotatedBitmap = Bitmap.CreateBitmap(originalImage, 0, 0, originalImage.Width,
                    originalImage.Height, matrix, true);

                using (MemoryStream ms = new MemoryStream())
                {
                    rotatedBitmap.Compress(Bitmap.CompressFormat.Jpeg, 100, ms);
                    return ms.ToArray();
                }
            }
            catch(Exception ex)
            {
                return byteArray;
            }
        }

让我知道它是否有效.

这篇关于相机捕获图像旋转到纵向 - Xamarin android的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆