Skip to content

Commit 4190fe6

Browse files
committed
RGB YUV420
1 parent e3ddb40 commit 4190fe6

File tree

1 file changed

+76
-0
lines changed

1 file changed

+76
-0
lines changed

android/src/main/java/cn/jiguang/imui/messagelist/DecodeUtil.java

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,82 @@
1818

1919
public class DecodeUtil {
2020

21+
public byte[] getYUV420sp(int inputWidth, int inputHeight,
22+
Bitmap scaled) {
23+
int[] argb = new int[inputWidth * inputHeight];
24+
25+
scaled.getPixels(argb, 0, inputWidth, 0, 0, inputWidth, inputHeight);
26+
27+
byte[] yuv = new byte[inputWidth * inputHeight * 3 / 2];
28+
29+
encodeYUV420SP(yuv, argb, inputWidth, inputHeight);
30+
31+
scaled.recycle();
32+
33+
return yuv;
34+
}
35+
36+
37+
private void encodeYUV420SP(byte[] yuv420sp, int[] argb, int width,
38+
int height) {
39+
// 帧图片的像素大小
40+
final int frameSize = width * height;
41+
// ---YUV数据---
42+
int Y, U, V;
43+
// Y的index从0开始
44+
int yIndex = 0;
45+
// UV的index从frameSize开始
46+
int uvIndex = frameSize;
47+
48+
// ---颜色数据---
49+
// int a, R, G, B;
50+
int R, G, B;
51+
//
52+
int argbIndex = 0;
53+
//
54+
55+
// ---循环所有像素点,RGB转YUV---
56+
for (int j = 0; j < height; j++) {
57+
for (int i = 0; i < width; i++) {
58+
59+
// a is not used obviously
60+
// a = (argb[argbIndex] & 0xff000000) >> 24;
61+
R = (argb[argbIndex] & 0xff0000) >> 16;
62+
G = (argb[argbIndex] & 0xff00) >> 8;
63+
B = (argb[argbIndex] & 0xff);
64+
//
65+
argbIndex++;
66+
67+
// well known RGB to YUV algorithm
68+
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
69+
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
70+
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
71+
72+
//
73+
Y = Math.max(0, Math.min(Y, 255));
74+
U = Math.max(0, Math.min(U, 255));
75+
V = Math.max(0, Math.min(V, 255));
76+
77+
// NV21 has a plane of Y and interleaved planes of VU each
78+
// sampled by a factor of 2
79+
// meaning for every 4 Y pixels there are 1 V and 1 U. Note the
80+
// sampling is every other
81+
// pixel AND every other scanline.
82+
// ---Y---
83+
yuv420sp[yIndex++] = (byte) Y;
84+
85+
// ---UV---
86+
// if ((j % 2 == 0) && (i % 2 == 0)) {
87+
//
88+
//
89+
//
90+
// yuv420sp[uvIndex++] = (byte) V;
91+
//
92+
// yuv420sp[uvIndex++] = (byte) U;
93+
// }
94+
}
95+
}
96+
}
2197
private static byte[] getRGB(Bitmap bitmap) {
2298
int width = bitmap.getWidth();
2399
int height = bitmap.getHeight();

0 commit comments

Comments
 (0)