termux_api/api/
camera.rs

1use std::process::Command;
2
3use serde::Deserialize;
4
5use super::errors::TermuxError;
6
7#[derive(Debug, Deserialize)]
8pub struct Size {
9    pub width: u32,
10    pub height: u32,
11}
12
13#[derive(Debug, Deserialize)]
14pub struct PhysicalSize {
15    pub width: f32,
16    pub height: f32,
17}
18
19#[derive(Debug, Deserialize)]
20pub struct CameraInfo {
21    pub id: String,
22    pub facing: String,
23    pub jpeg_output_sizes: Vec<Size>,
24    pub focal_lengths: Vec<f32>,
25    pub auto_exposure_modes: Vec<String>,
26    pub physical_size: PhysicalSize,
27    pub capabilities: Vec<String>,
28}
29
30pub type CamerasInfo = Vec<CameraInfo>;
31
32pub struct TermuxCameraInfo {}
33
34impl TermuxCameraInfo {
35    pub fn new() -> Self {
36        TermuxCameraInfo {}
37    }
38
39    pub fn run(self) -> Result<CamerasInfo, TermuxError> {
40        let mut command = Command::new("termux-camera-info");
41        let output = command.output();
42        match output {
43            Ok(output) => {
44                if output.status.success() {
45                    let camera_info: CamerasInfo = serde_json::from_slice(&output.stdout).unwrap();
46                    return Ok(camera_info);
47                }
48                Err(TermuxError::Output(output.to_owned()))
49            }
50            Err(e) => Err(TermuxError::IOError(e)),
51        }
52    }
53}
54
55pub struct TermuxCameraPhoto {
56    id: Option<u8>,
57    save_path: Option<String>,
58}
59
60impl TermuxCameraPhoto {
61    pub fn new() -> Self {
62        TermuxCameraPhoto {
63            id: None,
64            save_path: None,
65        }
66    }
67
68    pub fn id(mut self, id: u8) -> Self {
69        self.id = Some(id);
70        self
71    }
72
73    pub fn save_path(mut self, path: String) -> Self {
74        self.save_path = Some(path);
75        self
76    }
77
78    pub fn run(self) -> Result<(), TermuxError> {
79        let mut command = Command::new("termux-camera-photo");
80        if let Some(id) = self.id {
81            command.arg("-c").arg(format!("{}", id));
82        }
83        if let Some(path) = self.save_path {
84            command.arg(path);
85        }
86        let output = command.output();
87        match output {
88            Ok(output) => {
89                if output.status.success() {
90                    return Ok(());
91                }
92                Err(TermuxError::Output(output.to_owned()))
93            }
94            Err(e) => Err(TermuxError::IOError(e)),
95        }
96    }
97}
98
99pub struct TermuxCamera {
100    pub info: TermuxCameraInfo,
101    pub photo: TermuxCameraPhoto,
102}
103
104impl TermuxCamera {
105    pub fn new() -> Self {
106        TermuxCamera {
107            info: TermuxCameraInfo::new(),
108            photo: TermuxCameraPhoto::new(),
109        }
110    }
111}