Skip to content

Commit bc4ba2c

Browse files
committed
Merge branch 'fix-jpeg-signatures' into 'master'
Fix camera jpeg signatures See merge request meat-n-potatoes/pi-camera-connect!2
2 parents b1593be + 29d4559 commit bc4ba2c

File tree

6 files changed

+190
-175
lines changed

6 files changed

+190
-175
lines changed

package.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
"test": "jest --runInBand",
2323
"prepublishOnly": "yarn clean && yarn build"
2424
},
25+
"dependencies": {
26+
"systeminformation": "4"
27+
},
2528
"devDependencies": {
2629
"@lcdev/eslint-config": "0.2",
2730
"@lcdev/jest": "0.2",

src/lib/still-camera.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@ test('takeImage() returns JPEG', async () => {
55

66
const jpegImage = await stillCamera.takeImage();
77

8-
expect(jpegImage.indexOf(StillCamera.jpegSignature)).toBe(0);
8+
expect(jpegImage.indexOf(await StillCamera.getJpegSignature())).toBe(0);
99
});

src/lib/still-camera.ts

Lines changed: 16 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import * as si from 'systeminformation';
2+
import { Flip, Rotation } from '..';
13
import { spawnPromise } from '../util';
2-
import { Rotation, Flip } from '..';
34

45
export interface StillOptions {
56
width?: number;
@@ -10,20 +11,6 @@ export interface StillOptions {
1011
}
1112

1213
export default class StillCamera {
13-
static readonly jpegSignature = Buffer.from([
14-
0xff,
15-
0xd8,
16-
0xff,
17-
0xe1,
18-
0x64,
19-
0x1a,
20-
0x45,
21-
0x78,
22-
0x69,
23-
0x66,
24-
0x00,
25-
]);
26-
2714
private readonly options: StillOptions;
2815

2916
constructor(options: StillOptions = {}) {
@@ -35,6 +22,20 @@ export default class StillCamera {
3522
};
3623
}
3724

25+
static async getJpegSignature() {
26+
const systemInfo = await si.system();
27+
switch (systemInfo.model) {
28+
case 'BCM2835 - Pi 3 Model B':
29+
case 'BCM2835 - Pi 3 Model B+':
30+
case 'BCM2835 - Pi 4 Model B':
31+
return Buffer.from([0xff, 0xd8, 0xff, 0xe1]);
32+
default:
33+
throw new Error(
34+
`Could not determine JPEG signature. Unknown system model '${systemInfo.model}'`,
35+
);
36+
}
37+
}
38+
3839
async takeImage() {
3940
try {
4041
return await spawnPromise('raspistill', [

src/lib/stream-camera.test.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ test('Method takeImage() grabs JPEG from MJPEG stream', async () => {
1111

1212
await streamCamera.stopCapture();
1313

14-
expect(jpegImage.indexOf(StreamCamera.jpegSignature)).toBe(0);
14+
expect(jpegImage.indexOf(await StreamCamera.getJpegSignature())).toBe(0);
1515
});
1616

1717
test('Method createStream() returns a stream of video data', async () => {

0 commit comments

Comments
 (0)