|
1 | | -import { spawnPromise } from "../util"; |
2 | | -import { Rotation, Flip } from ".."; |
| 1 | +import { spawnPromise } from '../util'; |
| 2 | +import { Rotation, Flip } from '..'; |
3 | 3 |
|
4 | 4 | export interface StillOptions { |
5 | | -width?: number; |
6 | | -height?: number; |
7 | | -rotation?: Rotation; |
8 | | -flip?: Flip; |
9 | | -delay?: number; |
| 5 | + width?: number; |
| 6 | + height?: number; |
| 7 | + rotation?: Rotation; |
| 8 | + flip?: Flip; |
| 9 | + delay?: number; |
10 | 10 | } |
11 | 11 |
|
12 | 12 | export default class StillCamera { |
13 | | - |
14 | | -static readonly jpegSignature = Buffer.from([0xFF, 0xD8, 0xFF, 0xE1, 0x64, 0x1A, 0x45, 0x78, 0x69, 0x66, 0x00]); |
15 | | - |
16 | | -private options: StillOptions; |
17 | | - |
18 | | -constructor(options: StillOptions = {}) { |
19 | | - |
20 | | -this.options = { |
21 | | -rotation: Rotation.Rotate0, |
22 | | -flip: Flip.None, |
23 | | -delay: 1, |
24 | | -...options |
25 | | -}; |
26 | | -} |
27 | | - |
28 | | -async takeImage() { |
29 | | - |
30 | | -try { |
31 | | - |
32 | | -return await spawnPromise("raspistill", [ |
33 | | - |
34 | | -/** |
35 | | - * Width |
36 | | - */ |
37 | | -...(this.options.width ? ["--width", this.options.width.toString()] : []), |
38 | | - |
39 | | -/** |
40 | | - * Height |
41 | | - */ |
42 | | -...(this.options.height ? ["--height", this.options.height.toString()] : []), |
43 | | - |
44 | | -/** |
45 | | - * Rotation |
46 | | - */ |
47 | | -...(this.options.rotation ? ["--rotation", this.options.rotation.toString()] : []), |
48 | | - |
49 | | -/** |
50 | | - * Horizontal flip |
51 | | - */ |
52 | | -...(this.options.flip && (this.options.flip === Flip.Horizontal || this.options.flip === Flip.Both) ? |
53 | | -["--hflip"] : []), |
54 | | - |
55 | | -/** |
56 | | - * Vertical flip |
57 | | - */ |
58 | | -...(this.options.flip && (this.options.flip === Flip.Vertical || this.options.flip === Flip.Both) ? |
59 | | -["--vflip"] : []), |
60 | | - |
61 | | -/** |
62 | | - * Capture delay (ms) |
63 | | - */ |
64 | | -"--timeout", this.options.delay!.toString(), |
65 | | - |
66 | | -/** |
67 | | - * Do not display preview overlay on screen |
68 | | - */ |
69 | | -"--nopreview", |
70 | | - |
71 | | -/** |
72 | | - * Output to stdout |
73 | | - */ |
74 | | -"--output", "-" |
75 | | -]); |
76 | | -} |
77 | | -catch (err) { |
78 | | - |
79 | | -if (err.code === "ENOENT") { |
80 | | -throw new Error("Could not take image with StillCamera. Are you running on a Raspberry Pi with 'raspistill' installed?"); |
81 | | -} |
82 | | - |
83 | | -throw err; |
84 | | -} |
85 | | -} |
| 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 | + |
| 27 | + private readonly options: StillOptions; |
| 28 | + |
| 29 | + constructor(options: StillOptions = {}) { |
| 30 | + this.options = { |
| 31 | + rotation: Rotation.Rotate0, |
| 32 | + flip: Flip.None, |
| 33 | + delay: 1, |
| 34 | + ...options, |
| 35 | + }; |
| 36 | + } |
| 37 | + |
| 38 | + async takeImage() { |
| 39 | + try { |
| 40 | + return await spawnPromise('raspistill', [ |
| 41 | + /** |
| 42 | + * Width |
| 43 | + */ |
| 44 | + ...(this.options.width ? ['--width', this.options.width.toString()] : []), |
| 45 | + |
| 46 | + /** |
| 47 | + * Height |
| 48 | + */ |
| 49 | + ...(this.options.height ? ['--height', this.options.height.toString()] : []), |
| 50 | + |
| 51 | + /** |
| 52 | + * Rotation |
| 53 | + */ |
| 54 | + ...(this.options.rotation ? ['--rotation', this.options.rotation.toString()] : []), |
| 55 | + |
| 56 | + /** |
| 57 | + * Horizontal flip |
| 58 | + */ |
| 59 | + ...(this.options.flip && |
| 60 | + (this.options.flip === Flip.Horizontal || this.options.flip === Flip.Both) |
| 61 | + ? ['--hflip'] |
| 62 | + : []), |
| 63 | + |
| 64 | + /** |
| 65 | + * Vertical flip |
| 66 | + */ |
| 67 | + ...(this.options.flip && |
| 68 | + (this.options.flip === Flip.Vertical || this.options.flip === Flip.Both) |
| 69 | + ? ['--vflip'] |
| 70 | + : []), |
| 71 | + |
| 72 | + /** |
| 73 | + * Capture delay (ms) |
| 74 | + */ |
| 75 | + '--timeout', |
| 76 | + this.options.delay!.toString(), |
| 77 | + |
| 78 | + /** |
| 79 | + * Do not display preview overlay on screen |
| 80 | + */ |
| 81 | + '--nopreview', |
| 82 | + |
| 83 | + /** |
| 84 | + * Output to stdout |
| 85 | + */ |
| 86 | + '--output', |
| 87 | + '-', |
| 88 | + ]); |
| 89 | + } catch (err) { |
| 90 | + if (err.code === 'ENOENT') { |
| 91 | + throw new Error( |
| 92 | + "Could not take image with StillCamera. Are you running on a Raspberry Pi with 'raspistill' installed?", |
| 93 | + ); |
| 94 | + } |
| 95 | + |
| 96 | + throw err; |
| 97 | + } |
| 98 | + } |
86 | 99 | } |
0 commit comments