huanganhen / javacv

Automatically exported from code.google.com/p/javacv
GNU General Public License v2.0
0 stars 0 forks source link

BufferOverflowException caused by data get from camera(android) is larger than the IplImage buffer size #438

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?
1.Following the sample "record video on android" - 
https://code.google.com/p/javacv/source/browse/samples/RecordActivity.java
2.Made some change, refactor the code
3.Key code as below
yuvIplimage = IplImage.create(imageWidth, imageHeight,
                opencv_core.IPL_DEPTH_8U, 2);
//...
camParams.setPictureSize(imageWidth, imageHeight);
//And I'm sure the the image width/height in above two places are the same(both 
640*480)
public void onPreviewFrame(byte[] data, Camera camera) {
        /* get video data */
        if (yuvIplimage != null && recording) {
            int limit = yuvIplimage.getByteBuffer().limit();//614400
            int size = data.length;//1382400
            yuvIplimage.getByteBuffer().put(data); //Here thrown a BufferOverflowException

    }

What is the expected output? What do you see instead?
Expect bytebuffer size of yuvIplimage can meet the data get from camera.

What version of the product are you using? On what operating system?
javacv 0.7
Android 4.1.3

Please provide any additional information below.

Original issue reported on code.google.com by htp2...@gmail.com on 28 Feb 2014 at 4:35

GoogleCodeExporter commented 9 years ago
Could you also please provide the `width` and `height` that you get from 
`camera.getParameters().getPreviewSize()`? Thanks

Original comment by samuel.a...@gmail.com on 1 Mar 2014 at 10:57

GoogleCodeExporter commented 9 years ago
[deleted comment]
GoogleCodeExporter commented 9 years ago
Preview size, 1280*720
Picture size, 640*480

Original comment by htp2...@gmail.com on 2 Mar 2014 at 3:23

GoogleCodeExporter commented 9 years ago
Ok, so 1280*720*1.5 = 1382400 => The data your are getting has width=1280 and 
height=720, so please allocate an IplImage of that size.

And please ask your questions on the mailing list next time, if possible, 
thanks!

Original comment by samuel.a...@gmail.com on 2 Mar 2014 at 3:47

GoogleCodeExporter commented 9 years ago
Hi Samuel,
Why is not the data size determined by picture size rather than preview size?
Thanks. 

Original comment by htp2...@gmail.com on 2 Mar 2014 at 3:55

GoogleCodeExporter commented 9 years ago
Hi Samuel,
Why is not the data size determined by picture size rather than preview size? 
And why is the formula with a factor "1.5"? 
Should I allocate a IplImage as below?
yuvIplimage = IplImage.create(1280, 720,
                opencv_core.IPL_DEPTH_8U, 2);

And thanks for the great library you created.

Thanks. 

Original comment by htp2...@gmail.com on 2 Mar 2014 at 4:03

GoogleCodeExporter commented 9 years ago
1. I'm guessing it's because your device doesn't support 640x480. But I'm not 
the one you should be asking.

2. OpenCV wasn't designed to support YUV data, so yes that's how we need to 
allocate it, for now. This is just an ugly hack.

Original comment by samuel.a...@gmail.com on 2 Mar 2014 at 4:08