条形码扫描器
允许您的移动应用程序使用相机扫描 QR 码、EAN-13 和其他类型的条形码。
支持的平台
此插件需要至少 Rust 版本 1.77.2
平台 | 级别 | 备注 |
---|---|---|
Windows | | |
Linux | | |
macOS | | |
Android | ||
iOS |
设置
安装 barcode-scanner 插件以开始。
使用您项目的包管理器添加依赖项
npm run tauri add barcode-scanner
yarn run tauri add barcode-scanner
pnpm tauri add barcode-scanner
deno task tauri add barcode-scanner
bun tauri add barcode-scanner
cargo tauri add barcode-scanner
-
在
src-tauri
目录中运行以下命令以将插件添加到项目中Cargo.toml
cargo add tauri-plugin-barcode-scanner --target 'cfg(any(target_os = "android", target_os = "ios"))' -
修改
lib.rs
以初始化插件src-tauri/src/lib.rs #[cfg_attr(mobile, tauri::mobile_entry_point)]pub fn run() {tauri::Builder::default().setup(|app| {#[cfg(mobile)]app.handle().plugin(tauri_plugin_barcode_scanner::init());Ok(())}).run(tauri::generate_context!()).expect("error while running tauri application");} -
使用您喜欢的 JavaScript 包管理器安装 JavaScript 客户端绑定
npm install @tauri-apps/plugin-barcode-scanneryarn add @tauri-apps/plugin-barcode-scannerpnpm add @tauri-apps/plugin-barcode-scannerdeno add npm:@tauri-apps/plugin-barcode-scannerbun add @tauri-apps/plugin-barcode-scanner
配置
在 iOS 上,条形码扫描器插件需要 NSCameraUsageDescription
信息属性列表值,该值应描述为什么您的应用程序需要使用相机。
在 src-tauri/Info.ios.plist
文件中,添加以下片段
<?xml version="1.0" encoding="UTF-8"?><!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"><plist version="1.0"> <dict> <key>NSCameraUsageDescription</key> <string>Read QR codes</string> </dict></plist>
使用方式
条形码扫描器插件目前在 JavaScript 中可用。
import { scan, Format } from '@tauri-apps/plugin-barcode-scanner';// when using `"withGlobalTauri": true`, you may use// const { scan, Format } = window.__TAURI__.barcodeScanner;
// `windowed: true` actually sets the webview to transparent// instead of opening a separate view for the camera// make sure your user interface is ready to show what is underneath with a transparent elementscan({ windowed: true, formats: [Format.QRCode] });
权限
默认情况下,所有潜在的插件命令和范围都受到阻止,无法访问。您必须修改 capabilities
配置中的权限以启用这些功能。
{ "$schema": "../gen/schemas/mobile-schema.json", "identifier": "mobile-capability", "windows": ["main"], "platforms": ["iOS", "android"], "permissions": ["barcode-scanner:allow-scan", "barcode-scanner:allow-cancel"]}
默认权限
此权限集配置了默认暴露哪些条形码扫描功能。
已授予的权限
它允许所有与条形码相关的功能。
allow-cancel
allow-check-permissions
allow-open-app-settings
allow-request-permissions
allow-scan
allow-vibrate
权限表
标识符 | 描述 |
---|---|
|
启用取消命令,无需预先配置的任何作用域。 |
|
拒绝取消命令,无需预先配置的任何作用域。 |
|
启用检查权限命令,无需预先配置的任何作用域。 |
|
拒绝检查权限命令,无需预先配置的任何作用域。 |
|
启用打开应用设置命令,无需预先配置的任何作用域。 |
|
拒绝打开应用设置命令,无需预先配置的任何作用域。 |
|
启用请求权限命令,无需预先配置的任何作用域。 |
|
拒绝请求权限命令,无需预先配置的任何作用域。 |
|
启用扫描命令,无需预先配置的任何作用域。 |
|
拒绝扫描命令,无需预先配置的任何作用域。 |
|
启用振动命令,无需预先配置的任何作用域。 |
|
拒绝振动命令,无需预先配置的任何作用域。 |
© 2025 Tauri 贡献者。CC-BY / MIT