|
| 1 | +<!DOCTYPE html> |
| 2 | +<html lang="en"> |
| 3 | + <head> |
| 4 | + <meta charset="UTF-8" /> |
| 5 | + <meta http-equiv="X-UA-Compatible" content="IE=edge" /> |
| 6 | + <meta name="viewport" content="width=device-width, initial-scale=1.0" /> |
| 7 | + <title>Dynamsoft Camera Enhancer Sample - Control Camera</title> |
| 8 | + <script src="https://cdn.jsdelivr.net/npm/dynamsoft-core@3.0.10/dist/core.js"></script> |
| 9 | + <script src="https://cdn.jsdelivr.net/npm/dynamsoft-camera-enhancer@4.0.0/dist/dce.js"></script> |
| 10 | + </head> |
| 11 | + <body> |
| 12 | + <button id="btn-open-camera">open camera</button> |
| 13 | + <button id="btn-close-camera">close camera</button> |
| 14 | + <br /> |
| 15 | + <label for="select-camera">camera: </label> |
| 16 | + <select name="" id="select-camera"></select> |
| 17 | + <br /> |
| 18 | + <label for="select-resolution">resolution: </label> |
| 19 | + <select name="" id="select-resolution"> |
| 20 | + <option value="got-resolution" selected>1920*1080</option> |
| 21 | + <option value="3840*2160">3840*2160</option> |
| 22 | + <option value="1920*1080">1920*1080</option> |
| 23 | + <option value="1280*720">1280*720</option> |
| 24 | + <option value="640*480">640*480</option> |
| 25 | + </select> |
| 26 | + <div |
| 27 | + id="div-ui-container" |
| 28 | + style="width: 90vw; height: 80vh; border: 1px solid black" |
| 29 | + > |
| 30 | + <div class="dce-video-container" style="width: 100%; height: 100%"></div> |
| 31 | + </div> |
| 32 | + <script> |
| 33 | + const uiContainer = document.querySelector("#div-ui-container"); |
| 34 | + const cameraSelection = document.querySelector("#select-camera"); |
| 35 | + const resolutionSelection = document.querySelector("#select-resolution"); |
| 36 | + |
| 37 | + let cameraView, cameraEnhancer; |
| 38 | + |
| 39 | + const init = async () => { |
| 40 | + if (cameraEnhancer) return; |
| 41 | + |
| 42 | + // 'testCameraAccess()' help judge if cameras in the device are available. Also, it makes the first opening of the camera faster. |
| 43 | + const testResult = |
| 44 | + await Dynamsoft.DCE.CameraEnhancer.testCameraAccess(); |
| 45 | + if (!testResult.ok) { |
| 46 | + alert(testResult.message); |
| 47 | + console.error(testResult.message); |
| 48 | + return; |
| 49 | + } |
| 50 | + |
| 51 | + // Create 'CameraView' instance and set 'uiContainer' as its UI. |
| 52 | + cameraView = await Dynamsoft.DCE.CameraView.createInstance(uiContainer); |
| 53 | + // Create 'CameraEnhancer' instance and pass 'cameraView' to it for UI control. |
| 54 | + cameraEnhancer = await Dynamsoft.DCE.CameraEnhancer.createInstance( |
| 55 | + cameraView |
| 56 | + ); |
| 57 | + |
| 58 | + await getCameraList(); |
| 59 | + }; |
| 60 | + |
| 61 | + // Get all cameras in the devices and show them on 'select' element. |
| 62 | + const getCameraList = async () => { |
| 63 | + if (!cameraEnhancer) return null; |
| 64 | + |
| 65 | + const cameraList = await cameraEnhancer.getAllCameras(); |
| 66 | + cameraSelection.innerText = ""; |
| 67 | + for (let camera of cameraList) { |
| 68 | + const option = document.createElement("option"); |
| 69 | + option.innerText = camera.label; |
| 70 | + option.value = camera.deviceId; |
| 71 | + cameraSelection.add(option); |
| 72 | + } |
| 73 | + }; |
| 74 | + |
| 75 | + // Update camera that is using in the DOM. |
| 76 | + const renderCurCamera = () => { |
| 77 | + if (!cameraEnhancer) return; |
| 78 | + const curCamera = cameraEnhancer.getSelectedCamera(); |
| 79 | + for (let option of cameraSelection.options) { |
| 80 | + if (option.value == curCamera.deviceId) { |
| 81 | + cameraSelection.value = option.value; |
| 82 | + break; |
| 83 | + } |
| 84 | + } |
| 85 | + }; |
| 86 | + |
| 87 | + // Update camera's resolution in the DOM. |
| 88 | + const renderCurResolution = () => { |
| 89 | + if (!cameraEnhancer) return; |
| 90 | + const curResolution = cameraEnhancer.getResolution(); |
| 91 | + resolutionSelection.item( |
| 92 | + 0 |
| 93 | + ).innerText = `${curResolution.width}*${curResolution.height}`; |
| 94 | + resolutionSelection.selectedIndex = 0; |
| 95 | + }; |
| 96 | + |
| 97 | + init(); |
| 98 | + |
| 99 | + document.querySelector("#btn-open-camera").onclick = async () => { |
| 100 | + if (!cameraEnhancer) |
| 101 | + throw new Error(`Instance is not yet initialized.`); |
| 102 | + |
| 103 | + await cameraEnhancer.open(); |
| 104 | + renderCurCamera(); |
| 105 | + renderCurResolution(); |
| 106 | + }; |
| 107 | + |
| 108 | + document.querySelector("#btn-close-camera").onclick = async () => { |
| 109 | + if (!cameraEnhancer) return; |
| 110 | + |
| 111 | + await cameraEnhancer.close(); |
| 112 | + }; |
| 113 | + |
| 114 | + cameraSelection.addEventListener("change", async () => { |
| 115 | + if (!cameraEnhancer) return; |
| 116 | + |
| 117 | + await cameraEnhancer.selectCamera(cameraSelection.value); |
| 118 | + renderCurCamera(); |
| 119 | + }); |
| 120 | + |
| 121 | + resolutionSelection.addEventListener("change", async () => { |
| 122 | + if (!cameraEnhancer) return; |
| 123 | + const str = resolutionSelection.value; |
| 124 | + const index = str.indexOf("*"); |
| 125 | + const width = parseInt(str.slice(0, index)), |
| 126 | + height = parseInt(str.slice(index + 1)); |
| 127 | + await cameraEnhancer.setResolution({width, height}); |
| 128 | + renderCurResolution(); |
| 129 | + }); |
| 130 | + </script> |
| 131 | + </body> |
| 132 | +</html> |
0 commit comments