在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
开源软件名称:mebjas/html5-qrcode开源软件地址:https://github.com/mebjas/html5-qrcode开源编程语言:TypeScript 98.6%开源软件介绍:Html5-QRCode(supports barcodes now :))A cross-platform HTML5 QR code & barcode reader. Use this lightweight library to easily / quickly integrate QR code, bar code, and other common code scanning capabilities to your web application.
NoticeUX/UXD/UXR help wanted: We are looking to improve user experience of this code scanning library — if you are interested in making this experience better for both developers and end users, please share your ideas, thoughts, inputs in this discussion Supported platformsWe are working continuously on adding support for more and more platforms. If you find a platform or a browser where the library is not working, please feel free to file an issue. Check the demo link to test it out. Legends
PC / Mac
Android
IOS
Framework supportThe library can be easily used with several other frameworks, I have been adding examples for a few of them and would continue to add more.
Supported Code formatsCode scanning is dependent on Zxing-js library. We will be working on top of it to add support for more types of code scanning. If you feel a certain type of code would be helpful to have, please file a feature request.
View DemoDescription -
This is a cross-platform JavaScript library to integrate QR code, bar codes & a few other types of code scanning capabilities to your applications running on HTML5 compatible browser. Supports:
How to use
Download the script from release page or install using npm i html5-qrcode Add an element you want to use as a placeholder for QR Code scanner <div id="reader" width="600px"></div>
Using directly in browser without any loaderIf you are not using any loader, you can get the latest UMD javascript code in production from https://unpkg.com/html5-qrcode. <script src="https://unpkg.com/html5-qrcode" type="text/javascript">
Using with module loadersInclude the script with // To use Html5QrcodeScanner (more info below)
import {Html5QrcodeScanner} from "html5-qrcode"
// To use Html5Qrcode (more info below)
import {Html5Qrcode} from "html5-qrcode" Easy Mode - With end to end scanner user interface
You can set up the scanner as follows: function onScanSuccess(decodedText, decodedResult) {
// handle the scanned code as you like, for example:
console.log(`Code matched = ${decodedText}`, decodedResult);
}
function onScanFailure(error) {
// handle scan failure, usually better to ignore and keep scanning.
// for example:
console.warn(`Code scan error = ${error}`);
}
let html5QrcodeScanner = new Html5QrcodeScanner(
"reader",
{ fps: 10, qrbox: {width: 250, height: 250} },
/* verbose= */ false);
html5QrcodeScanner.render(onScanSuccess, onScanFailure); Pro Mode - if you want to implement your own user interfaceYou can use You can use the following APIs to For using inline QR Code scanning with Webcam or Smartphone cameraStart ScanningTo get a list of supported cameras, query it using static method // This method will trigger user permissions
Html5Qrcode.getCameras().then(devices => {
/**
* devices would be an array of objects of type:
* { id: "id", label: "label" }
*/
if (devices && devices.length) {
var cameraId = devices[0].id;
// .. use this to start scanning.
}
}).catch(err => {
// handle err
}); Important: Note that this method will trigger user permission if the user has not granted it already.
Once you have the camera ID from const html5QrCode = new Html5Qrcode(/* element id */ "reader");
html5QrCode.start(
cameraId,
{
fps: 10, // Optional, frame per seconds for qr code scanning
qrbox: { width: 250, height: 250 } // Optional, if you want bounded box UI
},
(decodedText, decodedResult) => {
// do something when code is read
},
(errorMessage) => {
// parse error, ignore it.
})
.catch((err) => {
// Start failed, handle it.
});
const html5QrCode = new Html5Qrcode("reader", /* verbose= */ true); Scanning without cameraIdIn mobile devices you may want users to directly scan the QR code using the back camera or the front camera for some use cases. For such cases you can avoid using the exact camera device ID that you get from const html5QrCode = new Html5Qrcode("reader");
const qrCodeSuccessCallback = (decodedText, decodedResult) => {
/* handle success */
};
const config = { fps: 10, qrbox: { width: 250, height: 250 } };
// If you want to prefer front camera
html5QrCode.start({ facingMode: "user" }, config, qrCodeSuccessCallback);
// If you want to prefer back camera
html5QrCode.start({ facingMode: "environment" }, config, qrCodeSuccessCallback);
// Select front camera or fail with `OverconstrainedError`.
html5QrCode.start({ facingMode: { exact: "user"} }, config, qrCodeSuccessCallback);
// Select back camera or fail with `OverconstrainedError`.
html5QrCode.start({ facingMode: { exact: "environment"} }, config, qrCodeSuccessCallback); Passing the html5QrCode.start({ deviceId: { exact: cameraId} }, config, qrCodeSuccessCallback); Stop ScanningTo stop using camera and thus stop scanning, call html5QrCode.stop().then((ignore) => {
// QR Code scanning is stopped.
}).catch((err) => {
// Stop failed, handle it.
});
For QR Code scanning using local files or inbuilt camera on Smartphones
You can alternatively leverage QR Code scanning for local files on the device or default camera on the device. It works similar to inline QR Code scanning. Define the HTML container and import the JavaScript library as mentioned above <div id="reader" width="600px" height="600px"></div>
<script src="./dist/html5-qrcode.js"></script>
Add an <input type="file" id="qr-input-file" accept="image/*">
<!--
Or add captured if you only want to enable smartphone camera, PC browsers will ignore it.
-->
<input type="file" id="qr-input-file" accept="image/*" capture> Find more information about this at developers.google.com. And in JavaScript code initialize the object and attach listener like this: const html5QrCode = new Html5Qrcode(/* element id */ "reader");
// File based scanning
const fileinput = document.getElementById('qr-input-file');
fileinput.addEventListener('change', e => {
if (e.target.files.length == 0) {
// No file selected, ignore
return;
}
const imageFile
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论