Skip to content
Tauri 中文网

条形码扫描仪

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

¥Allows your mobile application to use the camera to scan QR codes, EAN-13 and other kinds of barcodes.

支持的平台

¥Supported Platforms

This plugin requires a Rust version of at least 1.77.2

Platform Level Notes
windows
linux
macos
android
ios

设置

¥Setup

安装条形码扫描仪插件即可开始使用。

¥Install the barcode-scanner plugin to get started.

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

¥Use your project’s package manager to add the dependency:

npm run tauri add barcode-scanner

配置

¥Configuration

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

¥On iOS the barcode scanner plugin requires the NSCameraUsageDescription information property list value, which should describe why your app needs to use the camera.

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

¥In the src-tauri/Info.ios.plist file, add the following snippet:

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>

使用

¥Usage

条码扫描器插件在 JavaScript 中可用。

¥The barcode scanner plugin is available in 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] });

权限

¥Permissions

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

¥By default all potentially dangerous plugin commands and scopes are blocked and cannot be accessed. You must modify the permissions in your capabilities configuration to enable these.

有关更详细的说明,请参阅 功能概述

¥See the Capabilities Overview for more information and the step by step guide to use plugin permissions.

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"]
}

Default Permission

This permission set configures which barcode scanning features are by default exposed.

Granted Permissions

It allows all barcode related features.

  • allow-cancel
  • allow-check-permissions
  • allow-open-app-settings
  • allow-request-permissions
  • allow-scan
  • allow-vibrate

Permission Table

Identifier Description

barcode-scanner:allow-cancel

Enables the cancel command without any pre-configured scope.

barcode-scanner:deny-cancel

Denies the cancel command without any pre-configured scope.

barcode-scanner:allow-check-permissions

Enables the check_permissions command without any pre-configured scope.

barcode-scanner:deny-check-permissions

Denies the check_permissions command without any pre-configured scope.

barcode-scanner:allow-open-app-settings

Enables the open_app_settings command without any pre-configured scope.

barcode-scanner:deny-open-app-settings

Denies the open_app_settings command without any pre-configured scope.

barcode-scanner:allow-request-permissions

Enables the request_permissions command without any pre-configured scope.

barcode-scanner:deny-request-permissions

Denies the request_permissions command without any pre-configured scope.

barcode-scanner:allow-scan

Enables the scan command without any pre-configured scope.

barcode-scanner:deny-scan

Denies the scan command without any pre-configured scope.

barcode-scanner:allow-vibrate

Enables the vibrate command without any pre-configured scope.

barcode-scanner:deny-vibrate

Denies the vibrate command without any pre-configured scope.


Tauri 中文网 - 粤ICP备13048890号