Bitmap和YUV的转换

以前做过的一个视频通话中,有用到Bitmap和YUV的转换,现在整理出来。

参考自:http://blog.csdn.net/lancees/article/details/7686046

          http://www.cnblogs.com/leaven/archive/2012/09/06/2672830.html

以下是各种转换,仅供参考

	private static Bitmap getAssetFile() {
		Bitmap bitmap = null;
		try {
			bitmap = BitmapFactory.decodeStream(NgnApplication.getContext()
					.getAssets().open("chat_default_bg.png"));
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return bitmap;
	}

	public static byte[] rgb2YCbCr420(int[] pixels, int width, int height) {
		int len = width * height;
		// yuv格式数组大小,y亮度占len长度,u,v各占len/4长度。
		byte[] yuv = new byte[len * 3 / 2];
		int y, u, v;
		for (int i = 0; i < height; i++) {
			for (int j = 0; j < width; j++) {
				// 屏蔽ARGB的透明度值
				int rgb = pixels[i * width + j] & 0x00FFFFFF;
				// 像素的颜色顺序为bgr,移位运算。
				int r = rgb & 0xFF;
				int g = (rgb >> 8) & 0xFF;
				int b = (rgb >> 16) & 0xFF;
				// 套用公式
				y = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16;
				u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128;
				v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128;
				// rgb2yuv
				// y = (int) (0.299 * r + 0.587 * g + 0.114 * b);
				// u = (int) (-0.147 * r - 0.289 * g + 0.437 * b);
				// v = (int) (0.615 * r - 0.515 * g - 0.1 * b);
				// RGB转换YCbCr
				// y = (int) (0.299 * r + 0.587 * g + 0.114 * b);
				// u = (int) (-0.1687 * r - 0.3313 * g + 0.5 * b + 128);
				// if (u > 255)
				// u = 255;
				// v = (int) (0.5 * r - 0.4187 * g - 0.0813 * b + 128);
				// if (v > 255)
				// v = 255;
				// 调整
				y = y < 16 ? 16 : (y > 255 ? 255 : y);
				u = u < 0 ? 0 : (u > 255 ? 255 : u);
				v = v < 0 ? 0 : (v > 255 ? 255 : v);
				// 赋值
				yuv[i * width + j] = (byte) y;
				yuv[len + (i >> 1) * width + (j & ~1) + 0] = (byte) u;
				yuv[len + +(i >> 1) * width + (j & ~1) + 1] = (byte) v;
			}
		}
		return yuv;
	}

	public static void decodeYUV420SP(byte[] rgbBuf, byte[] yuv420sp,
			int width, int height) {

		final int frameSize = width * height;
		if (rgbBuf == null)

			throw new NullPointerException("buffer 'rgbBuf' is null");
		if (rgbBuf.length < frameSize * 3)

			throw new IllegalArgumentException("buffer 'rgbBuf' size "
					+ rgbBuf.length + " < minimum " + frameSize * 3);

		if (yuv420sp == null)

			throw new NullPointerException("buffer 'yuv420sp' is null");

		if (yuv420sp.length < frameSize * 3 / 2)

			throw new IllegalArgumentException("buffer 'yuv420sp' size "
					+ yuv420sp.length + " < minimum " + frameSize * 3 / 2);

		int i = 0, y = 0;

		int uvp = 0, u = 0, v = 0;

		int y1192 = 0, r = 0, g = 0, b = 0;

		for (int j = 0, yp = 0; j < height; j++) {
			uvp = frameSize + (j >> 1) * width;
			u = 0;
			v = 0;

			for (i = 0; i < width; i++, yp++) {
				y = (0xff & ((int) yuv420sp[yp])) - 16;

				if (y < 0)
					y = 0;

				if ((i & 1) == 0) {
					v = (0xff & yuv420sp[uvp++]) - 128;
					u = (0xff & yuv420sp[uvp++]) - 128;
				}

				y1192 = 1192 * y;
				r = (y1192 + 1634 * v);
				g = (y1192 - 833 * v - 400 * u);
				b = (y1192 + 2066 * u);

				if (r < 0)
					r = 0;
				else if (r > 262143)
					r = 262143;

				if (g < 0)
					g = 0;
				else if (g > 262143)
					g = 262143;

				if (b < 0)
					b = 0;
				else if (b > 262143)
					b = 262143;

				rgbBuf[yp * 3] = (byte) (r >> 10);
				rgbBuf[yp * 3 + 1] = (byte) (g >> 10);
				rgbBuf[yp * 3 + 2] = (byte) (b >> 10);
			}
		}
	}

	/*
	 * 获取位图的RGB数据
	 */
	public static byte[] getRGBByBitmap(Bitmap bitmap) {
		if (bitmap == null) {
			return null;
		}

		int width = bitmap.getWidth();
		int height = bitmap.getHeight();

		int size = width * height;

		int pixels[] = new int[size];
		bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

		byte[] data = convertColorToByte(pixels);

		return data;
	}

	/*
	 * 获取位图的YUV数据
	 */
	public static byte[] getYUVByBitmap(Bitmap bitmap) {
		if (bitmap == null) {
			return null;
		}
		int width = bitmap.getWidth();
		int height = bitmap.getHeight();

		int size = width * height;

		int pixels[] = new int[size];
		bitmap.getPixels(pixels, 0, width, 0, 0, width, height);

		// byte[] data = convertColorToByte(pixels);
		byte[] data = rgb2YCbCr420(pixels, width, height);

		return data;
	}

	/*
	 * 像素数组转化为RGB数组
	 */
	public static byte[] convertColorToByte(int color[]) {
		if (color == null) {
			return null;
		}

		byte[] data = new byte[color.length * 3];
		for (int i = 0; i < color.length; i++) {
			data[i * 3] = (byte) (color[i] >> 16 & 0xff);
			data[i * 3 + 1] = (byte) (color[i] >> 8 & 0xff);
			data[i * 3 + 2] = (byte) (color[i] & 0xff);
		}

		return data;

	}

// untested function
    byte [] getNV21(int inputWidth, int inputHeight, Bitmap scaled) {

        int [] argb = new int[inputWidth * inputHeight];

        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;
        for (int j = 0; j < height; j++) {
            for (int i = 0; i < width; i++) {

                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;

                // well known RGB to YUV algorithm
                Y = ( (  66 * R + 129 * G +  25 * B + 128) >> 8) +  16;
                U = ( ( -38 * R -  74 * G + 112 * B + 128) >> 8) + 128;
                V = ( ( 112 * R -  94 * G -  18 * B + 128) >> 8) + 128;

                // NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
                //    meaning for every 4 Y pixels there are 1 V and 1 U.  Note the sampling is every other
                //    pixel AND every other scanline.
                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 ++;
            }
        }
    }
static public void encodeYUV420SP(byte[] yuv420sp, int[] rgba, int width,
			int height) {
		final int frameSize = width * height;

		int[] U, V;
		U = new int[frameSize];
		V = new int[frameSize];

		final int uvwidth = width / 2;

		int r, g, b, y, u, v;
		for (int j = 0; j < height; j++) {
			int index = width * j;
			for (int i = 0; i < width; i++) {
				r = (rgba[index] & 0xff000000) >> 24;
				g = (rgba[index] & 0xff0000) >> 16;
				b = (rgba[index] & 0xff00) >> 8;

				// rgb to yuv
				y = (66 * r + 129 * g + 25 * b + 128) >> 8 + 16;
				u = (-38 * r - 74 * g + 112 * b + 128) >> 8 + 128;
				v = (112 * r - 94 * g - 18 * b + 128) >> 8 + 128;

				// clip y
				yuv420sp[index++] = (byte) ((y < 0) ? 0 : ((y > 255) ? 255 : y));
				U[index] = u;
				V[index++] = v;
			}
		}
	}

static void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width, int height) {
		final int frameSize = width * height;

		int yIndex = 0;
		int uvIndex = frameSize;

		int R, G, B, Y, U, V;
		int index = 0;
		for (int j = 0; j < height; j++) {
			for (int i = 0; i < width; i++) {

				R = (argb[index] & 0xff0000) >> 16;
				G = (argb[index] & 0xff00) >> 8;
				B = (argb[index] & 0xff) >> 0;

				// well known RGB to YUV algorithm
				Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
				U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
				V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;

				// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
				//    meaning for every 4 Y pixels there are 1 V and 1 U.  Note the sampling is every other
				//    pixel AND every other scanline.
				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++;
			}
		}
	}



好的,以下是一个简单的Java代码示例,演示了如何将Bitmap对象叠加在yuv数据上: ```java public class YuvUtils { public static void overlayBitmapOnYuv(byte[] yuvData, int width, int height, Bitmap bitmap, int x, int y) { // 将yuv数据转换RGB格式 int[] pixels = new int[width * height]; YuvImage yuvImage = new YuvImage(yuvData, ImageFormat.NV21, width, height, null); ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); yuvImage.compressToJpeg(new Rect(0, 0, width, height), 100, outputStream); byte[] jpegData = outputStream.toByteArray(); BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.ARGB_8888; Bitmap rgbBitmap = BitmapFactory.decodeByteArray(jpegData, 0, jpegData.length, options); rgbBitmap.getPixels(pixels, 0, width, 0, 0, width, height); // 在RGB数据上绘制半透明的Bitmap Canvas canvas = new Canvas(rgbBitmap); Paint paint = new Paint(); paint.setAlpha(128); canvas.drawBitmap(bitmap, x, y, paint); // 将RGB数据转换yuv格式 int[] yuv = new int[width * height]; for (int i = 0; i < height; i++) { for (int j = 0; j < width; j++) { int pixel = pixels[i * width + j]; int r = (pixel >> 16) & 0xff; int g = (pixel >> 8) & 0xff; int b = pixel & 0xff; yuv[i * width + j] = ((66 * r + 129 * g + 25 * b + 128) >> 8) + 16; if (j % 2 == 0 && i % 2 == 0) { int u = ((-38 * r - 74 * g + 112 * b + 128) >> 8) + 128; int v = ((112 * r - 94 * g - 18 * b + 128) >> 8) + 128; yuv[width * height + (i / 2) * (width / 2) + (j / 2)] = v; yuv[width * height + width * height / 4 + (i / 2) * (width / 2) + (j / 2)] = u; } } } // 将叠加了Bitmapyuv数据写回原始yuv数据 System.arraycopy(yuv, 0, yuvData, 0, yuvData.length); } } ``` 在调用该方法时,需要传入原始的yuv数据、宽高、Bitmap对象以及Bitmapyuv数据中的位置。该方法将在原始yuv数据上直接叠加Bitmap,并将结果写回原始yuv数据中。需要注意的是,该示例代码仅供参考,实际使用时需要根据具体需求进行修改和优化。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值