Rotate YUV420/NV21 Image in android(在android中旋转YUV420/NV21图像)
问题描述
在表面的 PreviewCall 中,我们在相机预览中获得了 YUV420SP 格式,但是由于该图像的错误旋转,我想正确旋转 YUV 图像,因为我需要通过网络发送它.所以需要应用正确的旋转.
In PreviewCall back of surface we are getting YUV420SP format in camera Preview but due to wrong rotation of that image I want to perform correct rotation of YUV image as I need to send it through network.so correct rotation need to be applied.
我发现这个链接可以正确旋转,但图像颜色变差了.
I found this link it does correct rotation but image loose the color.
http://www.wordsaretoys.com/2013/10/25/roll-that-camera-zombie-rotation-and-coversion-from-yv12-to-yuv420planar/
还检查了 在 Android 上旋转 YUV 字节数组 但它不能正确显示图像.
also checked Rotate an YUV byte array on Android but it does not show image properly.
我确实检查了 stckoverflow 上的链接,但他们都没有关于在 android 环境中正确使用代码的令人满意的答案.
I do have checked links on stckoverflow but none of them have satisfactory answer about correctly using the code in android environment.
有谁知道如何正确旋转 NV21 Image bytes[] 并正确保留其颜色信息.
do any one have idea how to correctly rotate NV21 Image bytes[] with retaining its color information correctly.
推荐答案
如果你只是想旋转 NV21,下面的代码会很有帮助.(我修改了代码 从这里)
If you just want to rotate NV21, following code will be helpful. (I modified the code from here)
public static void rotateNV21(byte[] input, byte[] output, int width, int height, int rotation) {
boolean swap = (rotation == 90 || rotation == 270);
boolean yflip = (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];
}
}
}
这篇关于在android中旋转YUV420/NV21图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:在android中旋转YUV420/NV21图像
基础教程推荐
- AdMob 广告未在模拟器中显示 2022-01-01
- Android:getLastKnownLocation(LocationManager.NETWORK_PROVIDER 2022-01-01
- 如何从 logcat 中删除旧数据? 2022-01-01
- libGDX 从精灵或纹理中获取像素颜色 2022-01-01
- iPhone - 获取给定地点/时区的当前日期和时间并将其与同一地点的另一个日期/时间进行比较的正确方法 2022-01-01
- iOS4 创建后台定时器 2022-01-01
- 通过重定向链接在 Google Play 中打开应用 2022-01-01
- Cocos2d iPhone 非矩形精灵触摸检测 2022-01-01
- navigator.geolocation.getCurrentPosition 在 Android 浏览器上 2022-01-01
- NSString intValue 不能用于检索电话号码 2022-01-01
