import android.annotation.TargetApi;
import android.graphics.Bitmap;
import android.media.Image;
import android.media.ImageReader;
import android.os.Build;
import androidx.annotation.RequiresApi;
import java.nio.ByteBuffer;
public class tool {
/******************************* YUV420旋转算法 *******************************/
// I420或YV12顺时针旋转
public static void rotateP(byte[] src, byte[] dest, int w, int h, int rotation) {
switch (rotation) {
case 0:
System.arraycopy(src, 0, dest, 0, src.length);
break;
case 90:
rotateP90(src, dest, w, h);
break;
case 180:
rotateP180(src, dest, w, h);
break;
case 270:
rotateP270(src, dest, w, h);
break;
}
}
// NV21或NV12顺时针旋转
public static void rotateSP(byte[] src, byte[] dest, int w, int h, int rotation) {
switch (rotation) {
case 0:
System.arraycopy(src, 0, dest, 0, src.length);
break;
case 90:
rotateSP90(src, dest, w, h);
break;
case 180:
rotateSP180(src, dest, w, h);
break;
case 270:
rotateSP270(src, dest, w, h);
break;
}
}
// NV21或NV12顺时针旋转90度
public static void rotateSP90(byte[] src, byte[] dest, int w, int h) {
int pos = 0;
int k = 0;
for (int i = 0; i <= w - 1; i++) {
for (int j = h - 1; j >= 0; j--) {
dest[k++] = src[j * w + i];
}
}
pos = w * h;
for (int i = 0; i <= w - 2; i += 2) {
for (int j = h / 2 - 1; j >= 0; j--) {
dest[k++] = src[pos + j * w + i];
dest[k++] = src[pos + j * w + i + 1];
}
}
}
// NV21或NV12顺时针旋转270度
public static void rotateSP270(byte[] src, byte[] dest, int w, int h) {
int pos = 0;
int k = 0;
for (int i = w - 1; i >= 0; i--) {
for (int j = 0; j <= h - 1; j++) {
dest[k++] = src[j * w + i];
}
}
pos = w * h;
for (int i = w - 2; i >= 0; i -= 2) {
for (int j = 0; j <= h / 2 - 1; j++) {
dest[k++] = src[pos + j * w + i];
dest[k++] = src[pos + j * w + i + 1];
}
}
}
// NV21或NV12顺时针旋转180度
public static void rotateSP180(byte[] src, byte[] dest, int w, int h) {
int pos = 0;
int k = w * h - 1;
while (k >= 0) {
dest[pos++] = src[k--];
}
k = src.length - 2;
while (pos < dest.length) {
dest[pos++] = src[k];
dest[pos++] = src[k + 1];
k -= 2;
}
}
// I420或YV12顺时针旋转90度
public static void rotateP90(byte[] src, byte[] dest, int w, int h) {
int pos = 0;
//旋转Y
int k = 0;
for (int i = 0; i < w; i++) {
for (int j = h - 1; j >= 0; j--) {
dest[k++] = src[j * w + i];
}
}
//旋转U
pos = w * h;
for (int i = 0; i < w / 2; i++) {
for (int j = h / 2 - 1; j >= 0; j--) {
dest[k++] = src[pos + j * w / 2 + i];
}
}
//旋转V
pos = w * h * 5 / 4;
for (int i = 0; i < w / 2; i++) {
for (int j = h / 2 - 1; j >= 0; j--) {
dest[k++] = src[pos + j * w / 2 + i];
}
}
}
// I420或YV12顺时针旋转270度
public static void rotateP270(byte[] src, byte[] dest, int w, int h) {
int pos = 0;
//旋转Y
int k = 0;
for (int i = w - 1; i >= 0; i--) {
for (int j = 0; j < h; j++) {
dest[k++] = src[j * w + i];
}
}
//旋转U
pos = w * h;
for (int i = w / 2 - 1; i >= 0; i--) {
for (int j = 0; j < h / 2; j++) {
dest[k++] = src[pos + j * w / 2 + i];
}
}
//旋转V
pos = w * h * 5 / 4;
for (int i = w / 2 - 1; i >= 0; i--) {
for (int j = 0; j < h / 2; j++) {
dest[k++] = src[pos + j * w / 2 + i];
}
}
}
// I420或YV12顺时针旋转180度
public static void rotateP180(byte[] src, byte[] dest, int w, int h) {
int pos = 0;
int k = w * h - 1;
while (k >= 0) {
dest[pos++] = src[k--];
}
k = w * h * 5 / 4;
while (k >= w * h) {
dest[pos++] = src[k--];
}
k = src.length - 1;
while (pos < dest.length) {
dest[pos++] = src[k--];
}
}
/******************************* YUV420格式相互转换算法 *******************************/
// i420 -> nv12, yv12 -> nv21
public static void pToSP(byte[] src, byte[] dest, int w, int h) {
int pos = w * h;
int u = pos;
int v = pos + (pos >> 2);
System.arraycopy(src, 0, dest, 0, pos);
while (pos < src.length) {
dest[pos++] = src[u++];
dest[pos++] = src[v++];
}
}
// i420 -> nv21, yv12 -> nv12
public static void pToSPx(byte[] src, byte[] dest, int w, int h) {
int pos = w * h;
int u = pos;
int v = pos + (pos >> 2);
System.arraycopy(src, 0, dest, 0, pos);
while (pos < src.length) {
dest[pos++] = src[v++];
dest[pos++] = src[u++];
}
}
// nv12 -> i420, nv21 -> yv12
public static void spToP(byte[] src, byte[] dest, int w, int h) {
int pos = w * h;
int u = pos;
int v = pos + (pos >> 2);
System.arraycopy(src, 0, dest, 0, pos);
while (pos < src.length) {
dest[u++] = src[pos++];
dest[v++] = src[pos++];
}
}
// nv12 -> yv12, nv21 -> i420
public static void spToPx(byte[] src, byte[] dest, int w, int h) {
int pos = w * h;
int u = pos;
int v = pos + (pos >> 2);
System.arraycopy(src, 0, dest, 0, pos);
while (pos < src.length) {
dest[v++] = src[pos++];
dest[u++] = src[pos++];
}
}
// i420 <-> yv12
public static void pToP(byte[] src, byte[] dest, int w, int h) {
int pos = w * h;
int off = pos >> 2;
System.arraycopy(src, 0, dest, 0, pos);
System.arraycopy(src, pos, dest, pos + off, off);
System.arraycopy(src, pos + off, dest, pos, off);
}
// nv12 <-> nv21
public static void spToSP(byte[] src, byte[] dest, int w, int h) {
int pos = w * h;
System.arraycopy(src, 0, dest, 0, pos);
for (; pos < src.length; pos += 2) {
dest[pos] = src[pos + 1];
dest[pos + 1] = src[pos];
}
}
/******************************* YUV420转换Bitmap算法 *******************************/
// 此方法虽然是官方的,但是耗时是下面方法的两倍
// public static Bitmap nv21ToBitmap(byte[] data, int w, int h) {
// final YuvImage image = new YuvImage(data, ImageFormat.NV21, w, h, null);
// ByteArrayOutputStream os = new ByteArrayOutputStream(data.length);
// if (image.compressToJpeg(new Rect(0, 0, w, h), 100, os)) {
// byte[] tmp = os.toByteArray();
// return BitmapFactory.decodeByteArray(tmp, 0, tmp.length);
// }
// return null;
// }
public static Bitmap nv12ToBitmap(byte[] data, int w, int h) {
return spToBitmap(data, w, h, 0, 1);
}
public static Bitmap nv21ToBitmap(byte[] data, int w, int h) {
return spToBitmap(data, w, h, 1, 0);
}
private static Bitmap spToBitmap(byte[] data, int w, int h, int uOff, int vOff) {
int plane = w * h;
int[] colors = new int[plane];
int yPos = 0, uvPos = plane;
for(int j = 0; j < h; j++) {
for(int i = 0; i < w; i++) {
// YUV byte to RGB int
final int y1 = data[yPos] & 0xff;
final int u = (data[uvPos + uOff] & 0xff) - 128;
final int v = (data[uvPos + vOff] & 0xff) - 128;
final int y1192 = 1192 * y1;
int r = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int b = (y1192 + 2066 * u);
r = (r < 0) ? 0 : ((r > 262143) ? 262143 : r);
g = (g < 0) ? 0 : ((g > 262143) ? 262143 : g);
b = (b < 0) ? 0 : ((b > 262143) ? 262143 : b);
colors[yPos] = ((r << 6) & 0xff0000) |
((g >> 2) & 0xff00) |
((b >> 10) & 0xff);
if((yPos++ & 1) == 1) uvPos += 2;
}
if((j & 1) == 0) uvPos -= w;
}
return Bitmap.createBitmap(colors, w, h, Bitmap.Config.RGB_565);
}
public static Bitmap i420ToBitmap(byte[] data, int w, int h) {
return pToBitmap(data, w, h, true);
}
public static Bitmap yv12ToBitmap(byte[] data, int w, int h) {
return pToBitmap(data, w, h, false);
}
private static Bitmap pToBitmap(byte[] data, int w, int h, boolean uv) {
int plane = w * h;
int[] colors = new int[plane];
int off = plane >> 2;
int yPos = 0, uPos = plane + (uv ? 0 : off), vPos = plane + (uv ? off : 0);
for(int j = 0; j < h; j++) {
for(int i = 0; i < w; i++) {
// YUV byte to RGB int
final int y1 = data[yPos] & 0xff;
final int u = (data[uPos] & 0xff) - 128;
final int v = (data[vPos] & 0xff) - 128;
final int y1192 = 1192 * y1;
int r = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int b = (y1192 + 2066 * u);
r = (r < 0) ? 0 : ((r > 262143) ? 262143 : r);
g = (g < 0) ? 0 : ((g > 262143) ? 262143 : g);
b = (b < 0) ? 0 : ((b > 262143) ? 262143 : b);
colors[yPos] = ((r << 6) & 0xff0000) |
((g >> 2) & 0xff00) |
((b >> 10) & 0xff);
if((yPos++ & 1) == 1) {
uPos++;
vPos++;
}
}
if((j & 1) == 0) {
uPos -= (w >> 1);
vPos -= (w >> 1);
}
}
return Bitmap.createBitmap(colors, w, h, Bitmap.Config.RGB_565);
}
public static int[] planesToColors(Image.Plane[] planes, int height) {
ByteBuffer yPlane = planes[0].getBuffer();
ByteBuffer uPlane = planes[1].getBuffer();
ByteBuffer vPlane = planes[2].getBuffer();
int bufferIndex = 0;
final int total = yPlane.capacity();
final int uvCapacity = uPlane.capacity();
final int width = planes[0].getRowStride();
int[] rgbBuffer = new int[width * height];
int yPos = 0;
for (int i = 0; i < height; i++) {
int uvPos = (i >> 1) * width;
for (int j = 0; j < width; j++) {
if (uvPos >= uvCapacity - 1)
break;
if (yPos >= total)
break;
final int y1 = yPlane.get(yPos++) & 0xff;
/*
The ordering of the u (Cb) and v (Cr) bytes inside the planes is a
bit strange. The _first_ byte of the u-plane and the _second_ byte
of the v-plane build the u/v pair and belong to the first two pixels
(y-bytes), thus usual YUV 420 behavior. What the Android devs did
here (IMHO): just copy the interleaved NV21 U/V data to two planes
but keep the offset of the interleaving.
*/
final int u = (uPlane.get(uvPos) & 0xff) - 128;
final int v = (vPlane.get(uvPos) & 0xff) - 128;
if ((j & 1) == 1) {
uvPos += 2;
}
// This is the integer variant to convert YCbCr to RGB, NTSC values.
// formulae found at
// https://software.intel.com/en-us/android/articles/trusted-tools-in-the-new-android-world-optimization-techniques-from-intel-sse-intrinsics-to
// and on StackOverflow etc.
final int y1192 = 1192 * y1;
int r = (y1192 + 1634 * v);
int g = (y1192 - 833 * v - 400 * u);
int b = (y1192 + 2066 * u);
r = (r < 0) ? 0 : ((r > 262143) ? 262143 : r);
g = (g < 0) ? 0 : ((g > 262143) ? 262143 : g);
b = (b < 0) ? 0 : ((b > 262143) ? 262143 : b);
rgbBuffer[bufferIndex++] = ((r << 6) & 0xff0000) |
((g >> 2) & 0xff00) |
((b >> 10) & 0xff);
}
}
return rgbBuffer;
}
/**
* 从ImageReader中获取byte[]数据
*/
public static ImageBytes getBytesFromImageReader(ImageReader imageReader) {
try (Image image = imageReader.acquireNextImage()) {
return getBytesFromImage(image);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
public static ImageBytes getBytesFromImage(Image image) {
final Image.Plane[] planes = image.getPlanes();
Image.Plane p0 = planes[0];
Image.Plane p1 = planes[1];
Image.Plane p2 = planes[2];
ByteBuffer b0 = p0.getBuffer();
ByteBuffer b1 = p1.getBuffer();
ByteBuffer b2 = p2.getBuffer();
int r0 = b0.remaining();
int r1 = b1.remaining();
int r2 = b2.remaining();
int w0 = p0.getRowStride();
int h0 = r0 / w0;
if(r0 % w0 > 0) h0++;
int w1 = p1.getRowStride();
int h1 = r1 / w1;
if(r1 % w1 > 1) h1++;
int w2 = p2.getRowStride();
int h2 = r2 / w2;
if(r2 % w2 > 2) h2++;
int y = w0 * h0;
int u = w1 * h1;
int v = w2 * h2;
byte[] bytes = new byte[y + u + v];
b0.get(bytes, 0, r0);
b1.get(bytes, y, r1); // u
b2.get(bytes, y + u, r2); // v
return new ImageBytes(bytes, w0, h0);
}
//yuv420转nv21
public static ByteBuffer yuv420ThreePlanesToNV21(Image.Plane[] yuv420888planes, int width, int height) {
int imageSize = width * height;
byte[] out = new byte[imageSize + 2 * (imageSize / 4)];
if (areUVPlanesNV21(yuv420888planes, width, height)) {
// 复制 Y 的值
yuv420888planes[0].getBuffer().get(out, 0, imageSize);
// 从 V 缓冲区获取第一个 V 值,因为 U 缓冲区不包含它。
yuv420888planes[2].getBuffer().get(out, imageSize, 1);
// 从 U 缓冲区复制第一个 U 值和剩余的 VU 值。
yuv420888planes[1].getBuffer().get(out, imageSize + 1, 2 * imageSize / 4 - 1);
} else {
// 回退到一个一个地复制 UV 值,这更慢但也有效。
// 取 Y.
unpackPlane(yuv420888planes[0], width, height, out, 0, 1);
// 取 U.
unpackPlane(yuv420888planes[1], width, height, out, imageSize + 1, 2);
// 取 V.
unpackPlane(yuv420888planes[2], width, height, out, imageSize, 2);
}
return ByteBuffer.wrap(out);
}
//是否nv21格式
public static boolean areUVPlanesNV21(Image.Plane[] planes, int width, int height) {
int imageSize = width * height;
ByteBuffer uBuffer = planes[1].getBuffer();
ByteBuffer vBuffer = planes[2].getBuffer();
// 备份缓冲区属性。
int vBufferPosition = vBuffer.position();
int uBufferLimit = uBuffer.limit();
// 将 V 缓冲区推进 1 个字节,因为 U 缓冲区将不包含第一个 V 值。
vBuffer.position(vBufferPosition + 1);
// 切掉 U 缓冲区的最后一个字节,因为 V 缓冲区将不包含最后一个 U 值。
uBuffer.limit(uBufferLimit - 1);
// 检查缓冲区是否相等并具有预期的元素数量。
boolean areNV21 = (vBuffer.remaining() == (2 * imageSize / 4 - 2)) && (vBuffer.compareTo(uBuffer) == 0);
// 将缓冲区恢复到初始状态。
vBuffer.position(vBufferPosition);
uBuffer.limit(uBufferLimit);
return areNV21;
}
public static void unpackPlane(Image.Plane plane, int width, int height, byte[] out, int offset, int pixelStride) {
ByteBuffer buffer = plane.getBuffer();
buffer.rewind();
// 计算当前平面的大小。假设它的纵横比与原始图像相同。
int numRow = (buffer.limit() + plane.getRowStride() - 1) / plane.getRowStride();
if (numRow == 0) {
return;
}
int scaleFactor = height / numRow;
int numCol = width / scaleFactor;
// 提取输出缓冲区中的数据。
int outputPos = offset;
int rowStart = 0;
for (int row = 0; row < numRow; row++) {
int inputPos = rowStart;
for (int col = 0; col < numCol; col++) {
out[outputPos] = buffer.get(inputPos);
outputPos += pixelStride;
inputPos += plane.getPixelStride();
}
rowStart += plane.getRowStride();
}
}
}
文件保存
import android.content.Context;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
public class save {
public static void saveFile(Context context,ByteBuffer nv21Buffer){
// 文件的保存路径
String filePath = "/sdcard/Mydata/"+"1024x576_test_"+System.currentTimeMillis()+".nv12";
Log.d("tag","保存地址为:"+filePath);
// 将ByteBuffer转换为byte数组
byte[] nv21Data = new byte[nv21Buffer.remaining()];
nv21Buffer.get(nv21Data);
// 使用FileOutputStream写入文件
try (FileOutputStream fos = new FileOutputStream(filePath)) {
fos.write(nv21Data);
// 如果需要,可以在这里添加更多的写入逻辑
} catch (IOException e) {
e.printStackTrace();
// 处理异常,比如显示一个错误消息
}
}
public static void saveFile1(Context context, ByteBuffer nv21Buffer) {
// 文件的保存路径
// 注意:确保你的应用有权限写入SD卡,并且路径存在
String filePath = "/sdcard/Mydata/" + "1024x576_test_" + 1 + ".nv21"; // 注意文件扩展名改为.nv21以匹配数据格式
Log.d("tag", "保存地址为:" + filePath);
// 确保目录存在
File dir = new File(filePath.substring(0, filePath.lastIndexOf('/')));
if (!dir.exists()) {
dir.mkdirs(); // 创建目录
}
// 将ByteBuffer转换为byte数组
byte[] nv21Data = new byte[nv21Buffer.remaining()];
nv21Buffer.get(nv21Data);
// 使用FileOutputStream写入文件,使用追加模式
try (FileOutputStream fos = new FileOutputStream(filePath, true)) { // 注意这里的true表示追加模式
fos.write(nv21Data);
// 如果需要,可以在这里添加更多的写入逻辑
} catch (IOException e) {
e.printStackTrace();
// 处理异常,比如显示一个错误消息
}
}
}