gpt4 book ai didi

android - 使用 NDK 在 C/C++ 中将 YUV 解码为 RGB

转载 作者:太空宇宙 更新时间:2023-11-03 11:45:22 24 4
gpt4 key购买 nike

我正在尝试将 Android 相机提要转换为位图以进行图像处理。

我有一些代码可以在 native java 中将 YUV 转换为 RGB,但是,这个过程对于实时视频来说不够快,所以我认为我需要在应用过滤器之前用 C 或 C++ 转换它。我已经设置并运行了 NDK,所以我唯一不知道该怎么做的是将以下代码移植到 C 或 C++:

// decode Y, U, and V values on the YUV 420 buffer described as YCbCr_422_SP by Android 
// David Manpearl 081201
public void decodeYUV(int[] out, byte[] fg, int width, int height)
throws NullPointerException, IllegalArgumentException {
int sz = width * height;
if (out == null)
throw new NullPointerException("buffer out is null");
if (out.length < sz)
throw new IllegalArgumentException("buffer out size " + out.length
+ " < minimum " + sz);
if (fg == null)
throw new NullPointerException("buffer 'fg' is null");
if (fg.length < sz)
throw new IllegalArgumentException("buffer fg size " + fg.length
+ " < minimum " + sz * 3 / 2);
int i, j;
int Y, Cr = 0, Cb = 0;
for (j = 0; j < height; j++) {
int pixPtr = j * width;
final int jDiv2 = j >> 1;
for (i = 0; i < width; i++) {
Y = fg[pixPtr];
if (Y < 0)
Y += 255;
if ((i & 0x1) != 1) {
final int cOff = sz + jDiv2 * width + (i >> 1) * 2;
Cb = fg[cOff];
if (Cb < 0)
Cb += 127;
else
Cb -= 128;
Cr = fg[cOff + 1];
if (Cr < 0)
Cr += 127;
else
Cr -= 128;
}
int R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5);
if (R < 0)
R = 0;
else if (R > 255)
R = 255;
int G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1)
+ (Cr >> 3) + (Cr >> 4) + (Cr >> 5);
if (G < 0)
G = 0;
else if (G > 255)
G = 255;
int B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6);
if (B < 0)
B = 0;
else if (B > 255)
B = 255;
out[pixPtr++] = 0xff000000 + (B << 16) + (G << 8) + R;
}
}

}

...

decodeYUV(argb8888, data, camSize.width, camSize.height);
Bitmap bitmap = Bitmap.createBitmap(argb8888, camSize.width,
camSize.height, Config.ARGB_8888);

有人知道怎么做吗?

非常感谢!


更新

这是我得到的结果:

JNIEXPORT void JNICALL Java_com_twothreetwo_zoomplus_ZoomPlus_YUVtoRGB(JNIEnv * env, jobject obj, jintArray rgb, jbyteArray yuv420sp, jint width, jint height)
{
int sz;
int i;
int j;
int Y;
int Cr = 0;
int Cb = 0;
int pixPtr = 0;
int jDiv2 = 0;
int R = 0;
int G = 0;
int B = 0;
int cOff;

sz = width * height;
//if(out == null) throw new NullPointerException("buffer 'out' is null");
//if(out.length < sz) throw new IllegalArgumentException("buffer 'out' size " + out.length + " < minimum " + sz);
//if(fg == null) throw new NullPointerException("buffer 'fg' is null");
//if(fg.length < sz) throw new IllegalArgumentException("buffer 'fg' size " + fg.length + " < minimum " + sz * 3/ 2);
for(j = 0; j < height; j++) {
pixPtr = j * width;
jDiv2 = j >> 1;
for(i = 0; i < width; i++) {
Y = yuv420sp[pixPtr]; if(Y < 0) Y += 255;
if((i & 0x1) != 1) {
cOff = sz + jDiv2 * width + (i >> 1) * 2;
Cb = yuv420sp[cOff];
if(Cb < 0) Cb += 127; else Cb -= 128;
Cr = yuv420sp[cOff + 1];
if(Cr < 0) Cr += 127; else Cr -= 128;
}
R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5);
if(R < 0) R = 0; else if(R > 255) R = 255;
G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1) + (Cr >> 3) + (Cr >> 4) + (Cr >> 5);
if(G < 0) G = 0; else if(G > 255) G = 255;
B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6);
if(B < 0) B = 0; else if(B > 255) B = 255;
rgb[pixPtr++] = 0xff000000 + (B << 16) + (G << 8) + R;
}
}

}

但我收到以下 C 错误:

apps/zoomplusndk/jni/zoomplusndk.c:53: warning: dereferencing 'void *' pointer
apps/zoomplusndk/jni/zoomplusndk.c:53: error: void value not ignored as it ought to be
apps/zoomplusndk/jni/zoomplusndk.c:56: warning: dereferencing 'void *' pointer
apps/zoomplusndk/jni/zoomplusndk.c:56: error: void value not ignored as it ought to be
apps/zoomplusndk/jni/zoomplusndk.c:58: warning: dereferencing 'void *' pointer
apps/zoomplusndk/jni/zoomplusndk.c:58: error: void value not ignored as it ought to be
apps/zoomplusndk/jni/zoomplusndk.c:67: warning: dereferencing 'void *' pointer
apps/zoomplusndk/jni/zoomplusndk.c:67: error: invalid use of void expression

第53行是Y = yuv420sp[pixPtr];如果(Y < 0) Y += 255;

最佳答案

这里是你的代码更正:

#include <jni.h>
#include <android/log.h>

int* rgbData;
int rgbDataSize = 0;

JNIEXPORT void JNICALL Java_mk_g6_transparency_CameraPreview_YUVtoRBG(JNIEnv * env, jobject obj, jintArray rgb, jbyteArray yuv420sp, jint width, jint height)
{
int sz;
int i;
int j;
int Y;
int Cr = 0;
int Cb = 0;
int pixPtr = 0;
int jDiv2 = 0;
int R = 0;
int G = 0;
int B = 0;
int cOff;
int w = width;
int h = height;
sz = w * h;

jbyte* yuv = yuv420sp;
if(rgbDataSize < sz) {
int tmp[sz];
rgbData = &tmp[0];
rgbDataSize = sz;
__android_log_write(ANDROID_LOG_INFO, "JNI", "alloc");
}

for(j = 0; j < h; j++) {
pixPtr = j * w;
jDiv2 = j >> 1;
for(i = 0; i < w; i++) {
Y = yuv[pixPtr];
if(Y < 0) Y += 255;
if((i & 0x1) != 1) {
cOff = sz + jDiv2 * w + (i >> 1) * 2;
Cb = yuv[cOff];
if(Cb < 0) Cb += 127; else Cb -= 128;
Cr = yuv[cOff + 1];
if(Cr < 0) Cr += 127; else Cr -= 128;
}
R = Y + Cr + (Cr >> 2) + (Cr >> 3) + (Cr >> 5);
if(R < 0) R = 0; else if(R > 255) R = 255;
G = Y - (Cb >> 2) + (Cb >> 4) + (Cb >> 5) - (Cr >> 1) + (Cr >> 3) + (Cr >> 4) + (Cr >> 5);
if(G < 0) G = 0; else if(G > 255) G = 255;
B = Y + Cb + (Cb >> 1) + (Cb >> 2) + (Cb >> 6);
if(B < 0) B = 0; else if(B > 255) B = 255;
rgbData[pixPtr++] = 0xff000000 + (B << 16) + (G << 8) + R;
}
}
(*env)->SetIntArrayRegion(env, rgb, 0, sz, ( jint * ) &rgbData[0] );
}

它也在 http://dl.dropbox.com/u/49855874/yuv-decoder.c 在线

我的测试:

  • 在 HTC Desire 上以 640x480 处理 ~20ms 的相机预览
  • 在 HTC Desire 上以 320x240 处理 ~6ms 的相机预览

关于android - 使用 NDK 在 C/C++ 中将 YUV 解码为 RGB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8340128/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com