node-libcamera 0.1.8 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
package/src/utils.ts CHANGED
@@ -1,20 +1,40 @@
1
1
  import { exec, ExecOptionsWithBufferEncoding } from 'child_process'
2
- import { optionConverterMap, LibCamera } from './options'
2
+ import LibCamera from './LibCamera'
3
3
 
4
+ /**
5
+ * Get the string command of a base command with an array of args
6
+ * @param base The base command without any flags
7
+ * @param args An array of args command args
8
+ * @returns The command string
9
+ */
4
10
  export function cmd(base: string, args?: string[]): string {
5
11
  if (base && !args) return base
6
12
  return `${base} ${args?.join(' ')}`
7
13
  }
8
14
 
9
- export function run(command: string, options?: ExecOptionsWithBufferEncoding) {
15
+ /**
16
+ * Execute a command
17
+ * @param command The command string
18
+ * @param options Any options to pass to the node exec function
19
+ * @returns A promise that resolves to the stdout of the command that was run
20
+ */
21
+ export function run(
22
+ command: string,
23
+ options?: ExecOptionsWithBufferEncoding
24
+ ): Promise<string> {
10
25
  return new Promise((resolve, reject) => {
11
26
  exec(command, options, (error, stdout, stderr) => {
12
27
  if (stderr || error) reject(stderr || error)
13
- resolve(stdout)
28
+ resolve(stdout?.toString()?.trim())
14
29
  })
15
30
  })
16
31
  }
17
32
 
33
+ /**
34
+ * Convert libcamera options to command line args
35
+ * @param options Options for the libcamera service
36
+ * @returns An array of command line args
37
+ */
18
38
  export function convertOptionsToCmdArgs(
19
39
  options: Partial<LibCamera.OptionsObject>
20
40
  ): string[] {
@@ -23,7 +43,27 @@ export function convertOptionsToCmdArgs(
23
43
  const converter = optionConverterMap[key as LibCamera.OptionKeys]
24
44
  const value = typeof converter === 'function' ? converter(val) : val
25
45
  if (value) args.push(`--${key}`)
26
- if (value !== true && value?.toString()) args.push(value?.toString())
46
+ if (value && value !== true && value?.toString()) {
47
+ args.push(value?.toString())
48
+ }
27
49
  }, true)
28
50
  return args
29
51
  }
52
+
53
+ export const optionConverterMap: Partial<Record<
54
+ LibCamera.OptionKeys,
55
+ LibCamera.OptionConverter
56
+ >> = {
57
+ preview({ x, y, width, height }: LibCamera.PreviewOption) {
58
+ return `${x},${y},${width},${height}`
59
+ },
60
+ thumb({ width, height, quality }: LibCamera.ThumbOption) {
61
+ return `${width}:${height}:${quality}`
62
+ },
63
+ awbgains([val1, val2]: [number, number]) {
64
+ return `${val1},${val2}`
65
+ },
66
+ roi({ x, y, w, h }: LibCamera.ROIOption) {
67
+ return `${x},${y},${w},${h}`
68
+ },
69
+ }
package/dist/options.d.ts DELETED
@@ -1,130 +0,0 @@
1
- export declare namespace LibCamera {
2
- interface CommandLineOptions {
3
- /**
4
- * Print help information for the application
5
- *
6
- * The `--help` option causes every application to print its full set of command line options with a brief synopsis of each, and then quit.
7
- **/
8
- help: boolean;
9
- /**
10
- * Print out a software version number
11
- *
12
- * All `libcamera-apps` will, when they see the `--version` option, print out a version string both for `libcamera` and `libcamera-apps` and then quit.
13
- */
14
- version: boolean;
15
- /**
16
- * Delay before application stops automatically <milliseconds>
17
- *
18
- * The `--timeout` option specifies how long the application runs before it stops, whether it is recording a video or showing a preview. In the case of still image capture, the application will show the preview window for this long before capturing the output image.
19
- *
20
- * If unspecified, the default value is 5000 (5 seconds). The value zero causes the application to run indefinitely.
21
- */
22
- timeout: number;
23
- }
24
- interface PreviewWindowOptions {
25
- /**
26
- * Preview window settings
27
- *
28
- * Sets the size and location of the preview window (both X Windows and DRM versions). It does not affect the resolution or aspect ratio of images being requested from the camera. The camera images will be scaled to the size of the preview window for display, and will be pillar/letter-boxed to fit.
29
- *
30
- * Example:
31
- * ```json
32
- * { "x": 100, "y": 100, "width": 500, "height": 500 }
33
- * ```
34
- */
35
- preview: PreviewOption;
36
- /**
37
- * Fullscreen preview mode
38
- *
39
- * Forces the preview window to use the whole screen, and the window will have no border or title bar. Again the image may be pillar/letter-boxed.
40
- */
41
- fullscreen: boolean;
42
- /**
43
- * Use Qt-based preview window
44
- *
45
- * The preview window is switched to use the Qt-based implementation. This option is not normally recommended because it no longer uses zero-copy buffer sharing nor GPU acceleration and is therefore very expensive, however, it does support X forwarding (which the other preview implementations do not).
46
- *
47
- * The Qt preview window does not support the `--fullscreen` option. Generally it is advised to try and keep the preview window small.
48
- */
49
- 'qt-preview': boolean;
50
- /**
51
- * Do not display a preview window
52
- *
53
- * The preview window is suppressed entirely.
54
- */
55
- nopreview: boolean;
56
- /**
57
- * Set window title bar text <string>
58
- *
59
- * The supplied string is set as the title of the preview window (when running under X Windows). Additionally the string may contain a number of `%` directives which are substituted with information from the image metadata. The permitted directives are:
60
- *
61
- * | Directive | Substitution |
62
- * | :--------------- | :------------------ |
63
- * | `%frame` | The sequence number of the frame |
64
- * | `%fps` | The instantaneous frame rate |
65
- * | `%exp` | The shutter speed used to capture the image, in microseconds |
66
- * | `%ag` | The analogue gain applied to the image in the sensor |
67
- * | `%dg` | The digital gain applied to the image by the ISP |
68
- * | `%rg` | The gain applied to the red component of each pixel |
69
- * | `%bg` | The gain applied to the blue component of each pixel |
70
- * | `%focus` | The focus metric for the image, where a larger value implies a sharper image |
71
- *
72
- * When not provided, the `--info-text` string defaults to `"#%frame (%fps fps) exp %exp ag %ag dg %dg"`.
73
- */
74
- 'info-text': string;
75
- }
76
- interface CameraResolutionOptions {
77
- /**
78
- * Capture image width <width>
79
- *
80
- * This number specifies the output resolution of the camera images captured by libcamera-still, libcamera-jpeg and libcamera-vid.
81
- *
82
- * For libcamera-raw, it affects the size of the raw frames captured. Where a camera has a 2x2 binned readout mode, specifying a resolution not larger than this binned mode will result in the capture of 2x2 binned raw frames.
83
- *
84
- * For libcamera-hello these parameters have no effect.
85
- */
86
- width: number;
87
- /**
88
- * Capture image height <height>
89
- *
90
- * This number specifies the output resolution of the camera images captured by libcamera-still, libcamera-jpeg and libcamera-vid.
91
- *
92
- * For libcamera-raw, it affects the size of the raw frames captured. Where a camera has a 2x2 binned readout mode, specifying a resolution not larger than this binned mode will result in the capture of 2x2 binned raw frames.
93
- *
94
- * For libcamera-hello these parameters have no effect.
95
- */
96
- height: number;
97
- }
98
- interface OutputFileOptions {
99
- /**
100
- * Output file name <string>
101
- *
102
- * `--output` sets the name of the output file to which the output image or video is written. Besides regular file names, this may take the following special values:
103
- *
104
- * `-` - write to stdout
105
- *
106
- * `udp://` - a string starting with this is taken as a network address for streaming
107
- *
108
- * `tcp://` - a string starting with this is taken as a network address for streaming
109
- *
110
- * a string containing a `%d` directive is taken as a file name where the format directive is replaced with a count that increments for each file that is opened. Standard C format directive modifiers are permitted.
111
- */
112
- output: string;
113
- }
114
- interface VideoOptions {
115
- /**
116
- * Saves timestamp information to the specified file. Useful as an input file to `mkvmerge`.
117
- */
118
- 'save-pts': string;
119
- }
120
- type OptionsObject = CommandLineOptions & PreviewWindowOptions & CameraResolutionOptions & OutputFileOptions & VideoOptions;
121
- type OptionKeys = keyof OptionsObject;
122
- type OptionConverter = (val: any) => string;
123
- interface PreviewOption {
124
- x: number;
125
- y: number;
126
- width: number;
127
- height: number;
128
- }
129
- }
130
- export declare const optionConverterMap: Partial<Record<LibCamera.OptionKeys, LibCamera.OptionConverter>>;
package/src/options.ts DELETED
@@ -1,149 +0,0 @@
1
- export namespace LibCamera {
2
- export interface CommandLineOptions {
3
- /**
4
- * Print help information for the application
5
- *
6
- * The `--help` option causes every application to print its full set of command line options with a brief synopsis of each, and then quit.
7
- **/
8
- help: boolean
9
- /**
10
- * Print out a software version number
11
- *
12
- * All `libcamera-apps` will, when they see the `--version` option, print out a version string both for `libcamera` and `libcamera-apps` and then quit.
13
- */
14
- version: boolean
15
- /**
16
- * Delay before application stops automatically <milliseconds>
17
- *
18
- * The `--timeout` option specifies how long the application runs before it stops, whether it is recording a video or showing a preview. In the case of still image capture, the application will show the preview window for this long before capturing the output image.
19
- *
20
- * If unspecified, the default value is 5000 (5 seconds). The value zero causes the application to run indefinitely.
21
- */
22
- timeout: number
23
- }
24
-
25
- export interface PreviewWindowOptions {
26
- /**
27
- * Preview window settings
28
- *
29
- * Sets the size and location of the preview window (both X Windows and DRM versions). It does not affect the resolution or aspect ratio of images being requested from the camera. The camera images will be scaled to the size of the preview window for display, and will be pillar/letter-boxed to fit.
30
- *
31
- * Example:
32
- * ```json
33
- * { "x": 100, "y": 100, "width": 500, "height": 500 }
34
- * ```
35
- */
36
- preview: PreviewOption
37
- /**
38
- * Fullscreen preview mode
39
- *
40
- * Forces the preview window to use the whole screen, and the window will have no border or title bar. Again the image may be pillar/letter-boxed.
41
- */
42
- fullscreen: boolean
43
- /**
44
- * Use Qt-based preview window
45
- *
46
- * The preview window is switched to use the Qt-based implementation. This option is not normally recommended because it no longer uses zero-copy buffer sharing nor GPU acceleration and is therefore very expensive, however, it does support X forwarding (which the other preview implementations do not).
47
- *
48
- * The Qt preview window does not support the `--fullscreen` option. Generally it is advised to try and keep the preview window small.
49
- */
50
- 'qt-preview': boolean
51
- /**
52
- * Do not display a preview window
53
- *
54
- * The preview window is suppressed entirely.
55
- */
56
- nopreview: boolean
57
- /**
58
- * Set window title bar text <string>
59
- *
60
- * The supplied string is set as the title of the preview window (when running under X Windows). Additionally the string may contain a number of `%` directives which are substituted with information from the image metadata. The permitted directives are:
61
- *
62
- * | Directive | Substitution |
63
- * | :--------------- | :------------------ |
64
- * | `%frame` | The sequence number of the frame |
65
- * | `%fps` | The instantaneous frame rate |
66
- * | `%exp` | The shutter speed used to capture the image, in microseconds |
67
- * | `%ag` | The analogue gain applied to the image in the sensor |
68
- * | `%dg` | The digital gain applied to the image by the ISP |
69
- * | `%rg` | The gain applied to the red component of each pixel |
70
- * | `%bg` | The gain applied to the blue component of each pixel |
71
- * | `%focus` | The focus metric for the image, where a larger value implies a sharper image |
72
- *
73
- * When not provided, the `--info-text` string defaults to `"#%frame (%fps fps) exp %exp ag %ag dg %dg"`.
74
- */
75
- 'info-text': string
76
- }
77
-
78
- export interface CameraResolutionOptions {
79
- /**
80
- * Capture image width <width>
81
- *
82
- * This number specifies the output resolution of the camera images captured by libcamera-still, libcamera-jpeg and libcamera-vid.
83
- *
84
- * For libcamera-raw, it affects the size of the raw frames captured. Where a camera has a 2x2 binned readout mode, specifying a resolution not larger than this binned mode will result in the capture of 2x2 binned raw frames.
85
- *
86
- * For libcamera-hello these parameters have no effect.
87
- */
88
- width: number
89
- /**
90
- * Capture image height <height>
91
- *
92
- * This number specifies the output resolution of the camera images captured by libcamera-still, libcamera-jpeg and libcamera-vid.
93
- *
94
- * For libcamera-raw, it affects the size of the raw frames captured. Where a camera has a 2x2 binned readout mode, specifying a resolution not larger than this binned mode will result in the capture of 2x2 binned raw frames.
95
- *
96
- * For libcamera-hello these parameters have no effect.
97
- */
98
- height: number
99
- }
100
-
101
- export interface OutputFileOptions {
102
- /**
103
- * Output file name <string>
104
- *
105
- * `--output` sets the name of the output file to which the output image or video is written. Besides regular file names, this may take the following special values:
106
- *
107
- * `-` - write to stdout
108
- *
109
- * `udp://` - a string starting with this is taken as a network address for streaming
110
- *
111
- * `tcp://` - a string starting with this is taken as a network address for streaming
112
- *
113
- * a string containing a `%d` directive is taken as a file name where the format directive is replaced with a count that increments for each file that is opened. Standard C format directive modifiers are permitted.
114
- */
115
- output: string
116
- }
117
-
118
- export interface VideoOptions {
119
- /**
120
- * Saves timestamp information to the specified file. Useful as an input file to `mkvmerge`.
121
- */
122
- 'save-pts': string
123
- }
124
-
125
- export type OptionsObject = CommandLineOptions &
126
- PreviewWindowOptions &
127
- CameraResolutionOptions &
128
- OutputFileOptions &
129
- VideoOptions
130
-
131
- export type OptionKeys = keyof OptionsObject
132
- export type OptionConverter = (val: any) => string
133
-
134
- export interface PreviewOption {
135
- x: number
136
- y: number
137
- width: number
138
- height: number
139
- }
140
- }
141
-
142
- export const optionConverterMap: Partial<Record<
143
- LibCamera.OptionKeys,
144
- LibCamera.OptionConverter
145
- >> = {
146
- preview(val: LibCamera.PreviewOption) {
147
- return `${val.x},${val.y},${val.width},${val.height}`
148
- },
149
- } as const