java - Get pixels matrix from YUV byte array -
i need pixels matrix yuv byte[] array obtain camera preview in onpreviewframe function. need pixels in form of matrix in order implement further image processing. i'm doing in following way:
- byte[] array yuv image
- yuv image jpeg.
- jpeg bitmap.
- bitmap pixels array.
- pixels array pixels matrix.
is there optimal way achieving same results? currently, implementation follow.
@override public void onpreviewframe(byte[] data, camera camera) { try { final yuvimage image = new yuvimage(data, imageformat.nv21, size.width, size.height, null); final file file = new file(environment.getexternalstoragedirectory() .getpath() + "/" + string.valueof(br) + "out.jpg"); final fileoutputstream file_o_s = new fileoutputstream(file); thread comp_thread = new thread(new runnable() { @override public void run() { try { image.compresstojpeg( new rect(0, 0, size.width, size.height), 40, file_o_s); file_o_s.flush(); file_o_s.close(); bitmap bmap = bitmapfactory.decodefile(file.getpath()); int[] pixels_array = new int[bmap.getheight()*bmap.getwidth()]; bmap.getpixels(pixels_array, 0, bmap.getwidth(), 0, 0, bmap.getwidth(), bmap.getheight()); int[][] pixels_matrix = new int[bmap.getheight()][bmap.getwidth()]; int start = 0; (int r = 0; r < bmap.getheight(); r++) { int l = math.min(bmap.getwidth(), pixels_array.length - start); pixels_matrix[r] = java.util.arrays.copyofrange(pixels_array, start, start + l); start += l; } } catch (ioexception e) { e.printstacktrace(); } } }); comp_thread.start(); } catch (exception e) { log.e(tag, e.getmessage()); } }
just specify camera preview must provide rgb images
i.e. camera.parameters.setpreviewformat(imageformat.rgb_565);
Comments
Post a Comment