我正在调用我的本机函数,该函数使用来自ANativeWindow API
线程的java代码中的UI
,如下所示:
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// your stuff to update the UI
mainFunction(3,inputFile,outputFile,surface);
Log.d(TAG, "Thread executed successfully");
}
});
然后在本机代码中将数据从rgba缓冲区复制到位时,应用程序正在运行。本机代码段如下所示:
void yuv2rgba(UINT8* y,UINT8* u,UINT8* v,int w, int h,FILE *fp_Out_File,jobject surface)
{
int mem1=w*h,i=0,j=0,k=0;
__android_log_print(ANDROID_LOG_DEBUG,"MYDECODER","yuv2rgba is entered.....",NULL);
JNIEnv *env;
ANativeWindow* window;
ANativeWindow_Buffer buffer;
//buffer1: holds the rgba data
UINT8 *buffer1=(UINT8*)malloc(sizeof(UINT8)*(4*mem1));
UINT8 *r=(UINT8*)malloc(sizeof(UINT8)*mem1);
UINT8 *g=(UINT8*)malloc(sizeof(UINT8)*mem1);
UINT8 *b=(UINT8*)malloc(sizeof(UINT8)*mem1);
int y1,u1,v1,rcomp,gcomp,bcomp;
int getEnvStat = (*g_vm)->GetEnv(g_vm,(void **)&env, JNI_VERSION_1_6);
if (getEnvStat == JNI_EDETACHED) {
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","GetEnv: not attached.....",NULL);
}
else if (getEnvStat == JNI_OK) {
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","getEnvStat == JNI_OK.....",NULL);
}
else if (getEnvStat == JNI_EVERSION) {
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","GetEnv: version not supported.....",NULL);
}
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","computing rgb.....",NULL);
for(i=0;i<mem1;++i)
{
y1=y[i]-16;
u1=u[i]-128;
v1=v[i]-128;
rcomp=((298 * y1) +( 409 * v1))>>8;
r[i]=(UINT8)((rcomp<0)?0:((rcomp > 255)?255:rcomp));
gcomp=((298 * y1) - (100 *u1) - (208 * v1))>>8;
g[i]=(UINT8)((gcomp<0)?0:((gcomp > 255)?255:gcomp));
bcomp=((298 * y1) + (516 * u1))>>8;
b[i]=(UINT8)((bcomp<0)?0:((bcomp > 255)?255:bcomp));
}
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","assigning rgba to buffer....",NULL);
// Storing the RGB value into a buffer
for(i=0,j=0;(i<(mem1*4))&&(j<mem1);i=i+4,++j)
{
buffer1[i]=r[j];
buffer1[i+1]=g[j];
buffer1[i+2]=b[j];
buffer1[i+3]=0xff;
}
free(r);
free(g);
free(b);
//fwrite to an output file
//fwrite(buffer1,sizeof(UINT8),w*h*3,fp_Out_File);
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","before creating a window.....",NULL);
window = ANativeWindow_fromSurface(env, surface);
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","window created.....",NULL);
if (ANativeWindow_lock(window, &buffer, NULL) == 0) {
memcpy(buffer.bits, buffer1, sizeof(UINT8)*(4*mem1));
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","memcpy successfull.....",NULL);
ANativeWindow_unlockAndPost(window);
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","unlock and post successful..." ,NULL);
}
ANativeWindow_release(window);
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","release successful.....",NULL);
free(buffer1);
}
运行应用程序时,应用程序在评论后崩溃:“窗口创建....”。以及logcat中的以下消息:
02-18 14:46:55.344: I/dalvikvm(2080): threadid=4: reacting to signal 3
02-18 14:46:55.348: I/dalvikvm(2080): Wrote stack traces to '/data/anr/traces.txt'
在上面的代码中,我的目标是使用buffer1
API <{1}}在设备屏幕上显示ANativeWindow
中的rgba数据
答案 0 :(得分:1)
最后 - 经过大量的试验和错误 - 我能够找出崩溃。
原因在上面的代码之前:
if (ANativeWindow_lock(window, &buffer, NULL) == 0) {
memcpy(buffer.bits, buffer1, sizeof(UINT8)*(4*mem1));
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","memcpy successfull.....",NULL);
ANativeWindow_unlockAndPost(window);
__android_log_print(ANDROID_LOG_DEBUG,"MYAPP","unlock and post successful..." ,NULL);
}
我们需要添加:
ANativeWindow_setBuffersGeometry(window,w,h,WINDOW_FORMAT_RGBA_8888)
其中,w
- &gt;框架的宽度h
- &gt;帧的高度和RGBA格式。