|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="utf-8" /> |
| 5 | + <meta name="viewport" content="width=device-width,initial-scale=1.0" /> |
| 6 | + <meta |
| 7 | + name="description" |
| 8 | + content="Read barcodes from camera with Dynamsoft Barcode Reader in a PWA application." |
| 9 | + /> |
| 10 | + <meta name="keywords" content="barcode, camera, PWA" /> |
| 11 | + <title> |
| 12 | + Dynamsoft Barcode Reader PWA Sample - Hello World (Decode via Camera) |
| 13 | + </title> |
| 14 | + <link |
| 15 | + rel="canonical" |
| 16 | + href="https://demo.dynamsoft.com/Samples/DBR/JS/hello-world/pwa/helloworld-pwa.html" |
| 17 | + /> |
| 18 | + <link rel="manifest" href="./helloworld-pwa.json" /> |
| 19 | + <meta name="theme-color" content="#B12A34" /> |
| 20 | + <meta name="mobile-web-app-capable" content="yes" /> |
| 21 | + <meta name="apple-mobile-web-app-title" content="sample for ios" /> |
| 22 | + <meta name="apple-mobile-web-app-capable" content="yes" /> |
| 23 | + <meta name="apple-mobile-web-app-status-bar-style" content="default" /> |
| 24 | + <link |
| 25 | + rel="apple-touch-icon" |
| 26 | + sizes="192x192" |
| 27 | + href="./dynamsoft-192x192.png" |
| 28 | + /> |
| 29 | + <link |
| 30 | + rel="apple-touch-icon" |
| 31 | + sizes="512x512" |
| 32 | + href="./dynamsoft-512x512.png" |
| 33 | + /> |
| 34 | + <script src="https://npm.scannerproxy.com/cdn/@dynamsoft/dynamsoft-core@3.0.20-dev-20231010152155/dist/core.js"></script> |
| 35 | + <script src="https://npm.scannerproxy.com/cdn/@dynamsoft/dynamsoft-utility@1.0.10-dev-20231023103736/dist/utility.js"></script> |
| 36 | + <script src="https://npm.scannerproxy.com/cdn/@dynamsoft/dynamsoft-barcode-reader@10.0.20-dev-20231020140243/dist/dbr.js"></script> |
| 37 | + <script src="https://npm.scannerproxy.com/cdn/@dynamsoft/dynamsoft-capture-vision-router@2.0.20-dev-20231026110217/dist/cvr.js"></script> |
| 38 | + <script src="https://npm.scannerproxy.com/cdn/@dynamsoft/dynamsoft-camera-enhancer@4.0.1-dev-20231023131759/dist/dce.js"></script> |
| 39 | + </head> |
| 40 | + |
| 41 | + <body> |
| 42 | + <h1>Hello World for PWA</h1> |
| 43 | + <div id="div-ui-container" style="width: 100%; height: 80vh"></div> |
| 44 | + <script> |
| 45 | + if (location.protocol === "file:") { |
| 46 | + const message = `The page is opened via file:// and some SDKs may not work properly. Please open the page via https:// or host it on "http://localhost/".`; |
| 47 | + console.warn(message); |
| 48 | + alert(message); |
| 49 | + } |
| 50 | + </script> |
| 51 | + <script> |
| 52 | + if ("serviceWorker" in navigator) { |
| 53 | + navigator.serviceWorker.register("./service-worker.js"); |
| 54 | + } |
| 55 | + </script> |
| 56 | + <script> |
| 57 | + /** LICENSE ALERT - README |
| 58 | + * To use the library, you need to first specify a license key using the API "initLicense" as shown below. |
| 59 | + */ |
| 60 | + |
| 61 | + Dynamsoft.License.LicenseManager.initLicense( |
| 62 | + "DLS2eyJoYW5kc2hha2VDb2RlIjoiNjY2Ni03Nzc3IiwibWFpblNlcnZlclVSTCI6Imh0dHBzOi8vMTkyLjE2OC44LjEyMi9kbHMvIiwib3JnYW5pemF0aW9uSUQiOiI2NjY2IiwiY2hlY2tDb2RlIjoxNTEyMTgzMzg3fQ==" |
| 63 | + ); |
| 64 | + |
| 65 | + /** |
| 66 | + * You can visit https://www.dynamsoft.com/customer/license/trialLicense?utm_source=github&product=dbr&package=js to get your own trial license good for 30 days. |
| 67 | + * Note that if you downloaded this sample from Dynamsoft while logged in, the above license key may already be your own 30-day trial license. |
| 68 | + * For more information, see https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=10.0.20&utm_source=github#specify-the-license or contact support@dynamsoft.com. |
| 69 | + * LICENSE ALERT - THE END |
| 70 | + */ |
| 71 | + |
| 72 | + (async function () { |
| 73 | + try { |
| 74 | + // Create a `CameraEnhancer` instance for camera control and a `CameraView` instance for UI control. |
| 75 | + const cameraView = await Dynamsoft.DCE.CameraView.createInstance(); |
| 76 | + const cameraEnhancer = |
| 77 | + await Dynamsoft.DCE.CameraEnhancer.createInstance(cameraView); |
| 78 | + document |
| 79 | + .querySelector("#div-ui-container") |
| 80 | + .append(cameraView.getUIElement()); // Get default UI and append it to DOM. |
| 81 | + |
| 82 | + // Create a `CaptureVisionRouter` instance and set `CameraEnhancer` instance as its image source. |
| 83 | + const router = |
| 84 | + await Dynamsoft.CVR.CaptureVisionRouter.createInstance(); |
| 85 | + router.setInput(cameraEnhancer); |
| 86 | + |
| 87 | + // Define a callback for results. |
| 88 | + const resultReceiver = new Dynamsoft.CVR.CapturedResultReceiver(); |
| 89 | + resultReceiver.onDecodedBarcodesReceived = (result) => { |
| 90 | + for (let item of result.barcodesResultItems) { |
| 91 | + console.log(item.text); |
| 92 | + alert(item.text); |
| 93 | + } |
| 94 | + }; |
| 95 | + router.addResultReceiver(resultReceiver); |
| 96 | + |
| 97 | + // Filter out unchecked and duplicate results. |
| 98 | + const filter = new Dynamsoft.Utility.MultiFrameResultCrossFilter(); |
| 99 | + filter.enableResultCrossVerification( |
| 100 | + Dynamsoft.Core.EnumCapturedResultItemType.CRIT_BARCODE, |
| 101 | + true |
| 102 | + ); // Filter out unchecked barcodes. |
| 103 | + // Filter out duplicate barcodes within 3 seconds. |
| 104 | + filter.enableResultDeduplication( |
| 105 | + Dynamsoft.Core.EnumCapturedResultItemType.CRIT_BARCODE, |
| 106 | + true |
| 107 | + ); |
| 108 | + filter.setDuplicateForgetTime( |
| 109 | + Dynamsoft.Core.EnumCapturedResultItemType.CRIT_BARCODE, |
| 110 | + 3000 |
| 111 | + ); |
| 112 | + await router.addResultFilter(filter); |
| 113 | + |
| 114 | + // Open camera and start scanning single barcode. |
| 115 | + await cameraEnhancer.open(); |
| 116 | + await router.startCapturing("ReadSingleBarcode"); |
| 117 | + } catch (ex) { |
| 118 | + let errMsg; |
| 119 | + if (ex.message.includes("network connection error")) { |
| 120 | + errMsg = |
| 121 | + "Failed to connect to Dynamsoft License Server: network connection error. Check your Internet connection or contact Dynamsoft Support (support@dynamsoft.com) to acquire an offline license."; |
| 122 | + } else { |
| 123 | + errMsg = ex.message || ex; |
| 124 | + } |
| 125 | + console.error(errMsg); |
| 126 | + alert(errMsg); |
| 127 | + } |
| 128 | + })(); |
| 129 | + </script> |
| 130 | + </body> |
| 131 | +</html> |
0 commit comments