js_setup_usage.html 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="utf-8">
  5. <title>Hello OpenCV.js</title>
  6. <link href="js_example_style.css" rel="stylesheet" type="text/css" />
  7. </head>
  8. <body>
  9. <h2>Hello OpenCV.js</h2>
  10. <p id="status">OpenCV.js is loading...</p>
  11. <div>
  12. <table cellpadding="0" cellspacing="0" width="0" border="0">
  13. <tr>
  14. <td>
  15. <img id="imageSrc" alt="No Image" class="small" />
  16. </td>
  17. <td>
  18. <canvas id="canvasOutput" class="small" height="300px"></canvas>
  19. </td>
  20. </tr>
  21. <tr>
  22. <td>
  23. <div class="caption">imageSrc <input type="file" id="fileInput" name="file" accept="image/*" /></div>
  24. </td>
  25. <td>
  26. <div class="caption">canvasOutput</div>
  27. </td>
  28. </tr>
  29. </table>
  30. </div>
  31. <script src="utils.js" type="text/javascript"></script>
  32. <script type="text/javascript">
  33. let imgElement = document.getElementById('imageSrc');
  34. let inputElement = document.getElementById('fileInput');
  35. inputElement.addEventListener('change', (e) => {
  36. imgElement.src = URL.createObjectURL(e.target.files[0]);
  37. }, false);
  38. imgElement.onload = function() {
  39. let mat = cv.imread(imgElement);
  40. cv.imshow('canvasOutput', mat);
  41. mat.delete();
  42. };
  43. function onOpenCvReady() { // eslint-disable-line no-unused-vars
  44. document.getElementById('status').innerHTML = '<b>OpenCV.js is ready</b>.' +
  45. 'You can upload an image.<br>' +
  46. 'The <b>imageSrc</b> is a &lt;img&gt; element used as cv.Mat input. ' +
  47. 'The <b>canvasOutput</b> is a &lt;canvas&gt; element used as cv.Mat output.';
  48. }
  49. function onOpenCvError() { // eslint-disable-line no-unused-vars
  50. let element = document.getElementById('status');
  51. element.setAttribute('class', 'err');
  52. element.innerHTML = 'Failed to load opencv.js';
  53. }
  54. </script>
  55. <script async src="opencv.js" type="text/javascript" onload="onOpenCvReady();" onerror="onOpenCvError();"></script>
  56. </body>
  57. </html>