mirror of
https://github.com/tauri-apps/plugins-workspace.git
synced 2026-05-01 12:08:06 +02:00
feat(camera): add plugin for Android and iOS
This commit is contained in:
@@ -0,0 +1,24 @@
|
||||
// Copyright 2019-2022 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::{ser::Serializer, Serialize};
|
||||
|
||||
pub type Result<T> = std::result::Result<T, Error>;
|
||||
|
||||
#[derive(Debug, thiserror::Error)]
|
||||
pub enum Error {
|
||||
#[error(transparent)]
|
||||
Io(#[from] std::io::Error),
|
||||
#[error(transparent)]
|
||||
PluginInvoke(#[from] tauri::plugin::mobile::PluginInvokeError),
|
||||
}
|
||||
|
||||
impl Serialize for Error {
|
||||
fn serialize<S>(&self, serializer: S) -> std::result::Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(self.to_string().as_ref())
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
// Copyright 2019-2022 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#![cfg(mobile)]
|
||||
|
||||
use tauri::{
|
||||
plugin::{Builder, PluginHandle, TauriPlugin},
|
||||
Manager, Runtime,
|
||||
};
|
||||
|
||||
pub use models::*;
|
||||
mod error;
|
||||
pub mod models;
|
||||
pub use error::*;
|
||||
|
||||
#[cfg(target_os = "android")]
|
||||
const PLUGIN_IDENTIFIER: &str = "app.tauri.camera";
|
||||
|
||||
#[cfg(target_os = "ios")]
|
||||
extern "C" {
|
||||
fn init_plugin_camera(webview: tauri::cocoa::base::id);
|
||||
}
|
||||
|
||||
/// A helper class to access the mobile camera APIs.
|
||||
pub struct Camera<R: Runtime>(PluginHandle<R>);
|
||||
|
||||
impl<R: Runtime> Camera<R> {
|
||||
pub fn get_photo(&self, options: ImageOptions) -> Result<Image> {
|
||||
self
|
||||
.0
|
||||
.run_mobile_plugin("getPhoto", options)
|
||||
.map_err(Into::into)
|
||||
}
|
||||
}
|
||||
|
||||
/// Extensions to [`tauri::App`], [`tauri::AppHandle`] and [`tauri::Window`] to access the camera APIs.
|
||||
pub trait CameraExt<R: Runtime> {
|
||||
fn camera(&self) -> &Camera<R>;
|
||||
}
|
||||
|
||||
impl<R: Runtime, T: Manager<R>> CameraExt<R> for T {
|
||||
fn camera(&self) -> &Camera<R> {
|
||||
self.state::<Camera<R>>().inner()
|
||||
}
|
||||
}
|
||||
|
||||
/// Initializes the plugin.
|
||||
pub fn init<R: Runtime>() -> TauriPlugin<R> {
|
||||
Builder::new("camera")
|
||||
.setup(|app, api| {
|
||||
#[cfg(target_os = "android")]
|
||||
let handle = api.register_android_plugin(PLUGIN_IDENTIFIER, "CameraPlugin")?;
|
||||
#[cfg(target_os = "ios")]
|
||||
let handle = api.register_ios_plugin(init_plugin_camera)?;
|
||||
app.manage(Camera(handle));
|
||||
Ok(())
|
||||
})
|
||||
.build()
|
||||
}
|
||||
@@ -0,0 +1,38 @@
|
||||
// Copyright 2019-2022 Tauri Programme within The Commons Conservancy
|
||||
// SPDX-License-Identifier: Apache-2.0
|
||||
// SPDX-License-Identifier: MIT
|
||||
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
||||
#[derive(Debug, Default, Serialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct ImageOptions {
|
||||
pub quality: Option<u8>,
|
||||
#[serde(default)]
|
||||
pub allow_editing: bool,
|
||||
pub result_type: Option<String>,
|
||||
#[serde(default)]
|
||||
pub save_to_gallery: bool,
|
||||
pub width: Option<usize>,
|
||||
pub height: Option<usize>,
|
||||
#[serde(default)]
|
||||
pub correct_orientation: bool,
|
||||
pub source: Option<String>,
|
||||
pub direction: Option<String>,
|
||||
pub presentation_style: Option<String>,
|
||||
pub prompt_label_header: Option<String>,
|
||||
pub prompt_label_cancel: Option<String>,
|
||||
pub prompt_label_photo: Option<String>,
|
||||
pub prompt_label_picture: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Deserialize)]
|
||||
#[serde(rename_all = "camelCase")]
|
||||
pub struct Image {
|
||||
pub data: String,
|
||||
pub asset_url: Option<String>,
|
||||
pub format: String,
|
||||
#[serde(default)]
|
||||
pub saved: bool,
|
||||
pub exif: serde_json::Value,
|
||||
}
|
||||
Reference in New Issue
Block a user