demo_reader.html 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  6. <title>zxing-cpp/wasm reader demo</title>
  7. <link rel="shortcut icon" href="#" />
  8. <script src="zxing_reader.js"></script>
  9. <script>
  10. var zxing = ZXing().then(function(instance) {
  11. zxing = instance; // this line is supposedly not required but with current emsdk it is :-/
  12. });
  13. async function readBarcodes() {
  14. let format = document.getElementById("format").value;
  15. let file = document.getElementById("file").files[0];
  16. let arrayBuffer = await file.arrayBuffer();
  17. let u8Buffer = new Uint8Array(arrayBuffer);
  18. let zxingBuffer = zxing._malloc(u8Buffer.length);
  19. zxing.HEAPU8.set(u8Buffer, zxingBuffer);
  20. let results = zxing.readBarcodesFromImage(zxingBuffer, u8Buffer.length, true, format, 0xff);
  21. zxing._free(zxingBuffer);
  22. showResults(results);
  23. showImageWithResults(file, results);
  24. }
  25. function showImageWithResults(file, results) {
  26. var canvas = document.getElementById("canvas");
  27. var img = new Image()
  28. img.addEventListener('load', function() {
  29. canvas.width = img.width;
  30. canvas.height = img.height;
  31. const ctx = canvas.getContext("2d");
  32. ctx.drawImage(img, 0, 0);
  33. for (let i = 0; i < results.size(); i += 1) {
  34. const { position } = results.get(i);
  35. // Draw outline square
  36. ctx.beginPath();
  37. ctx.moveTo(position.topLeft.x, position.topLeft.y);
  38. ctx.lineTo(position.topRight.x, position.topRight.y);
  39. ctx.lineTo(position.bottomRight.x, position.bottomRight.y);
  40. ctx.lineTo(position.bottomLeft.x, position.bottomLeft.y);
  41. ctx.closePath();
  42. ctx.strokeStyle = "red";
  43. ctx.lineWidth = 4;
  44. ctx.stroke();
  45. // Draw number inside square
  46. const text = {
  47. text: i + 1,
  48. size: Math.abs((position.bottomRight.y - position.topLeft.y)) / 2,
  49. x: (position.topLeft.x + position.bottomRight.x) / 2.0,
  50. y: (position.topLeft.y + position.bottomRight.y) / 2.0,
  51. };
  52. ctx.fillStyle = "white";
  53. ctx.font = `bold ${text.size}px sans`;
  54. ctx.textAlign = "center";
  55. ctx.textBaseline = "middle";
  56. ctx.fillText(text.text, text.x, text.y);
  57. ctx.strokeStyle = "red";
  58. ctx.lineWidth = 2;
  59. ctx.strokeText(text.text, text.x, text.y);
  60. }
  61. });
  62. img.src = URL.createObjectURL(file)
  63. }
  64. function escapeTags(htmlStr) {
  65. return htmlStr.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;").replace(/'/g, "&#39;");
  66. }
  67. function u8a2hex(bytes) {
  68. return bytes.reduce((a, b) => a + b.toString(16).padStart(2, '0') + ' ', '');
  69. }
  70. function showResults(results) {
  71. const resultsDiv = document.getElementById("results");
  72. resultsDiv.innerHTML = "";
  73. if (results.size() === 0) {
  74. resultsDiv.innerHTML += "No " + (document.getElementById("format").value || "barcode") + " found";
  75. }
  76. else {
  77. for (let i = 0; i < results.size(); i += 1) {
  78. const { format, text, bytes, error } = results.get(i);
  79. resultsDiv.innerHTML += "<li>Format: <strong>" + format + "</strong>"
  80. + "<pre>" + (escapeTags(text) || '<font color="red">Error: ' + error + '</font>') + "</pre>"
  81. + "<pre>" + u8a2hex(bytes) + "</pre>"
  82. + "</li>";
  83. }
  84. }
  85. }
  86. </script>
  87. </head>
  88. <body style="text-align: left">
  89. <h2>zxing-cpp/wasm reader demo</h2>
  90. <p>
  91. This is a simple demo of the wasm wrapper of <a href="https://github.com/zxing-cpp/zxing-cpp">zxing-cpp</a>
  92. scanning for barcodes in image files.
  93. </p>
  94. <p></p>
  95. <div>
  96. Scan Format:
  97. <select id="format" onchange='readBarcodes()'>
  98. <option value="" selected="">Any</option>
  99. <option value="Aztec">Aztec</option>
  100. <option value="Codabar">Codabar</option>
  101. <option value="Code39">Code 39</option>
  102. <option value="Code93">Code 93</option>
  103. <option value="Code128">Code 128</option>
  104. <option value="DataMatrix">DataMatrix</option>
  105. <option value="DataBar">DataBar</option>
  106. <option value="DataBarExpanded">DataBarExpanded</option>
  107. <option value="DataBarLimited">DataBarLimited</option>
  108. <option value="DXFilmEdge">DXFilmEdge</option>
  109. <option value="EAN8">EAN-8</option>
  110. <option value="EAN13">EAN-13</option>
  111. <option value="ITF">ITF</option>
  112. <option value="PDF417">PDF417</option>
  113. <option value="QRCode">QR Code</option>
  114. <option value="MicroQRCode">Micro QRCode</option>
  115. <option value="RMQRCode">rMQR Code</option>
  116. <option value="UPCA">UPC-A</option>
  117. <option value="UPCE">UPC-E</option>
  118. <option value="LinearCodes">Linear Codes</option>
  119. <option value="MatrixCodes">Matrix Codes</option>
  120. </select>
  121. &nbsp;&nbsp;
  122. Image File: <input id="file" type="file" accept="image/png, image/jpeg" onchange="readBarcodes()" />
  123. <br /><br />
  124. <canvas id="canvas" width="640" height="480"></canvas>
  125. <br /><br />
  126. <ol id="results"></ol>
  127. </div>
  128. </body>
  129. </html>