Skip to content

Commit cf677ae

Browse files
author
xufuji456
committed
Feature: flip yuv when it is front camera
1 parent 02b0b96 commit cf677ae

5 files changed

Lines changed: 31 additions & 2 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -593,6 +593,10 @@ public void onImageAvailable(ImageReader reader) {
593593
} else {
594594
YUVUtil.YUV420pRotate180(dstData, yuvData, width, height);
595595
}
596+
// front camera need to flip
597+
if (CAMERA_ID_FRONT.equals(mCameraId)) {
598+
YUVUtil.flipYUV(dstData, width, height, rotation == 90, rotation == 180);
599+
}
596600
if (camera2Listener != null) {
597601
camera2Listener.onPreviewFrame(dstData);
598602
}

‎Live/src/main/java/com/frank/live/util/YUVUtil.java‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,4 +126,29 @@ public static void YUV420pRotate180(byte[] dst, byte[] src, int width, int heigh
126126
}
127127
}
128128

129+
private static void swap(byte[] a, int i, int j) {
130+
byte temp = a[i];
131+
a[i] = a[j];
132+
a[j] = temp;
133+
}
134+
135+
public static void flipYUV(byte[] dst, int width, int height, boolean flipX, boolean flipY) {
136+
if (dst == null || width <= 0 || height <= 0)
137+
return;
138+
if (flipY) {
139+
for (int i=0; i<height; i++) {
140+
for (int j=0; j<width/2; j++) {
141+
swap(dst, j, width - j - 1);
142+
}
143+
}
144+
}
145+
if (flipX) {
146+
for (int i=0; i<width; i++) {
147+
for (int j=0; j<height/2; j++) {
148+
swap(dst, j, height - j - 1);
149+
}
150+
}
151+
}
152+
}
153+
129154
}

‎app/src/main/cpp/yuv/yuv_converter.cpp‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
// U = (-38 * R - 74 * G + 112 * B) >> 8 + 128
2929
// V = (112 * R - 94 * G - 18 * B) >> 8 + 128
3030

31-
static void rgba_to_yuv420p(int *argb, int8_t *yuv, int width, int height) {
31+
static void rgba_to_yuv420p(const int *argb, int8_t *yuv, int width, int height) {
3232
int frameSize = width * height;
3333
int index = 0;
3434
int yIndex = 0;

‎app/src/main/cpp/yuv/yuv_converter.h‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77

88
#include <cstdint>
99

10-
static void rgba_to_yuv420p(int *argb, int8_t *yuv, int width, int height);
10+
static void rgba_to_yuv420p(const int *argb, int8_t *yuv, int width, int height);
1111

1212
static void yuv420p_to_argb(const int8_t *yuv, int *argb, int width, int height);
1313

‎picture/ffmpeg_group.png‎

979 Bytes
Loading

0 commit comments

Comments
 (0)