跳到内容
Tauri

条形码扫描器

允许您的移动应用程序使用摄像头扫描二维码、EAN-13 和其他类型的条形码。

此插件需要 Rust 版本至少为 **1.77.2**

平台 级别 备注
windows
linux
macos
android
ios

安装 barcode-scanner 插件即可开始使用。

使用你的项目包管理器添加依赖项

npm run tauri add barcode-scanner

在 iOS 上,条形码扫描器插件需要 NSCameraUsageDescription 信息属性列表值,该值应描述您的应用需要使用相机的原因。

src-tauri/Info.ios.plist 文件中,添加以下代码片段

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 element
scan({ windowed: true, formats: [Format.QRCode] });

默认情况下,所有潜在危险的插件命令和范围都被阻止,无法访问。您必须修改 capabilities 配置中的权限才能启用这些功能。

有关更多信息,请参阅功能概述,并参阅分步指南以使用插件权限。

src-tauri/capabilities/mobile.json
{
"$schema": "../gen/schemas/mobile-schema.json",
"identifier": "mobile-capability",
"windows": ["main"],
"platforms": ["iOS", "android"],
"permissions": ["barcode-scanner:allow-scan", "barcode-scanner:allow-cancel"]
}

默认权限

此权限集配置了默认公开的条形码扫描功能。

已授予权限

它允许所有与条形码相关的功能。

此默认权限集包括以下内容

  • 允许取消
  • 允许检查权限
  • 允许打开应用设置
  • 允许请求权限
  • 允许扫描
  • 允许振动

权限表

标识符 描述

barcode-scanner:allow-cancel

启用取消命令,不带任何预配置范围。

barcode-scanner:deny-cancel

拒绝取消命令,不带任何预配置范围。

barcode-scanner:allow-check-permissions

启用 check_permissions 命令,不带任何预配置范围。

barcode-scanner:deny-check-permissions

拒绝 check_permissions 命令,不带任何预配置范围。

barcode-scanner:allow-open-app-settings

启用 open_app_settings 命令,不带任何预配置范围。

barcode-scanner:deny-open-app-settings

拒绝 open_app_settings 命令,不带任何预配置范围。

barcode-scanner:allow-request-permissions

启用 request_permissions 命令,不带任何预配置范围。

barcode-scanner:deny-request-permissions

拒绝 request_permissions 命令,不带任何预配置范围。

barcode-scanner:allow-scan

启用扫描命令,不带任何预配置范围。

barcode-scanner:deny-scan

拒绝扫描命令,不带任何预配置范围。

barcode-scanner:allow-vibrate

启用振动命令,不带任何预配置范围。

barcode-scanner:deny-vibrate

拒绝振动命令,不带任何预配置范围。


© 2025 Tauri 贡献者。CC-BY / MIT