android - 在android中录制视频时旋转图像帧
问题描述
我想在视频录制时旋转图像帧,我找到了一种旋转图像字节数组的解决方案,如下面的代码。
private byte[] rotateYUV420Degree90(byte[] data, int imageWidth, int imageHeight) {
byte[] yuv = new byte[imageWidth * imageHeight * 3 / 2];
// Rotate the Y luma
int i = 0;
for (int x = 0; x < imageWidth; x++) {
for (int y = imageHeight - 1; y >= 0; y--) {
yuv[i] = data[y * imageWidth + x];
i++;
}
}
// Rotate the U and V color components
i = imageWidth * imageHeight * 3 / 2 - 1;
for (int x = imageWidth - 1; x > 0; x = x - 2) {
for (int y = 0; y < imageHeight / 2; y++) {
yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + x];
i--;
yuv[i] = data[(imageWidth * imageHeight) + (y * imageWidth) + (x - 1)];
i--;
}
}
return yuv;
}
如果有人有想法实现这一点,请帮助我旋转字节数组图像。
谢谢你。
解决方案
我找到了 NV12 方法的解决方案,通过我的这种方法已经实现了正确的输出,如果有人遇到同样的问题,请尝试下面的代码。
public byte[] rotateNV21(byte[] input, int width, int height, int rotation) {
byte[] output = new byte[input.length];
boolean swap = (rotation == 90 || rotation == 270);
// **EDIT:** in portrait mode & front cam this needs to be set to true:
boolean yflip = true;// (rotation == 90 || rotation == 180);
boolean xflip = (rotation == 270 || rotation == 180);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
int xo = x, yo = y;
int w = width, h = height;
int xi = xo, yi = yo;
if (swap) {
xi = w * yo / h;
yi = h * xo / w;
}
if (yflip) {
yi = h - yi - 1;
}
if (xflip) {
xi = w - xi - 1;
}
output[w * yo + xo] = input[w * yi + xi];
int fs = w * h;
int qs = (fs >> 2);
xi = (xi >> 1);
yi = (yi >> 1);
xo = (xo >> 1);
yo = (yo >> 1);
w = (w >> 1);
h = (h >> 1);
// adjust for interleave here
int ui = fs + (w * yi + xi) * 2;
int uo = fs + (w * yo + xo) * 2;
// and here
int vi = ui + 1;
int vo = uo + 1;
output[uo] = input[ui];
output[vo] = input[vi];
}
}
return output;
}
推荐阅读
- css - Fixed positioning not at the top of current page view
- c++ - 如何减少 constexpr 函数的编译时间?
- ios - 如何使用 Swift 通过用户输入保存音频录制文件?
- php - PHP页面 - 试图在浏览器上运行文件
- php - SEO 友好的 HTACCESS 301 重定向非 www 到 www PLUS .php 到 non-.php PLUS http 到 htps
- signal-processing - 零填充 - 在窗口之前还是之后?
- kubernetes - 更新 kubernetes_deployment 时出现 Terraform 错误
- css - 自定义扩展面板__标题高度
- sql - 可与持续交付管道集成的内存中数据库过程的 VS 单元测试
- php - 在 PHP MYSQL 中获取显示查询结果的按钮