I am trying to build an AR app and would like to display the image captured from the camera on an opengl plane.The source file on the link :http://nhenze.net/?p=172 shows a texture which was created from the YUV byte array as captured from the camera.I tried to convert the YUV texture provided by OpenCV into RGB using the Img.cvt color and changed the glTexImage2D parameters but that did not work .I then looked at another post http://nhenze.net/?p=253 and read about how to convert the YUV to RGB .However,this app exits abnormally when I try it on the Google Nexus -s .The code on the first link compiles fine though.
My code snippets are :
Using java Syntax Highlighting
- void bindCameraTexture(GL10 gl) {
- synchronized(this) {
- if (cameraTexture==null)
- cameraTexture=new int[1];
- else
- gl.glDeleteTextures(1, cameraTexture, 0);
- gl.glGenTextures(1, cameraTexture, 0);
- int tex = cameraTexture[0];
- gl.glBindTexture(GL10.GL_TEXTURE_2D, tex);
- gl.glTexImage2D(GL10.GL_TEXTURE_2D, 0, GL10.GL_LUMINANCE, 256, 256,0, GL10.GL_LUMINANCE, GL10.GL_UNSIGNED_BYTE,bytebuffer);
- gl.glTexParameterf(GL10.GL_TEXTURE_2D, GL10.GL_TEXTURE_MIN_FILTER, GL10.GL_LINEAR);
- }
- public void onPreviewFrame(byte[] yuvs, Camera camera)
- {
- int bwCounter=0;
- int yuvsCounter=0;
- byteBuffer = ByteBuffer.allocateDirect(yuvs.length);
- imageconversion(yuvs,240,160,byteBuffer,240,160); // Camera Preview size is 240*160 ,imageconversion is the native function.,.
- }
Parsed in 0.033 seconds, using GeSHi 1.0.8.4
and on the Native side .... :
Using cpp Syntax Highlighting
- void toRGB565(unsigned short *yuvs, int widthIn, int heightIn, unsigned int *rgbs, int widthOut, int heightOut)
- {
- int half_widthIn = widthIn >> 1;
- //the end of the luminance data
- int lumEnd = (widthIn * heightIn) >> 1;
- //points to the next luminance value pair
- int lumPtr = 0;
- //points to the next chromiance value pair
- int chrPtr = lumEnd;
- //the end of the current luminance scanline
- int lineEnd = half_widthIn;
- int x,y;
- for (y=0;y<heightIn;y++)
- {
- int yPosOut=(y*widthOut) >> 1;
- for (x=0;x<half_widthIn;x++)
- {
- //read the luminance and chromiance values
- int Y1 = yuvs[lumPtr++];
- int Y2 = (Y1 >> 8 ) & 0xff;
- Y1 = Y1 & 0xff;
- int Cr = yuvs[chrPtr++];
- int Cb = ((Cr >> 8 ) & 0xff) - 128;
- Cr = (Cr & 0xff) - 128;
- int R, G, B;
- //generate first RGB components
- B = Y1 + ((454 * Cb) >> 8 ) ;
- if (B < 0) B = 0; if (B > 255) B = 255;
- G = Y1 - (( 88 * Cb + 183 * Cr) >> 8 ) ;
- if (G < 0) G = 0; if (G > 255) G = 255;
- R = Y1 + ((359 * Cr) >> 8 ) ;
- if (R < 0) R = 0; if (R > 255) R = 255;
- int val = ((R & 0xf8) << 8 )| ((G & 0xfc) << 3) | (B >> 3) ;
- //generate second RGB components
- B = Y1 + ((454 * Cb) >> 8 );
- if (B < 0) B = 0; if (B > 255) B = 255;
- G = Y1 - ((88 * Cb + 183 * Cr) > > 8 );
- if (G < 0) G = 0; if (G > 255) G = 255;
- R = Y1 + ((359 * Cr) > > 8 ) ;
- if (R < 0) R = 0; if (R > 255) R = 255;
- rgbs[yPosOut+x] = val | ((((R & 0xf8) << 8 ) | ((G & 0xfc) << 3) | (B >> 3)) << 16);
- }
- //skip back to the start of the chromiance values when necessary
- chrPtr = lumEnd + ((lumPtr >> 1) / half_widthIn) * half_widthIn;
- lineEnd += half_widthIn;
- }
- }
- JNIEXPORT void JNICALL Java_com_abc_android_GLLayer_imageconversion (JNIEnv *env, jclass clazz,jbyteArray imageIn, jint widthIn, jint heightIn, jobject imageOut, jint widthOut, jint heightOut,jint filter)
- {
- jbyte *cImageIn = (env)->GetByteArrayElements(imageIn, NULL);
- jbyte *cImageOut = (jbyte*)(env)->GetDirectBufferAddress(imageOut);
- toRGB565((unsigned short*)cImageIn, widthIn, heightIn, (unsigned int*)cImageOut, widthOut, heightOut);
- (env)->ReleaseByteArrayElements(imageIn, cImageIn,0);
- }
Parsed in 0.016 seconds, using GeSHi 1.0.8.4
Can anyone please tell me why the app exits with an error.If I comment the line
// rgbs[yPosOut+x] = val | ((((R & 0xf8) << 8 ) | ((G & 0xfc) << 3) | (B >> 3)) << 16);
The program does not exit .I do not understand y the program exits when it reaches this line .Please can anyone give any ideas what could be the error.
Thanks a lot !
Cheers,
Rahul

