Skip to content

Commit 4aa4e00

Browse files
committed
Feature: support rotate degree of 180
1 parent 1804315 commit 4aa4e00

2 files changed

Lines changed: 84 additions & 50 deletions

File tree

‎Live/src/main/java/com/frank/live/camera/Camera2Helper.java‎

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -584,11 +584,15 @@ public void onImageAvailable(ImageReader reader) {
584584
offset += len / 4;
585585
}
586586

587-
if (rotateDegree == 90) {
587+
if (rotateDegree == 90 || rotateDegree == 180) {
588588
if (dstData == null) {
589589
dstData = new byte[len * 3 / 2];
590590
}
591-
YUVUtil.YUV420pRotate90(dstData, yuvData, width, height);
591+
if (rotateDegree == 90) {
592+
YUVUtil.YUV420pRotate90(dstData, yuvData, width, height);
593+
} else {
594+
YUVUtil.YUV420pRotate180(dstData, yuvData, width, height);
595+
}
592596
if (camera2Listener != null) {
593597
camera2Listener.onPreviewFrame(dstData);
594598
}
Lines changed: 78 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,69 +1,99 @@
11
package com.frank.live.util;
22

3+
import android.util.Log;
4+
35
/**
46
* Tool of transforming YUV format
57
* Created by frank on 2018/7/1.
68
*/
79

810
public class YUVUtil {
911

10-
public static byte[] ARGBtoYUV420SemiPlanar(int[] input, int width, int height) {
12+
public static byte[] ARGBtoYUV420SemiPlanar(int[] input, int width, int height) {
1113

12-
final int frameSize = width * height;
13-
byte[] yuv420sp = new byte[width * height * 3 / 2];
14-
int yIndex = 0;
15-
int uvIndex = frameSize;
14+
final int frameSize = width * height;
15+
byte[] yuv420sp = new byte[width * height * 3 / 2];
16+
int yIndex = 0;
17+
int uvIndex = frameSize;
1618

17-
int a, R, G, B, Y, U, V;
18-
int index = 0;
19-
for (int j = 0; j < height; j++) {
20-
for (int i = 0; i < width; i++) {
19+
int a, R, G, B, Y, U, V;
20+
int index = 0;
21+
for (int j = 0; j < height; j++) {
22+
for (int i = 0; i < width; i++) {
2123

22-
a = (input[index] & 0xff000000) >> 24;
23-
R = (input[index] & 0xff0000) >> 16;
24-
G = (input[index] & 0xff00) >> 8;
25-
B = (input[index] & 0xff);
24+
a = (input[index] & 0xff000000) >> 24;
25+
R = (input[index] & 0xff0000) >> 16;
26+
G = (input[index] & 0xff00) >> 8;
27+
B = (input[index] & 0xff);
2628

27-
// RGB to YUV algorithm
28-
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
29-
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
30-
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
29+
// RGB to YUV algorithm
30+
Y = ((66 * R + 129 * G + 25 * B + 128) >> 8) + 16;
31+
U = ((-38 * R - 74 * G + 112 * B + 128) >> 8) + 128;
32+
V = ((112 * R - 94 * G - 18 * B + 128) >> 8) + 128;
3133

32-
// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
33-
// meaning for every 4 Y pixels there are 1 V and 1 U.
34-
yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : (Math.min(Y, 255)));
35-
if (j % 2 == 0 && index % 2 == 0) {
36-
yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : (Math.min(V, 255)));
37-
yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : (Math.min(U, 255)));
38-
}
34+
// NV21 has a plane of Y and interleaved planes of VU each sampled by a factor of 2
35+
// meaning for every 4 Y pixels there are 1 V and 1 U.
36+
yuv420sp[yIndex++] = (byte) ((Y < 0) ? 0 : (Math.min(Y, 255)));
37+
if (j % 2 == 0 && index % 2 == 0) {
38+
yuv420sp[uvIndex++] = (byte) ((V < 0) ? 0 : (Math.min(V, 255)));
39+
yuv420sp[uvIndex++] = (byte) ((U < 0) ? 0 : (Math.min(U, 255)));
40+
}
3941

40-
index++;
41-
}
42+
index++;
43+
}
44+
}
45+
return yuv420sp;
4246
}
43-
return yuv420sp;
44-
}
4547

46-
public static void YUV420pRotate90(byte[] dstData, byte[] data, int width, int height) {
47-
int n = 0;
48-
int wh = width * height;
49-
//y
50-
for (int j = 0; j < width; j++) {
51-
for(int i = height - 1; i >= 0; i--) {
52-
dstData[n++] = data[width * i + j];
53-
}
54-
}
55-
//u
56-
for (int i = 0; i < width / 2; i++) {
57-
for (int j = 1; j <= height / 2; j++) {
58-
dstData[n++] = data[wh + ((height/2 - j) * (width / 2) + i)];
59-
}
48+
public static void YUV420pRotate90(byte[] dst, byte[] src, int width, int height) {
49+
int n = 0;
50+
int wh = width * height;
51+
int halfWidth = width / 2;
52+
int halfHeight = height / 2;
53+
// y
54+
for (int j = 0; j < width; j++) {
55+
for (int i = height - 1; i >= 0; i--) {
56+
dst[n++] = src[width * i + j];
57+
}
58+
}
59+
// u
60+
for (int i = 0; i < halfWidth; i++) {
61+
for (int j = 1; j <= halfHeight; j++) {
62+
dst[n++] = src[wh + ((halfHeight - j) * halfWidth + i)];
63+
}
64+
}
65+
// v
66+
for (int i = 0; i < halfWidth; i++) {
67+
for (int j = 1; j <= halfHeight; j++) {
68+
dst[n++] = src[wh + wh / 4 + ((halfHeight - j) * halfWidth + i)];
69+
}
70+
}
6071
}
61-
//v
62-
for(int i = 0; i < width / 2; i++) {
63-
for(int j = 1; j <= height / 2; j++) {
64-
dstData[n++] = data[wh + wh / 4 + ((height / 2 - j) * (width / 2) + i)];
65-
}
72+
73+
public static void YUV420pRotate180(byte[] dst, byte[] src, int width, int height) {
74+
int n = 0;
75+
int halfWidth = width / 2;
76+
int halfHeight = height / 2;
77+
// y
78+
for (int j = height - 1; j >= 0; j--) {
79+
for (int i = width; i > 0; i--) {
80+
dst[n++] = src[width * j + i - 1];
81+
}
82+
}
83+
// u
84+
int offset = width * height;
85+
for (int j = halfHeight - 1; j >= 0; j--) {
86+
for (int i = halfWidth; i > 0; i--) {
87+
dst[n++] = src[offset + halfWidth * j + i - 1];
88+
}
89+
}
90+
// v
91+
offset += halfWidth * halfHeight;
92+
for (int j = halfHeight - 1; j >= 0; j--) {
93+
for (int i = halfWidth; i > 0; i--) {
94+
dst[n++] = src[offset + halfWidth * j + i - 1];
95+
}
96+
}
6697
}
67-
}
6898

6999
}

0 commit comments

Comments
 (0)