tauri-apps / tauri-apps/plugins-workspace
[barcode-scanner]call scan but never returned
- Dominant language
- Rust
- Stars
- 1.8k
- Forks
- 602
- Avg merge
- 4d 14h
- Merged PRs (30d)
- 9
Description
I followed the sample at https://v2.tauri.app/plugin/barcode-scanner/,but never got Scanned result after calling scan.
The camera icon showed on the top-right corner of the screen, and keeps while the app runs, but no any result returns.
Also I tried to set `scan({ windowed: false, formats: [Format.QRCode] });`, it shows the camera screen, and never response, no matter how i scan the qr code.
I'm using XiaoMi 13 with HyperOS, android version 14
---
The project using automatic install with `npm run tauri add barcode-scanner`, then the related code as follows:
`src-tauri/Cargo.toml`:
```
[dependencies]
...
[target.'cfg(any(target_os = "android", target_os = "ios"))'.dependencies]
tauri-plugin-barcode-scanner = "2"
```
---
`src-tauri/src/lib.rs`
```rust
pub fn run() {
let mut app = tauri::Builder::default();
...
#[cfg(any(target_os = "android", target_os = "ios"))]
{
app = app.plugin(tauri_plugin_barcode_scanner::init());
}
...
}
```
---
`src-tauri/capabilities/mobile.json`
```
...
"permissions": [
"biometric:default",
"barcode-scanner:allow-scan",
"barcode-scanner:allow-cancel",
"barcode-scanner:allow-check-permissions",
"barcode-scanner:allow-request-permissions"
]
```
---
`src/Features/Scanner.tsx`
```tsx
import { useEffect, useState } from "react";
import { platform } from "@tauri-apps/plugin-os";
import { scan, cancel, Format, checkPermissions, requestPermissions } from "@tauri-apps/plugin-barcode-scanner";
import "./Scanner.css";
function Scanner() {
const [scanResult, setScanResult] = useState("");
const [currentPlatform, setCurrentPlatform] = useState("");
useEffect(() => {
const fetchPlatform = async () => {
const platformName = await platform();
setCurrentPlatform(platformName);
};
fetchPlatform();
}, [currentPlatform]);
const handleScan = async () => {
//alert("Current Platform: " + currentPlatform);
if (["android", "ios"].includes(currentPlatform)) {
try {
const permissions = await checkPermissions();
if (permissions !== "granted") {
const requestResult = await requestPermissions();
if (requestResult !== "granted") {
alert("Camera permission is required to scan QR codes.");
return;
}
}
const result = await scan({ windowed: true, formats: [Format.QRCode] });
//await cancel();
//alert("QR code scanning result: " + result.content);
setScanResult(result.content);
} catch (error) {
alert("QR code scanning failed: " + error);
console.error("QR code scanning failed:", error);
}
} else {
alert("QR code scanning is only supported on Android and iOS platforms.");
}
};
const handleCopy = () => {
navigator.clipboard.writeText(scanResult);
alert("Text copied to clipboard!");
};
return (
Scan QR Code
<button onClick={handleCopy}>Copy Text</button>
</div>
);
}
export default Scanner;
```
Contributor guide
Assessment
This issue has not been assessed yet.