Question

I try to save, through JNI, the output of the camera modified by OpenGL ES 2 on my tablet.

To achieve this, I use the libjpeg library compiled by the NDK-r8b.

I use the following code:

In the rendering function:

renderImage();
if (iIsPictureRequired)
{
  savePicture();
  iIsPictureRequired=false;
}

The saving procedure:

bool Image::savePicture()
{
 bool l_res =false;
char p_filename[]={"/sdcard/Pictures/testPic.jpg"};
// Allocates the image buffer (RGBA)
int l_size = iWidth*iHeight*4*sizeof(GLubyte);
GLubyte *l_image = (GLubyte*)malloc(l_size);
if (l_image==NULL)
{
  LOGE("Image::savePicture:could not allocate %d bytes",l_size);
  return l_res;
}
// Reads pixels from the color buffer (byte-aligned)
glPixelStorei(GL_PACK_ALIGNMENT, 1);
checkGlError("glPixelStorei");
// Saves the pixel buffer
glReadPixels(0,0,iWidth,iHeight,GL_RGBA,GL_UNSIGNED_BYTE,l_image);
checkGlError("glReadPixels");
// Stores the file
FILE* l_file  = fopen(p_filename, "wb");
if (l_file==NULL)
 {
   LOGE("Image::savePicture:could not create %s:errno=%d",p_filename,errno);
   free(l_image);
   return l_res;
 }
 // JPEG structures
 struct jpeg_compress_struct cinfo;
 struct jpeg_error_mgr       jerr;

 cinfo.err = jpeg_std_error(&jerr);
 jerr.trace_level = 10;

 jpeg_create_compress(&cinfo);
 jpeg_stdio_dest(&cinfo, l_file);
 cinfo.image_width      = iWidth;
 cinfo.image_height     = iHeight;
 cinfo.input_components = 3;
 cinfo.in_color_space   = JCS_RGB;
 jpeg_set_defaults(&cinfo);

 // Image quality [0..100]
 jpeg_set_quality (&cinfo, 70, true);
 jpeg_start_compress(&cinfo, true);

 // Saves the buffer
 JSAMPROW row_pointer[1];          // pointer to a single row

 // JPEG stores the image from top to bottom (OpenGL does the opposite)
 while (cinfo.next_scanline < cinfo.image_height)
{
  row_pointer[0] = (JSAMPROW)&l_image[(cinfo.image_height-1-cinfo.next_scanline)* (cinfo.input_components)*iWidth];
  jpeg_write_scanlines(&cinfo, row_pointer, 1);
}
// End of the process
jpeg_finish_compress(&cinfo);
fclose(l_file);
free(l_image);
l_res =true;
return l_res;

}

The display is correct but the generated JPEG seems tripled and overlap from left to right.

Scaled image (original size is 1210x648)

What did I do wrong ?

Was it helpful?

Solution

It appears that the internal format of the jpeg lib and the canvas do not match. Other appears to read/encode with RGBRGBRGB, other with RGBARGBARGBA.

You might be able to rearrange the image data, if everything else fails...

 char *dst_ptr = l_image; char *src_ptr = l_image;
 for (i=0;i<width*height;i++) { *dst_ptr++=*src_ptr++;
  *dst_ptr++=*src_ptr++; *dst_ptr++=*src_ptr++; src_ptr++; }

EDIT: now that the cause is verified, there might be even simpler modification. You might be able to get data from gl pixel buffer in the correct format:

 int l_size = iWidth*iHeight*3*sizeof(GLubyte);
 ...
 glReadPixels(0,0,iWidth,iHeight,GL_RGB,GL_UNSIGNED_BYTE,l_image);

And one more piece of warning: if this compiles, but the output is tilted, then it means that your screen width is not a multiple of 4, but that opengl wants to start each new row at dword boundary. But in that case there's a good opportunity of a crash, because in that case the l_size should have been 1,2 or 3 bytes larger than expected.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top