android - Convert bitmap array to YUV (YCbCr NV21) -
how convert bitmap returned bitmapfactory.decodefile()
yuv format (simillar camera's onpreviewframe() returns in byte array)?
here code works:
// untested function byte [] getnv21(int inputwidth, int inputheight, bitmap scaled) { int [] argb = new int[inputwidth * inputheight]; scaled.getpixels(argb, 0, inputwidth, 0, 0, inputwidth, inputheight); byte [] yuv = new byte[inputwidth*inputheight*3/2]; encodeyuv420sp(yuv, argb, inputwidth, inputheight); scaled.recycle(); return yuv; } void encodeyuv420sp(byte[] yuv420sp, int[] argb, int width, int height) { final int framesize = width * height; int yindex = 0; int uvindex = framesize; int a, r, g, b, y, u, v; int index = 0; (int j = 0; j < height; j++) { (int = 0; < width; i++) { = (argb[index] & 0xff000000) >> 24; // not used r = (argb[index] & 0xff0000) >> 16; g = (argb[index] & 0xff00) >> 8; b = (argb[index] & 0xff) >> 0; // known rgb yuv algorithm y = ( ( 66 * r + 129 * g + 25 * b + 128) >> 8) + 16; u = ( ( -38 * r - 74 * g + 112 * b + 128) >> 8) + 128; v = ( ( 112 * r - 94 * g - 18 * b + 128) >> 8) + 128; // nv21 has plane of y , interleaved planes of vu each sampled factor of 2 // meaning every 4 y pixels there 1 v , 1 u. note sampling every other // pixel , every other scanline. yuv420sp[yindex++] = (byte) ((y < 0) ? 0 : ((y > 255) ? 255 : y)); if (j % 2 == 0 && index % 2 == 0) { yuv420sp[uvindex++] = (byte)((v<0) ? 0 : ((v > 255) ? 255 : v)); yuv420sp[uvindex++] = (byte)((u<0) ? 0 : ((u > 255) ? 255 : u)); } index ++; } } }
Comments
Post a Comment