Feature: support rotate degree of 180

pull/209/head
xufuji456 3 years ago
parent 1804315e3a
commit 4aa4e0063a
  1. 6
      Live/src/main/java/com/frank/live/camera/Camera2Helper.java
  2. 54
      Live/src/main/java/com/frank/live/util/YUVUtil.java

@ -584,11 +584,15 @@ public class Camera2Helper {
offset += len / 4; offset += len / 4;
} }
if (rotateDegree == 90) { if (rotateDegree == 90 || rotateDegree == 180) {
if (dstData == null) { if (dstData == null) {
dstData = new byte[len * 3 / 2]; dstData = new byte[len * 3 / 2];
} }
if (rotateDegree == 90) {
YUVUtil.YUV420pRotate90(dstData, yuvData, width, height); YUVUtil.YUV420pRotate90(dstData, yuvData, width, height);
} else {
YUVUtil.YUV420pRotate180(dstData, yuvData, width, height);
}
if (camera2Listener != null) { if (camera2Listener != null) {
camera2Listener.onPreviewFrame(dstData); camera2Listener.onPreviewFrame(dstData);
} }

@ -1,5 +1,7 @@
package com.frank.live.util; package com.frank.live.util;
import android.util.Log;
/** /**
* Tool of transforming YUV format * Tool of transforming YUV format
* Created by frank on 2018/7/1. * Created by frank on 2018/7/1.
@ -43,25 +45,53 @@ public class YUVUtil {
return yuv420sp; return yuv420sp;
} }
public static void YUV420pRotate90(byte[] dstData, byte[] data, int width, int height) { public static void YUV420pRotate90(byte[] dst, byte[] src, int width, int height) {
int n = 0; int n = 0;
int wh = width * height; int wh = width * height;
//y int halfWidth = width / 2;
int halfHeight = height / 2;
// y
for (int j = 0; j < width; j++) { for (int j = 0; j < width; j++) {
for(int i = height - 1; i >= 0; i--) { for (int i = height - 1; i >= 0; i--) {
dstData[n++] = data[width * i + j]; dst[n++] = src[width * i + j];
}
}
// u
for (int i = 0; i < halfWidth; i++) {
for (int j = 1; j <= halfHeight; j++) {
dst[n++] = src[wh + ((halfHeight - j) * halfWidth + i)];
}
}
// v
for (int i = 0; i < halfWidth; i++) {
for (int j = 1; j <= halfHeight; j++) {
dst[n++] = src[wh + wh / 4 + ((halfHeight - j) * halfWidth + i)];
}
}
}
public static void YUV420pRotate180(byte[] dst, byte[] src, int width, int height) {
int n = 0;
int halfWidth = width / 2;
int halfHeight = height / 2;
// y
for (int j = height - 1; j >= 0; j--) {
for (int i = width; i > 0; i--) {
dst[n++] = src[width * j + i - 1];
} }
} }
//u // u
for (int i = 0; i < width / 2; i++) { int offset = width * height;
for (int j = 1; j <= height / 2; j++) { for (int j = halfHeight - 1; j >= 0; j--) {
dstData[n++] = data[wh + ((height/2 - j) * (width / 2) + i)]; for (int i = halfWidth; i > 0; i--) {
dst[n++] = src[offset + halfWidth * j + i - 1];
} }
} }
//v // v
for(int i = 0; i < width / 2; i++) { offset += halfWidth * halfHeight;
for(int j = 1; j <= height / 2; j++) { for (int j = halfHeight - 1; j >= 0; j--) {
dstData[n++] = data[wh + wh / 4 + ((height / 2 - j) * (width / 2) + i)]; for (int i = halfWidth; i > 0; i--) {
dst[n++] = src[offset + halfWidth * j + i - 1];
} }
} }
} }

Loading…
Cancel
Save