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;

}

}

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 由于H.264等压缩算法都是在YUV的颜色空间上进行的,所有在进行压缩前,首先要进行颜色空间的转换。如果摄像头采集...
    眷卿三世阅读 13,694评论 2 6
  • ¥开启¥ 【iAPP实现进入界面执行逐一显】 〖2017-08-25 15:22:14〗 《//首先开一个线程,因...
    小菜c阅读 6,632评论 0 17
  • 转眼间,我在易效能天使21班的100天践行即将结束了。 我在2016年夏天偶然间看见了一篇写时间管理的文章,于是就...
    汤静阅读 1,189评论 7 14
  • 测试文章
    了缘道长阅读 411评论 0 0
  • 第21天·21天美颜瘦身 每一位都可以通过这张卡片觉察自己: 1、直觉他叫什么名字?小强 2、他几岁了?8 3、他...
    韦梅梅阅读 224评论 0 0