条形码扫描器
允许您的移动应用程序使用相机扫描 QR 码、EAN-13 和其他类型的条形码。
支持的平台
This plugin requires a Rust version of at least 1.77.2
Platform | Level | Notes |
---|---|---|
windows | | |
linux | | |
macos | | |
android | ||
ios |
设置
从安装条形码扫描器插件开始。
使用项目的包管理器来添加依赖。
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 Guest 绑定。
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';// 当设置 `"withGlobalTauri": true` 时, 你可以使用// const { scan, Format } = window.__TAURI__.barcodeScanner;
// `windowed: true` 实际上将 webview 设置为透明的// 而不是为相机打开一个单独的视图// 确保你的用户界面已经准备好使用透明元素显示下面的内容scan({ 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"]}
Default Permission
This permission set configures which barcode scanning features are by default exposed.
Granted Permissions
It allows all barcode related features.
This default permission set includes the following:
allow-cancel
allow-check-permissions
allow-open-app-settings
allow-request-permissions
allow-scan
allow-vibrate
Permission Table
Identifier | Description |
---|---|
|
Enables the cancel command without any pre-configured scope. |
|
Denies the cancel command without any pre-configured scope. |
|
Enables the check_permissions command without any pre-configured scope. |
|
Denies the check_permissions command without any pre-configured scope. |
|
Enables the open_app_settings command without any pre-configured scope. |
|
Denies the open_app_settings command without any pre-configured scope. |
|
Enables the request_permissions command without any pre-configured scope. |
|
Denies the request_permissions command without any pre-configured scope. |
|
Enables the scan command without any pre-configured scope. |
|
Denies the scan command without any pre-configured scope. |
|
Enables the vibrate command without any pre-configured scope. |
|
Denies the vibrate command without any pre-configured scope. |
© 2025 Tauri Contributors. CC-BY / MIT