Skip to content

Commit c0d5dee

Browse files
committed
Add resize and mirror flip
1 parent b7f3c96 commit c0d5dee

File tree

2 files changed

+51
-5
lines changed

2 files changed

+51
-5
lines changed

README.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,12 @@ composer require vdechenaux/webcam
2424

2525
```php
2626
$webcam = new \VDX\Webcam\Webcam();
27+
28+
// It can produce an other size if your webcam does not support the provided size
29+
$webcam->setDesiredSize(1280, 720);
30+
2731
if ($webcam->open()) {
28-
$webcam->saveFrame('/tmp/test.jpg');
32+
$webcam->saveFrame('/tmp/test.jpg'/*, true*/); // It accepts a second parameter to mirror the image
2933
$webcam->close();
3034
}
3135
```

src/Webcam.php

Lines changed: 46 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -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);
2234
DEF;
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

Comments
 (0)