Android Camera - How can i take a specific “rectangle” from the byte array?

て烟熏妆下的殇ゞ 提交于 2019-11-30 07:06:19

It took me some time to figure out that the code should look like this:

int[] pixels = new int[100*100];//the size of the array is the dimensions of the sub-photo
        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        Bitmap bitmap = BitmapFactory.decodeByteArray(data , 0, data.length);
        bitmap.getPixels(pixels, 0, 100, 350, 50, 100, 100);//the stride value is (in my case) the width value
        bitmap = Bitmap.createBitmap(pixels, 0, 100, 100, 100, Config.ARGB_8888);//ARGB_8888 is a good quality configuration
        bitmap.compress(CompressFormat.JPEG, 100, bos);//100 is the best quality possibe
        byte[] square = bos.toByteArray();
bnenning

The camera preview data comes in YUV format (specifically ImageFormat.NV21), which BitmapFactory doesn't support directly. You can use YuvImage to convert it to a Bitmap as described here. The only difference is that you'll want to pass a Rect corresponding to the square that you want when calling YuvImage.compressToJpeg.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!