`
dzxiang
  • 浏览: 35512 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

将YUV422转为BMP格式

    博客分类:
  • Java
 
阅读更多
在网上找了一下,没有现成的JAVA代码,参考了一下相关的C语言转换程序,自己动手了。
/**
 * 与YUV格式转换相关,提供:将YUV422转为BMP格式
 */
public class YuvHelper {	
	public static int yuvToR(int y,int u,int v){
		if(y<0) y = (y&0x7f)|0x80;  
		if(u<0) u = (u&0x7f)|0x80;  
		if(v<0) v = (v&0x7f)|0x80;  
		return Math.round((1.164f * (y -16f) + 1.596f * (v - 128f)));
	}
	
	public static int yuvToG(int y,int u,int v){
		if(y<0) y = (y&0x7f)|0x80;  
		if(u<0) u = (u&0x7f)|0x80;  
		if(v<0) v = (v&0x7f)|0x80;  		
		return Math.round((1.164f * (y -16f) - 0.813f * (v - 128f) - 0.393f * (u - 128f)));
	}	
	
	public static int yuvToB(int y,int u,int v){
		if(y<0) y = (y&0x7f)|0x80;  
		if(u<0) u = (u&0x7f)|0x80;  
		if(v<0) v = (v&0x7f)|0x80;  		
		return Math.round(1.164f * (y -16f) + 2.018f * (u - 128f));
	}		
	
	/**
	 * RGB修正
	 * @param rgb
	 * @return
	 */
	public static byte revise(int rgb){
		if( rgb > 255) return (byte)255;
		if( rgb < 0) return 0;
		return (byte)rgb;  	
	}
	
	/**
	 * 将整数转换为双字,返回值存储在一个 4 字节数组中
	 * @param parValue
	 * @return
	 */
	public static byte[] intToDWord(int parValue) {
		byte retValue[] = new byte[4];
		retValue[0] = (byte) (parValue & 0x00FF);
		retValue[1] = (byte) ((parValue>>8) & 0x000000FF);
		retValue[2] = (byte) ((parValue>>16) & 0x000000FF);
		retValue[3] = (byte) ((parValue>>24) & 0x000000FF);
		return retValue;
	}
	
	/**
	 * 将整数转换为单字,返回值 存储在一个双字节数组中
	 * @param parValue
	 * @return
	 */
	public static byte[] intToWord(int parValue) {
		byte retValue[] = new byte[2];
		retValue[0] = (byte) (parValue & 0x00FF);
		retValue[1] = (byte) ((parValue>>8) & 0x00FF);
		return retValue;
	}	
	
	/**
	 * 将yuv文件转换为bmp格式的数据
	 * @param yuvFileBytes
	 * @param width
	 * @param height
	 * @return
	 * @throws Exception
	 */
	public static byte[][] convertYuv422ToRgb(byte[] yuvFileBytes,int width,int height) throws Exception{
		byte[][] rgbBuf = new byte[height][width*3];
		byte[][] yuv444Data = new byte[3][width];
		for(int line =0; line < height; line ++){
			for(int pixel=0; pixel < width/2; pixel++){
				//将422格式的数据转换为444格式的数据
				int from = line*width*2;
				yuv444Data[0][pixel*2] = yuvFileBytes[from+pixel*4 + 1];
				yuv444Data[1][pixel*2] = yuvFileBytes[from+pixel*4];
				yuv444Data[2][pixel*2] = yuvFileBytes[from+pixel*4 + 2];
				yuv444Data[0][pixel*2+1] = yuvFileBytes[from+pixel*4 + 3];
				yuv444Data[1][pixel*2+1] = yuvFileBytes[from+pixel*4];
				yuv444Data[2][pixel*2+1] = yuvFileBytes[from+pixel*4 + 2];
			}		
			
			for(int pixel = 0; pixel < width; pixel++){
				///将444格式的数据根据公式转换为RGB
				int r = yuvToR(yuv444Data[0][pixel], yuv444Data[1][pixel], yuv444Data[2][pixel]);
				int g = yuvToG(yuv444Data[0][pixel], yuv444Data[1][pixel], yuv444Data[2][pixel]);
				int b = yuvToB(yuv444Data[0][pixel], yuv444Data[1][pixel], yuv444Data[2][pixel]);
				rgbBuf[line][pixel*3+2] = revise(r);
				rgbBuf[line][pixel*3+1] = revise(g);
				rgbBuf[line][pixel*3] = revise(b);			
			}
		}
		return rgbBuf;
	}
	
	/**
	 * 将RGB转为BMP
	 * @param rgbBuf
	 * @param width
	 * @param height
	 * @return
	 * @throws Exception
	 */
	public static byte[] convertRgb2Bmp(byte[][] rgbBuf,int width, int height) throws Exception{
		ByteArrayOutputStream bos = new ByteArrayOutputStream();
		try{
			bos.write(new byte[]{ 'B', 'M' });
			bos.write(intToDWord(width*height*3 + 0x36));
			bos.write(intToDWord(0));
			//bos.write(intToWord(0));
			bos.write(intToDWord(0x36));
			bos.write(intToDWord(0x28));
			bos.write(intToDWord(width));
			bos.write(intToDWord(height));
			bos.write(intToWord(1));
			bos.write(intToWord(24));
			bos.write(intToDWord(0));
			bos.write(intToDWord(width*height*3));
			bos.write(intToDWord(0));
			bos.write(intToDWord(0));
			bos.write(intToDWord(0));
			bos.write(intToDWord(0));		
			
			int len = rgbBuf.length;
			for(int i=len-1;i>=0;i--){
				bos.write(rgbBuf[i]);			
			}
			bos.flush();
		}catch(Exception ex){
			ex.printStackTrace();
			throw ex;
		}finally{
			bos.close();
		}		
		return bos.toByteArray();
	}
	
	public static void main(String[] args) {
		try {
			int width = 720,height = 288;
			byte[][] rgbBuf = YuvHelper.convertYuv422ToRgb(FileHelper.getBytes("E:/test/aaa.yuv"), width, height);
			byte[] bmp = YuvHelper.convertRgb2Bmp(rgbBuf, width, height);
			FileHelper.writeFile(bmp, "E:/test/aaa.bmp");
		} catch (Exception e) {
			e.printStackTrace();
		}		
	}
}
0
0
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics