常用YUV转RGB代码

常用YUV转RGB java代码


public class YuvToRGB {
	private static int R = 0;
	private static int G = 1;
	private static int B = 2;
	//I420是yuv420格式,是3个plane,排列方式为(Y)(U)(V)
	public static int[] I420ToRGB(byte[] src, int width, int height){
		int numOfPixel = width * height;
		int positionOfV = numOfPixel;
		int positionOfU = numOfPixel/4 + numOfPixel;
		int[] rgb = new int[numOfPixel*3];
		for(int i=0; i255? 255 : rgb.r);
		rgb.g =(rgb.g<0? 0: rgb.g>255? 255 : rgb.g);
		rgb.b =(rgb.b<0? 0: rgb.b>255? 255 : rgb.b);
		return rgb;
	}
	
	//YV16是yuv422格式,是三个plane,(Y)(U)(V)
	public static int[] YV16ToRGB(byte[] src, int width, int height){
		int numOfPixel = width * height;
		int positionOfU = numOfPixel;
		int positionOfV = numOfPixel/2 + numOfPixel;
		int[] rgb = new int[numOfPixel*3];
		for(int i=0; i>1)*3;
				RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
				rgb[index+R] = tmp.r;
				rgb[index+G] = tmp.g;
				rgb[index+B] = tmp.b;
				index += 3;
				tmp = yuvTorgb(src[Y2], src[U], src[V]);
				rgb[index+R] = tmp.r;
				rgb[index+G] = tmp.g;
				rgb[index+B] = tmp.b;
			}
		}
		return rgb;
	}
	
	//UYVY是YUV422格式,排列是(UYVY),是1 plane
	public static int[] UYVYToRGB(byte[] src, int width, int height){
		int numOfPixel = width * height;
		int[] rgb = new int[numOfPixel*3];
		int lineWidth = 2*width;
		for(int i=0; i>1)*3;
				RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
				rgb[index+R] = tmp.r;
				rgb[index+G] = tmp.g;
				rgb[index+B] = tmp.b;
				index += 3;
				tmp = yuvTorgb(src[Y2], src[U], src[V]);
				rgb[index+R] = tmp.r;
				rgb[index+G] = tmp.g;
				rgb[index+B] = tmp.b;
			}
		}
		return rgb;
	}
	
	//NV21是YUV420格式,排列是(Y), (VU),是2 plane
	public static int[] NV21ToRGB(byte[] src, int width, int height){
		int numOfPixel = width * height;
		int positionOfV = numOfPixel;
		int[] rgb = new int[numOfPixel*3];

		for(int i=0; i>1)*3;
				RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
				rgb[index+R] = tmp.r;
				rgb[index+G] = tmp.g;
				rgb[index+B] = tmp.b;
				index += 3;
				tmp = yuvTorgb(src[Y2], src[U], src[V]);
				rgb[index+R] = tmp.r;
				rgb[index+G] = tmp.g;
				rgb[index+B] = tmp.b;
			}
		}
		return rgb;
	}
	
	//VYUY是YUV422格式,排列是(VYUY),是1 plane
	public static int[] VYUYToRGB(byte[] src, int width, int height){
		int numOfPixel = width * height;
		int[] rgb = new int[numOfPixel*3];
		int lineWidth = 2*width;
		for(int i=0; i>1)*3;
				RGB tmp = yuvTorgb(src[Y1], src[U], src[V]);
				rgb[index+R] = tmp.r;
				rgb[index+G] = tmp.g;
				rgb[index+B] = tmp.b;
				index += 3;
				tmp = yuvTorgb(src[Y2], src[U], src[V]);
				rgb[index+R] = tmp.r;
				rgb[index+G] = tmp.g;
				rgb[index+B] = tmp.b;
			}
		}
		return rgb;
	}
}




你可能感兴趣的:(Algorithms,Java,Web)