Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the buffer size for callbackbuffer of Android Camera need to be 8 time as big as the image?

Tags:

android

camera

As the Android document said:

For formats besides YV12, the size of the buffer is determined by multiplying the preview image width, height, and bytes per pixel. The width and height can be read from getPreviewSize(). Bytes per pixel can be computed from getBitsPerPixel(int) / 8, using the image format from getPreviewFormat().

But most of online code using bitsperpixel to create buffer instead of the byteperpixel=bitsperpixel/8.

If I use following code with use the exact size in bytes of the image, will result in an error: E/Camera-JNI(3656): Callback buffer was too small! Expected 1336320 bytes, but got 890880 bytes! Why is that? Why the buffer need to be 8 time as big as the image size?

Camera.Parameters parameters=mCamera.getParameters();
parameters.setPreviewSize(width,height);
mCamera.setParameters(parameters);

int previewFormat=parameters.getPreviewFormat();
int bitsperpixel=ImageFormat.getBitsPerPixel(previewFormat);
int byteperpixel=bitsperpixel/8;
Camera.Size camerasize=parameters.getPreviewSize();
int frame_bytesize=((camerasize.width*camerasize.height)*byteperpixel);

//create buffer
byte[]frameBuffer=new byte[frame_bytesize];

//buffer registry 
mCamera.addCallbackBuffer(frameBuffer);
like image 346
Wang Avatar asked Dec 19 '25 02:12

Wang


1 Answers

1336320 is 1.5 X 890880, so I'd imagine bitsperpixel == 12, and in using int for bytesperpixel you are losing the remainder. e.g.

int bytesperpixel = 12 / 8

Will result in 1, not the 1.5 you need.

like image 127
Ryan Avatar answered Dec 21 '25 14:12

Ryan