|
| 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 and hightlight results with custom style." |
| 9 | + /> |
| 10 | + <meta name="keywords" content="read barcode from camera, custom style" /> |
| 11 | + <link |
| 12 | + rel="canonical" |
| 13 | + href="https://demo.dynamsoft.com/Samples/DBR/JS/4.use-case/3.highlight-results-with-custom-style.html" |
| 14 | + /> |
| 15 | + <title>Dynamsoft Barcode Reader Sample - Highlight Results with Custom Style</title> |
| 16 | + <!-- |
| 17 | + This sample makes use of the library hosted by the CDN jsDelivr. If you would rather use the |
| 18 | + library offline. Please see the guide on how to host the library: |
| 19 | + https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=latest#host-the-library-yourself-recommended |
| 20 | + --> |
| 21 | + <script src=" https://cdn.jsdelivr.net/npm/[email protected]/dist/dbr.js" ></script> |
| 22 | + </head> |
| 23 | + <body> |
| 24 | + <div id="div-ui-container" style="width: 100%; height: 100vh"></div> |
| 25 | + <script> |
| 26 | + if(location.protocol === "file:") { |
| 27 | + const message = `The page is opened via file:// and "BarcodeScanner" may not work properly. Please open the page via https:// or host it on "http://localhost/".`; |
| 28 | + console.warn(message); |
| 29 | + alert(message); |
| 30 | + } |
| 31 | + </script> |
| 32 | + <script> |
| 33 | + /** LICENSE ALERT - README |
| 34 | + * To use the library, you need to first specify a license key using the API "license" as shown below. |
| 35 | + */ |
| 36 | + |
| 37 | + Dynamsoft.DBR.BarcodeReader.license = "DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9"; |
| 38 | + |
| 39 | + /** |
| 40 | + * 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. |
| 41 | + * 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. |
| 42 | + * For more information, see https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=9.6.11&utm_source=github#specify-the-license or contact [email protected]. |
| 43 | + * LICENSE ALERT - THE END |
| 44 | + */ |
| 45 | + |
| 46 | + let scanner; |
| 47 | + const highlightResultMasks = []; |
| 48 | + |
| 49 | + (async () => { |
| 50 | + try { |
| 51 | + scanner = await Dynamsoft.DBR.BarcodeScanner.createInstance(); |
| 52 | + |
| 53 | + // Get ui by 'getUIElement()' and append it to DOM. |
| 54 | + document |
| 55 | + .querySelector("#div-ui-container") |
| 56 | + .append(scanner.getUIElement()); |
| 57 | + |
| 58 | + // Hide the built-in shape which highlights a found barcode. |
| 59 | + scanner.barcodeFillStyle = "transparent"; |
| 60 | + scanner.barcodeStrokeStyle = "transparent"; |
| 61 | + |
| 62 | + /** |
| 63 | + * 'onFrameRead' is triggered after the library finishes reading a frame image. |
| 64 | + * There can be multiple barcodes on one image. |
| 65 | + */ |
| 66 | + scanner.onFrameRead = (results) => { |
| 67 | + console.log(results); |
| 68 | + |
| 69 | + // Clear previous highlight result masks. |
| 70 | + highlightResultMasks.forEach(mask => mask.remove()); |
| 71 | + highlightResultMasks.length = 0; |
| 72 | + |
| 73 | + for (let result of results) { |
| 74 | + const p1 = { |
| 75 | + x: result.localizationResult.x1, |
| 76 | + y: result.localizationResult.y1 |
| 77 | + }; |
| 78 | + const p2 = { |
| 79 | + x: result.localizationResult.x3, |
| 80 | + y: result.localizationResult.y3 |
| 81 | + }; |
| 82 | + /** |
| 83 | + * 'convertToClientCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the viewport. |
| 84 | + * Then we can place a div element according to the converted coordinate. |
| 85 | + */ |
| 86 | + const convertedP1 = scanner.convertToClientCoordinates(p1); |
| 87 | + const convertedP2 = scanner.convertToClientCoordinates(p2); |
| 88 | + mask = document.createElement("div"); |
| 89 | + mask.style.position = "fixed"; |
| 90 | + mask.style.left = convertedP1.x+"px"; |
| 91 | + mask.style.top = convertedP1.y+"px"; |
| 92 | + mask.style.width = convertedP2.x-convertedP1.x+"px"; |
| 93 | + mask.style.height = convertedP2.y-convertedP1.y+"px"; |
| 94 | + mask.style.border = "2px dashed red"; |
| 95 | + document.body.append(mask); |
| 96 | + highlightResultMasks.push(mask); |
| 97 | + } |
| 98 | + }; |
| 99 | + |
| 100 | + /** |
| 101 | + * 'open()' opens the camera. |
| 102 | + * After that, the library starts to scan the frame images continuously. |
| 103 | + */ |
| 104 | + await scanner.open(); |
| 105 | + } catch (ex) { |
| 106 | + let errMsg; |
| 107 | + if (ex.message.includes("network connection error")) { |
| 108 | + errMsg = |
| 109 | + "Failed to connect to Dynamsoft License Server: network connection error. Check your Internet connection or contact Dynamsoft Support ([email protected]) to acquire an offline license."; |
| 110 | + } else { |
| 111 | + errMsg = ex.message || ex; |
| 112 | + } |
| 113 | + console.error(errMsg); |
| 114 | + alert(errMsg); |
| 115 | + } |
| 116 | + })(); |
| 117 | + </script> |
| 118 | + </body> |
| 119 | +</html> |
0 commit comments