RGB YUV420sp 互相转换 nv21

packagecom.demo;

importjavax.imageio.ImageIO;

importjava.awt.image.BufferedImage;

importjava.io.File;

importjava.io.IOException;

import staticcom.demo.MyColorUtil.decodeYUV420SP;

/**

* Created by ff135 on 2017/4/27.

* RGB转YUV:

Y = 0.299 R + 0.587 G + 0.114 B

U = - 0.1687 R - 0.3313 G + 0.5 B + 128

V = 0.5 R - 0.4187 G - 0.0813 B + 128

YUV转RGB

R = Y + 1.402 (V - 128)

G = Y - 0.34414 (U - 128) - 0.71414 (V - 128)

B = Y + 1.772 (U - 128)

*

*

*

*/

public classTestUtil {

public static voidmain(String[] args) {

File imgFile =newFile("E:/p3.jpg");

try{

BufferedImage bufferedImage = ImageIO.read(imgFile);

intwidth = bufferedImage.getWidth();

intheight = bufferedImage.getHeight();

int[] pixels = bufferedImage.getRGB(0,0,width,height,null,0,width);

byte[] yuvs=encodeYUV420SP(pixels,width,height);

int[] pixelsNew=decodeYUV420sp(yuvs,width,height);

BufferedImage newbuff=newBufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

newbuff.setRGB(0,0,width,height,pixelsNew,0,width);

ImageIO.write(newbuff,"jpg",newFile("E:/yy.jpg"));

}catch(IOException e) {

e.printStackTrace();

}

}

/**

* nv21  先u后v

*

*@paramargb

*@paramwidth

*@paramheight

*@return

*/

public static byte[] encodeYUV420SP(int[] argb,intwidth,intheight){

intsize = (int) ((Math.ceil(width/2.0)) * (Math.ceil(height/2.0)));

intframeSize = width * height;

byte[] yuvs=new byte[frameSize + size *2];

inty=0,u=0,v=0;

intr=0,g=0,b=0,a=0;

intindex=0,uvindex=frameSize;

for(inti =0; i < height; i++) {

for(intj =0; j < width; j++) {

a = (argb[index] &0xff000000) >>24;// a is not used obviously

r = (argb[index] &0xff0000) >>16;

g = (argb[index] &0xff00) >>8;

b = (argb[index] &0xff) >>0;

//转换公式

y= (int) (0.299*r +0.587*g +0.114*b);

u= (int) (-0.1687*r -0.3313*g +0.5*b +128);

v= (int) (0.5*r -0.4187*g -0.0813*b +128);

System.out.println("x:"+i+"y:"+j+"  y:"+y+" u:"+u+" v:"+v);

yuvs[index++] = (byte) ((y <0) ?0: ((y >255) ?255: y));

if(i%2==0&& j %2==0){

yuvs[uvindex++] = (byte) ((u <0) ?0: ((u >255) ?255: u));

yuvs[uvindex++] = (byte) ((v <0) ?0: ((v >255) ?255: v));

//                    yuvs[uvindex++]=(byte)120;

//                    yuvs[uvindex++] = (byte) ((v < 0) ? 0 : ((v > 255) ? 255 : v));

//                    yuvs[uvindex++]=(byte)240;

}

}

}

returnyuvs;

}

public static int[] decodeYUV420sp(byte[] yuvs ,intwidth,intheight){

intframeSize = width * height;

int[] rgbs =new int[frameSize];

intindex=0;

intuvindex=frameSize;

intu=0,v=0,y=0;

for(inti =0; i < height; i++) {

for(intj =0; j < width; j++) {

if(i%2==0&& j %2==0){

u=yuvs[uvindex++];

v=yuvs[uvindex++];

}

//保存到数组中去

y=yuvs[index];

rgbs[index++]=yuv2Rgb(y,u,v);

}

}

returnrgbs;

}

public static intyuv2Rgb(inty,intu,intv){

// 转换公式

intr = (int) ((y&0xff)+1.402*((v&0xff) -128));

intg = (int) ((y&0xff)-0.34414* ((u&0xff) -128) -0.71414* ((v&0xff) -128));

intb = (int) ((y&0xff)+1.772* ((u&0xff) -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);

return(0xff<<24) + (r<<16) + (g<<8) + b;

}

}

你可能感兴趣的:(RGB YUV420sp 互相转换 nv21)