AndroidUSBCamera icon indicating copy to clipboard operation
AndroidUSBCamera copied to clipboard

face turns blue when converting to bitmap

Open necatiCelik opened this issue 3 years ago • 1 comments

Hi, On the onPreviewResult method, //I am call yuvToBitmap method, Bitmap btmpFace= yuvToBitmap(data, width, height);

public Bitmap yuvToBitmap(byte[] data, int width, int height) { int frameSize = width * height; int[] rgba = new int[frameSize]; for (int i = 0; i < height; i++) for (int j = 0; j < width; j++) { int y = (0xff & ((int) data[i * width + j])); int u = (0xff & ((int) data[frameSize + (i >> 1) * width + (j & ~1) + 0])); int v = (0xff & ((int) data[frameSize + (i >> 1) * width + (j & ~1) + 1])); y = y < 16 ? 16 : y; int r = Math.round(1.164f * (y - 16) + 1.596f * (v - 128)); int g = Math.round(1.164f * (y - 16) - 0.813f * (v - 128) - 0.391f * (u - 128)); int b = Math.round(1.164f * (y - 16) + 2.018f * (u - 128)); r = r < 0 ? 0 : (r > 255 ? 255 : r); g = g < 0 ? 0 : (g > 255 ? 255 : g); b = b < 0 ? 0 : (b > 255 ? 255 : b); rgba[i * width + j] = 0xff000000 + (b << 16) + (g << 8) + r; } Bitmap bmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); bmp.setPixels(rgba, 0 , width, 0, 0, width, height); return bmp; }

When I save btmpFace as an image, the faces turn blue. How can i solve this problem? Thanks

necatiCelik avatar Jul 01 '21 14:07 necatiCelik

@necatiCelik Man i don't know how to thank you but I was in so trouble converting byte array to bitmap because BitmapFactory.decodeByteArray() was not working at all and was giving error. But your code works so fine. Thank you so much brother

ssoftwares avatar Sep 17 '21 13:09 ssoftwares