UNPKG

@dderevjanik/termux-api

Version:

This library allows you to interact with your Android device from Node.js using termux-api

50 lines (47 loc) 1.24 kB
import { exec } from "child_process"; export interface CameraPhotoOptions { /** * ID of the camera to use (see termux-camera-info) * @default 0 */ cameraId?: number; /** * Photo is saved at specified file path * @default DATE_TIME.jpg */ output?: string; /** * Timeout in milliseconds for the command execution * @default 3000 */ timeout?: number; } /** * Take a photo and save it to a file in JPEG format * * **Requires permission** `android.permission.CAMERA` */ export async function cameraPhoto( options: CameraPhotoOptions = {} ): Promise<void> { const opts: CameraPhotoOptions = { cameraId: 0, output: `${new Date().toISOString().replace(/:/g, "-")}.jpg`, timeout: 3_000, ...options, }; // TODO: Check if path is correct, or create it return new Promise<void>((resolve, reject) => { const command = `termux-camera-photo -c ${opts.cameraId} "${opts.output}"`; const execOptions = { timeout: opts.timeout }; exec(command, execOptions, (error, stdout, stderr) => { if (error) { return reject(`Error: ${error.message}`); } if (stderr) { return reject(`Error: ${stderr}`); } return resolve(); }); }); }