2017-06-02 37 views
2

我正在轉換字節組圖像到YuvImage在Android界面我xamarin廣告應用程式這樣如何xamarin的Android

 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度左旋YuvImage所以我想保存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); 
    } 

請幫忙。 謝謝。

+0

你爲什麼想這樣做。您可以捕捉帶有旋轉的jpeg,而不是轉換成YUV。如果它的工作比它容易的方式 – DreamCoder

+0

實際上這裏圖像(字節[] bt)我得到不是從camera.that我從條形碼掃描儀DLL的結果。所以它默認是旋轉的。 –

+0

好吧,你用於條碼掃描的庫。該庫不提供旋轉功能? – DreamCoder

回答

2
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()); 

此代碼適用於我。

+0

接受答案:https://stackoverflow.blog/2011/07/01/its-ok-to-ask-and-answer-your-own-questions/ –