How can I convert android bitmap to NV12 color format? -


i'm writing code converting android bitmap nv12 format.

i found code gives me nv21 android bitmap, , seems code works. (convert bitmap array yuv (ycbcr nv21))

only difference found switch u , v byte between nv12 , nv21 according reference. (http://www.fourcc.org/yuv.php)

so changed position of u , v original code, , result following.

byte [] getnv12(int inputwidth, int inputheight, bitmap scaled) {     // reference (variation) : https://gist.github.com/wobbals/5725412      int [] argb = new int[inputwidth * inputheight];      //log.i(tag, "scaled : " + scaled);     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;         v = ( ( -38 * r -  74 * g + 112 * b + 128) >> 8) + 128; // u         u = ( ( 112 * r -  94 * g -  18 * b + 128) >> 8) + 128; // v          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 ++;         }     } } 

am wrong in converting images? (i'm pretty sure encoder has no problem.)

broken image screenshot : https://www.dropbox.com/s/vho14831fgnh1kl/thu%20aug%2001%2008_56_14%20gmt%2b09_00%202013%20%281%29.mp4_000002000.jpg

replace

a = (argb[index] & 0xff000000) >> 24; // not used  r = (argb[index] & 0xff0000) >> 16;  g = (argb[index] & 0xff00) >> 8;  b = (argb[index] & 0xff) >> 0; 

with,

r = (argb[index] & 0xff000000) >>> 24; g = (argb[index] & 0xff0000) >> 16; b = (argb[index] & 0xff00) >> 8; 

Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

How to get multiresult with multicondition in Sql Server -