Camara 自訂預覽效果 PreviewCallBack 的使用

來源:互聯網
上載者:User

參考:http://www.bangchui.org/read.php?tid=4203

Camera.Parameters.setPreviewFormat(PixelFormat.JPEG);這個方法發生作用的前提必須是手機camara能夠支援,否則是沒有什麼效果的。
通過List list = Camera.Parameters.getSupportedPreviewFormats();這個方法來判斷手機支援哪種類型的PixelFormat,一般情況下是 NV21 即值為17。 BitmapFactory.decodeByteArray這個方法是不能直接解析NV21格式的byte[],返回的是null,所以需要對NV21格式的byte[]進行轉換。
最後:sdk 2.2提供了類YuvImage 通過方法compressToJpeg進行轉換,但是sdk2.1沒有現成的api進行此操作(或者是說我沒找到,找到了給說一下),所以需要自己進行格式的轉換。

轉換函式:

public void decodeYUV420SP(int[] rgb, byte[] yuv420sp, 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) yuv420sp[yp])) - 16;if (y < 0)y = 0;if ((i & 1) == 0) {v = (0xff & yuv420sp[uvp++]) - 128;u = (0xff & yuv420sp[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);}}}

轉換成bitmap:

if(format == ImageFormat.NV21 || format == ImageFormat.YUY2){ if((version >= 8)){  YuvImage yuv_image = new YuvImage(data, format, 320, 240, null);                 // Convert YuV to Jpeg                 Rect rect = new Rect(0, 0, 320, 240);                 ByteArrayOutputStream output_stream = new ByteArrayOutputStream();                 yuv_image.compressToJpeg(rect, 100, output_stream);                 // Convert from Jpeg to Bitmap                 bitmap = BitmapFactory.decodeByteArray(output_stream.toByteArray(), 0, output_stream.size()); }else{  decodeYUV420SP(bufInt, data, 320, 240); bitmap = Bitmap.createBitmap(bufInt, 320, 240, Bitmap.Config.RGB_565); }}else if(format == PixelFormat.RGB_565 || format == ImageFormat.JPEG){bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);}

ps:bufInt是個int類型數組,長度和回呼函數:

onPreviewFrame(byte[] data, Camera camera)
data數組一樣。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.