@@ -6,19 +6,31 @@ class Webcam
66{
77 private \FFI $ ffi ;
88 private ?\FFI \CData $ cvCamera = null ;
9+ private ?int $ desiredWidth = null ;
10+ private ?int $ desiredHeight = null ;
911
1012 /**
1113 * @throws \FFI\Exception If OpenCV is not available
1214 */
1315 public function __construct ()
1416 {
1517 $ def = <<<DEF
18+ enum {
19+ CV_CAP_ANY = 0
20+ };
21+ enum {
22+ CV_CAP_PROP_FRAME_WIDTH = 3,
23+ CV_CAP_PROP_FRAME_HEIGHT = 4
24+ };
25+
1626 typedef struct CvCapture CvCapture;
1727 typedef struct IplImage IplImage;
1828 CvCapture* cvCreateCameraCapture(int index);
1929 IplImage* cvQueryFrame(CvCapture* capture);
2030 int cvSaveImage(const char *filename, const IplImage *image);
2131 void cvReleaseCapture (CvCapture **capture);
32+ int cvSetCaptureProperty(CvCapture* capture, int property_id, double value);
33+ void cvFlip(const IplImage* src, IplImage* dst, int flip_mode);
2234DEF ;
2335
2436 $ this ->ffi = \FFI ::cdef ($ def , "libopencv_videoio.so " );
@@ -29,15 +41,35 @@ public function __destruct()
2941 $ this ->close ();
3042 }
3143
44+ public function setDesiredSize (int $ desiredWidth , int $ desiredHeight ): void
45+ {
46+ $ this ->desiredWidth = $ desiredWidth ;
47+ $ this ->desiredHeight = $ desiredHeight ;
48+
49+ if ($ this ->cvCamera !== null ) {
50+ $ this ->ffi ->cvSetCaptureProperty ($ this ->cvCamera , $ this ->ffi ->CV_CAP_PROP_FRAME_WIDTH , $ this ->desiredWidth );
51+ $ this ->ffi ->cvSetCaptureProperty ($ this ->cvCamera , $ this ->ffi ->CV_CAP_PROP_FRAME_HEIGHT , $ this ->desiredHeight );
52+ }
53+ }
54+
3255 public function open (): bool
3356 {
3457 if ($ this ->cvCamera !== null ) {
3558 return false ;
3659 }
3760
38- $ this ->cvCamera = $ this ->ffi ->cvCreateCameraCapture (0 );
61+ $ this ->cvCamera = $ this ->ffi ->cvCreateCameraCapture ($ this ->ffi ->CV_CAP_ANY );
62+
63+ if ($ this ->cvCamera === null ) {
64+ return false ;
65+ }
66+
67+ if ($ this ->desiredWidth !== null && $ this ->desiredHeight !== null ) {
68+ $ this ->ffi ->cvSetCaptureProperty ($ this ->cvCamera , $ this ->ffi ->CV_CAP_PROP_FRAME_WIDTH , $ this ->desiredWidth );
69+ $ this ->ffi ->cvSetCaptureProperty ($ this ->cvCamera , $ this ->ffi ->CV_CAP_PROP_FRAME_HEIGHT , $ this ->desiredHeight );
70+ }
3971
40- return $ this -> cvCamera !== null ;
72+ return true ;
4173 }
4274
4375 public function close (): void
@@ -50,12 +82,22 @@ public function close(): void
5082 $ this ->cvCamera = null ;
5183 }
5284
53- public function saveFrame (string $ filename ): bool
85+ public function saveFrame (string $ filename, bool $ mirror = false ): bool
5486 {
5587 if ($ this ->cvCamera === null ) {
5688 return false ;
5789 }
5890
59- return (bool ) $ this ->ffi ->cvSaveImage ($ filename , $ this ->ffi ->cvQueryFrame ($ this ->cvCamera ));
91+ $ image = $ this ->ffi ->cvQueryFrame ($ this ->cvCamera );
92+
93+ if ($ image === null ) {
94+ return false ;
95+ }
96+
97+ if ($ mirror ) {
98+ $ this ->ffi ->cvFlip ($ image , null , 1 );
99+ }
100+
101+ return (bool ) $ this ->ffi ->cvSaveImage ($ filename , $ image );
60102 }
61103}
0 commit comments