【问题标题】:how to Rotate YuvImage in xamarin android如何在 xamarin android 中旋转 YuvImage
【发布时间】:2017-11-03 10:43:42
【问题描述】:

我正在为我的 xamarin 应用程序在 android 界面中将图像的 bytearray 转换为 YuvImage,就像这样

      public KeyValuePair<string, string> CaptureImageFromBarcode(byte[] bt, int width, int height)
    {

        Java.IO.FileOutputStream outStream = null;
        Android.Graphics.YuvImage yuvimage = new Android.Graphics.YuvImage(bt, Android.Graphics.ImageFormat.Nv21, width, height,null);
        MemoryStream baos = new MemoryStream();
        yuvimage.CompressToJpeg(new Android.Graphics.Rect(0, 0, width, height), 80, baos);
        var directory = Android.OS.Environment.GetExternalStoragePublicDirectory(Android.OS.Environment.DirectoryPictures);
        string filenamefile = DateTime.Now.Ticks.ToString() + ".jpg";
        string filename = System.IO.Path.Combine(directory.AbsolutePath, filenamefile);
        outStream = new Java.IO.FileOutputStream(filename);
        outStream.Write(baos.ToArray());
        outStream.Close();
        return new KeyValuePair<string, string>(filenamefile, filename);
    }

在这里我将图像保存到磁盘。但是保存的图像向左旋转了 90 度。所以我想保存正确旋转 90 度的图像。我尝试了一些代码,但它没有给我正确的输出。

这是我尝试过的。

试过代码1:

private byte[] rotateYUV420Degree90(byte[] data, int imageWidth, int imageHeight)
    {
        byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
        // Rotate the Y luma
        int i = 0;
        for (int x = 0; x < imageWidth; x++)
        {
            for (int y = imageHeight - 1; y >= 0; y--)
            {
                yuv[i] = data[y * imageWidth + x];
                i++;
            }
        }
        // Rotate the U and V color components 
        i = imageWidth * imageHeight * 3 / 2 - 1;
        for (int x = imageWidth - 1; x > 0; x = x - 2)
        {
            for (int y = 0; y < imageHeight / 2; y++)
            {
                yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x];
                i--;
                yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + (x - 1)];
                i--;
            }
        }
        return yuv;
    }

尝试代码 2:

   public static byte[] rotateNV21(byte[] input, byte[] output, int width, int height, int rotation)
    {
        Boolean swap = (rotation == 90 || rotation == 270);
        Boolean yflip = (rotation == 90 || rotation == 180);
        Boolean xflip = (rotation == 270 || rotation == 180);
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {
                int xo = x, yo = y;
                int w = width, h = height;
                int xi = xo, yi = yo;
                if (swap)
                {
                    xi = w * yo / h;
                    yi = h * xo / w;
                }
                if (yflip)
                {
                    yi = h - yi - 1;
                }
                if (xflip)
                {
                    xi = w - xi - 1;
                }
                output[w * yo + xo] = input[w * yi + xi];
                int fs = w * h;
                int qs = (fs >> 2);
                xi = (xi >> 1);
                yi = (yi >> 1);
                xo = (xo >> 1);
                yo = (yo >> 1);
                w = (w >> 1);
                h = (h >> 1);
                // adjust for interleave here
                int ui = fs + (w * yi + xi) * 2;
                int uo = fs + (w * yo + xo) * 2;
                // and here
                int vi = ui + 1;
                int vo = uo + 1;
                output[uo] = input[ui];
                output[vo] = input[vi];
               // return output;
            }
        }
        return output;
    }

尝试过代码 3:

 public static void rotateY12toYUV420(byte[] input, byte[] output, int width, int height, int rotation)
    {
        Boolean swap = (rotation == 90 || rotation == 270);
        Boolean flip = (rotation == 90 || rotation == 180);
        for (int x = 0; x < width; x++)
        {
            for (int y = 0; y < height; y++)
            {


                int xo = x, yo = y;
                int w = width, h = height;
                int xi = xo, yi = yo;
                if (swap)
                {
                    xi = w * yo / h;
                    yi = h * xo / w;
                }
                if (flip)
                {
                    xi = w - xi - 1;
                    yi = h - yi - 1;
                }
                output[w * yo + xo] = input[w * yi + xi];
                int fs = w * h;
                int qs = (fs >> 2);
                xi = (xi >> 1);
                yi = (yi >> 1);
                xo = (xo >> 1);
                yo = (yo >> 1);
                w = (w >> 1);
                int ui = fs + w * yi + xi;
                int uo = fs + w * yo + xo;
                int vi = qs + ui;
                int vo = qs + uo;
                output[uo] = input[vi];
                output[vo] = input[ui];
            }
        }
    }

尝试过代码 4:

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

请帮忙。 谢谢。

【问题讨论】:

  • 你为什么要这样做。您可以通过旋转捕获 jpeg 而不是转换为 YUV。如果它有效,那么它是简单的方法
  • 实际上我得到的图像 (byte[] bt) 不是来自相机。我是从条形码扫描仪 dll 结果中得到的。所以它默认是旋转的。
  • 好的,您使用哪个库进行条码扫描。那个库不提供旋转功能?
  • zxing.net.mobile lib m 用于扫描
  • 不...为什么???????

标签: android image rotation xamarin.forms


【解决方案1】:
Android.Graphics.Bitmap bmp = Android.Graphics.BitmapFactory.DecodeByteArray(imageData, 0, imageData.Length);
            Android.Graphics.Matrix matrix = new Android.Graphics.Matrix();
            matrix.PostRotate(90);
            bmp = Android.Graphics.Bitmap.CreateBitmap(bmp, 0, 0, bmp.Width, bmp.Height, matrix, true);

            MemoryStream ms = new MemoryStream();
            bmp.Compress(Android.Graphics.Bitmap.CompressFormat.Jpeg, 100, ms);
            File.WriteAllBytes(filePath, ms.ToArray());

这段代码对我有用。

【讨论】:

【解决方案2】:

请看看这个

https://blog.xamarin.com/barcode-scanning-made-easy-with-zxing-net-for-xamarin-forms/

这提供了对旋转的支持。

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-09-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-07-11
    • 2011-07-20
    相关资源
    最近更新 更多