2017-03-14 145 views
0

onPreviewFrame使用android.hardware.Camera,如果我使用默認的NV21格式,我可以使用YuvImage壓縮爲jpeg格式,這很好。如果我嘗試使用setPreviewFormat(ImageFormat.YV12)更改格式,則它不再起作用,因爲YuvImage不支持YV12格式。我發現只有一個解決方案將Bitmap轉換爲YV12,但我想做相反的事情,並從這些字節中獲取一個jpeg。有沒有圖書館這樣做?轉換YV12(yuv420p)爲JPEG/Bitamp

回答

0

如果YUV420爲JPEG是你在找什麼,然後,

@Override 
    public void onPreviewFrame(byte[] data, Camera camera) { 
     // TODO Auto-generated method stub 
     ByteArrayOutputStream out= new ByteArrayOutputStream(); 
     decodeYUV420(pixels, data, previewSize.width, previewSize.height); 
     mBitmap = Bitmap.createBitmap(pixels, previewSize.width, previewSize.height,Config.ARGB_8888); 
     mBitmap.compress(CompressFormat.JPEG , 25, out); 
     ....... 

何地,decodeYUV420方法去如下:

public void decodeYUV420(int[] rgb, byte[] yuv420, int width, int height) { 
     final int frameSize = width * height; 

     for (int j = 0, yp = 0; j < height; j++) { 
      int uvp = frameSize + (j >> 1) * width, u = 0, v = 0; 
      for (int i = 0; i < width; i++, yp++) { 
       int y = (0xff & ((int) yuv420[yp])) - 16; 
       if (y < 0) y = 0; 
       if ((i & 1) == 0) { 
        v = (0xff & yuv420[uvp++]) - 128; 
        u = (0xff & yuv420[uvp++]) - 128; 
       } 

       int y1192 = 1192 * y; 
       int r = (y1192 + 1634 * v); 
       int g = (y1192 - 833 * v - 400 * u); 
       int b = (y1192 + 2066 * u); 

       if (r < 0) r = 0; else if (r > 262143) r = 262143; 
       if (g < 0) g = 0; else if (g > 262143) g = 262143; 
       if (b < 0) b = 0; else if (b > 262143) b = 262143; 

       rgb[yp] = 0xff000000 | ((r << 6) & 0xff0000) | ((g >> 2) & 0xff00) | ((b >> 10) & 0xff); 
      } 
} 

我創建了一個類似的項目,這對GitHub的檢查here

和代碼執行:here

是的,它的工作原理!

+0

您確定這是用於'yuv420p'而不是'yuv420sp'吧? – Guillaume

+0

等。讓我檢查一下。 – MadScientist

+0

看起來更像yuv420sp。因爲有一個yuv陣列。讓我檢查一下如何將yuv420p採樣到yuv20sp – MadScientist