termux/
camera.rs

1use std::path::Path;
2#[cfg(test)]
3use std::path::PathBuf;
4use std::env::current_dir;
5#[cfg(test)]
6use std::env::var;
7#[cfg(test)]
8use std::ffi::OsStr;
9use crate::*;
10use serde::Deserialize;
11
12#[derive(Deserialize, Debug, Clone)]
13pub struct JpegOutputSize {
14    pub width: usize,
15    pub height: usize,
16}
17
18#[derive(Deserialize, Debug, Clone)]
19pub struct PhysicalSize {
20    pub width: f64,
21    pub height: f64,
22}
23
24#[derive(Deserialize, Debug, Clone)]
25pub struct CameraInfo {
26    pub id: String,
27    pub facing: String,
28    pub jpeg_output_sizes: Vec<JpegOutputSize>,
29    pub focal_lengths: Vec<f64>,
30    pub auto_exposure_modes: Vec<String>,
31    pub physical_size: PhysicalSize,
32    pub capabilities: Vec<String>,
33}
34
35/// Get information about device camera(s).
36pub fn info() -> io::Result<Vec<CameraInfo>> {
37    let out = run_api_cmd("CameraInfo")?;
38    Ok(serde_json::from_str(&out).unwrap())
39}
40
41/// Take a photo and save it to a file in JPEG format.
42/// 
43/// `camera-id` is the id of the camera to use.
44/// 
45/// See [termux::camera::info]
46pub fn photo<P: AsRef<Path>>(camera_id: &str, path: P) -> io::Result<()> {
47    let mut p = current_dir()?;
48    p.push(path);
49    let _out = run_api_cmd_with_args(
50        "CameraPhoto",
51        &[
52            "--es",
53            "camera",
54            camera_id,
55            "--es", "file",
56            &p.to_string_lossy()
57        ],
58    )?;
59    Ok(())
60}
61
62#[test]
63fn test_info() {
64    assert!(info().is_ok())
65}
66
67#[test]
68fn test_photo() {
69    let mut p = PathBuf::from(var(OsStr::new("TMPDIR")).unwrap());
70    p.push("picture.jpg");
71    assert!(photo("0", p).is_ok())
72}