I'm experiencing a strange behaviour of my Camera Module v2 when using the raspicam API in my code. Here is what I've done:
I've downloaded the last version (0.1.3) of the raspicam API from https://sourceforge.net/projects/raspicam/files/
I've added the missing .h file at line 5 in src/CMakeLists.txt:
SET(public_hdrs_base raspicamtypes.h raspicam.h raspicam_still.h)
I've successfully compiled and installed the API
I've modified an example code as follows:
#include <fstream> #include <cstdlib> #include <iostream> #include "raspicam/raspicam_still.h" using namespace std; //Returns the value of a param. If not present, returns the defvalue float getParamVal ( string id,int argc,char **argv,float defvalue ) { for ( int i=0; i<argc; i++ ) if ( id== argv[i] ) return atof ( argv[i+1] ); return defvalue; } //prints program command line usage void usage() { cout<<"-w val : sets image width (648 default)"<<endl; cout<<"-h val : sets image height (486 default)"<<endl; cout<<"-iso val: set iso [100,800] (400 default)"<<endl; } int main ( int argc, char *argv[] ) { usage(); raspicam::RaspiCam_Still *Camera = new raspicam::RaspiCam_Still(); // int width = getParamVal ( "-w",argc,argv,2592 ); // int height =getParamVal ( "-h",argc,argv,1944 ); int width = getParamVal ( "-w",argc,argv,648 ); int height =getParamVal ( "-h",argc,argv,486 ); int iso=getParamVal ( "-iso",argc,argv,400); cout << "Initializing ..."<<width<<"x"<<height<<endl; Camera->setWidth ( width ); Camera->setHeight ( height ); Camera->setISO(iso); Camera->setEncoding ( raspicam::RASPICAM_ENCODING_BMP ); Camera->open(); cout<<"capture"<<endl; unsigned int length = Camera->getImageBufferSize(); // Header + Image Data + Padding unsigned char * data = new unsigned char[length]; if ( !Camera->grab_retrieve(data, length) ) { cerr<<"Error in grab"<<endl; return -1; } cout<<"saving picture.bmp"<<endl; ofstream file ( "picture.bmp",ios::binary ); file.write ( ( char* ) data, length ); delete Camera; return 0; }
I've compiled it using:
g++ simpletest_raspicam.cpp -o simpletest_raspicam -I/usr/local/include -L/opt/vc/lib -lraspicam -lmmal -lmmal_core -lmmal_util
I can run the executable and I get picture.bmp as output
If I remove the picture.bmp and I try to get a picture again, my app can't reach the end and gets stucked as if some resources were not freed after the first run. To get another picture the only option is to reboot my RPi.
root : ~/tmp> ./simpletest_raspicam -w val : sets image width (648 default) -h val : sets image height (486 default) -iso val: set iso [100,800] (400 default) Initializing ...648x486 capture saving picture.bmp root : ~/tmp> rm picture.bmp root : ~/tmp> ./simpletest_raspicam -w val : sets image width (648 default) -h val : sets image height (486 default) -iso val: set iso [100,800] (400 default) Initializing ...648x486
This issue is not reproducible using raspistill; I can run it all the times I want:
root : ~/tmp> raspistill -w 648 -h 486 -e bmp -o test1.bmp root : ~/tmp> raspistill -w 648 -h 486 -e bmp -o test2.bmp root : ~/tmp> raspistill -w 648 -h 486 -e bmp -o test3.bmp root : ~/tmp> ll -rwxr-xr-x 1 root 45K Jan 8 22:20 simpletest_raspicam -rw-r--r-- 1 root 1.6K Jan 8 22:20 simpletest_raspicam.cpp -rw-r--r-- 1 root 923K Jan 8 22:35 test1.bmp -rw-r--r-- 1 root 923K Jan 8 22:35 test2.bmp -rw-r--r-- 1 root 923K Jan 8 22:35 test3.bmp
Any idea on what I am missing?