|
| 1 | +--- |
| 2 | +layout: default-layout |
| 3 | +title: User Guide - Dynamsoft Camera Enhancer for JavaScript |
| 4 | +description: This is the user guide of Dynamsoft Camera Enhancer JavaScript SDK. |
| 5 | +keywords: user guide, javascript, js |
| 6 | +breadcrumbText: User Guide |
| 7 | +noTitleIndex: true |
| 8 | +needGenerateH3Content: true |
| 9 | +needAutoGenerateSidebar: true |
| 10 | +--- |
| 11 | + |
| 12 | +# Dynamsoft Camera Enhancer for Your Website |
| 13 | + |
| 14 | +Dynamsoft Camera Enhancer allows your website to easily control the camera in the browser on desktop or mobile devices. |
| 15 | + |
| 16 | +With the SDK integrated, your users can open your website in a browser, access their cameras to stream live video and acquire real-time frames for further processing. The SDK also supports simple shape drawing, allowing information to be presented directly on the video stream or over a static image for integrated user interaction. |
| 17 | + |
| 18 | +In this guide, you will learn step by step on how to integrate the Dynamsoft Camera Enhancer SDK into your website. |
| 19 | + |
| 20 | +- [Dynamsoft Camera Enhancer for Your Website](#dynamsoft-camera-enhancer-for-your-website) |
| 21 | + - [Adding the SDK](#adding-the-sdk) |
| 22 | + - [Use a CDN](#use-a-cdn) |
| 23 | + - [Host the SDK yourself](#host-the-sdk-yourself) |
| 24 | + - [Using the SDK](#using-the-sdk) |
| 25 | + - [Stream live video](#stream-live-video) |
| 26 | + - [Capture image frames](#capture-image-frames) |
| 27 | + - [Use ImageEditorView](#use-imageeditorview) |
| 28 | + - [Customize the UI](#customize-the-ui) |
| 29 | + - [Hosting the SDK](#hosting-the-sdk) |
| 30 | + - [Step One: Deploy the dist folder](#step-one-deploy-the-dist-folder) |
| 31 | + - [Step Two: Configure the Server](#step-two-configure-the-server) |
| 32 | + - [Step Three: Add the SDK from the server](#step-three-add-the-sdk-from-the-server) |
| 33 | + - [FAQ](#faq) |
| 34 | + - [Can I open the web page directly from the hard drive?](#can-i-open-the-web-page-directly-from-the-hard-drive) |
| 35 | + - [Why can't I use my camera?](#why-cant-i-use-my-camera) |
| 36 | + - [API Documentation](#api-documentation) |
| 37 | + - [System Requirements](#system-requirements) |
| 38 | + - [Next Steps](#next-steps) |
| 39 | + |
| 40 | +## Adding the SDK |
| 41 | + |
| 42 | +To use the SDK, we first add the following packages: |
| 43 | + |
| 44 | +1. `dynamsoft-camera-enhancer`: Required, it provides the camera related functionalities used in this guide. |
| 45 | +2. `dynamsoft-core`: Required, it includes basic classes, interfaces, and enumerations that are shared between all Dynamsoft SDKs. |
| 46 | +3. `dynamsoft-license`: Optional, if you wish to utilize advanced features such as auto-zoom, enhanced focus, and tap-to-focus, you will need it. |
| 47 | + |
| 48 | +### Use a CDN |
| 49 | + |
| 50 | +The simplest way to add the SDK is to use a CDN such as [jsDelivr](https://jsdelivr.com/) or [UNPKG](https://unpkg.com/). |
| 51 | + |
| 52 | +- jsDelivr |
| 53 | + |
| 54 | + ```html |
| 55 | + <script src="https://cdn.jsdelivr.net/npm/dynamsoft-core@3.4.20/dist/core.js"></script> |
| 56 | + <script src="https://cdn.jsdelivr.net/npm/dynamsoft-license@3.4.21/dist/license.js"></script> |
| 57 | + <script src="https://cdn.jsdelivr.net/npm/dynamsoft-camera-enhancer@4.1.0/dist/dce.js"></script> |
| 58 | + ``` |
| 59 | + |
| 60 | +- UNPKG |
| 61 | + |
| 62 | + ```html |
| 63 | + <script src="https://unpkg.com/dynamsoft-core@3.4.20/dist/core.js"></script> |
| 64 | + <script src="https://unpkg.com/dynamsoft-license@3.4.21/dist/license.js"></script> |
| 65 | + <script src="https://unpkg.com/dynamsoft-camera-enhancer@4.1.0/dist/dce.js"></script> |
| 66 | + ``` |
| 67 | + |
| 68 | +<!-- > In some rare cases, you might not be able to access the CDN. If this happens, you can use |
| 69 | +>- [https://download2.dynamsoft.com/packages/dynamsoft-core@3.4.20/dist/core.js](https://download2.dynamsoft.com/packages/dynamsoft-core@3.2.10/dist/core.js) |
| 70 | +>- [https://download2.dynamsoft.com/packages/dynamsoft-license@3.4.21/dist/license.js](https://download2.dynamsoft.com/packages/dynamsoft-license@3.2.10/dist/license.js) |
| 71 | +>- [https://download2.dynamsoft.com/packages/dynamsoft-camera-enhancer@4.1.0/dist/dce.js](https://download2.dynamsoft.com/packages/dynamsoft-camera-enhancer@4.0.2/dist/dce.js) --> |
| 72 | + |
| 73 | +### Host the SDK yourself |
| 74 | + |
| 75 | +Besides using the CDN, you can also download the SDK and host it locally. |
| 76 | + |
| 77 | +The following shows a few ways to download the SDK. |
| 78 | + |
| 79 | +- yarn |
| 80 | + |
| 81 | + ```cmd |
| 82 | + yarn add dynamsoft-camera-enhancer |
| 83 | + ``` |
| 84 | + |
| 85 | +- npm |
| 86 | + |
| 87 | + ```cmd |
| 88 | + npm install dynamsoft-camera-enhancer --save |
| 89 | + ``` |
| 90 | + |
| 91 | +> Note: Upon installation of `dynamsoft-camera-enhancer`, a compatible versions of `dynamsoft-core` will be automatically installed if not installed already. |
| 92 | +
|
| 93 | +Depending on how you downloaded the SDK and where you put it. You can typically add it like this: |
| 94 | + |
| 95 | +```html |
| 96 | +<script src="./node_modules/dynamsoft-core/dist/core.js"></script> |
| 97 | +<script src="./node_modules/dynamsoft-camera-enhancer/dist/dce.js"></script> |
| 98 | +``` |
| 99 | + |
| 100 | +Read more on [how to host the SDK](#hosting-the-sdk). |
| 101 | + |
| 102 | +## Using the SDK |
| 103 | + |
| 104 | +### Stream live video |
| 105 | + |
| 106 | +We can create a `CameraEnhancer` instance to open and show the video stream on the page. The following code snippet demonstrates the complete process: |
| 107 | + |
| 108 | +1. Create a `CameraView` instance. In our case, it's named `cameraView`; |
| 109 | +2. Pass `cameraView` to the method `createInstance()` to create a `CameraEnhancer` instance. This way, `cameraView` is bound to the created object `cameraEnhancer`; |
| 110 | +3. Show the UI element of `cameraView` by appending it to an existing element in the DOM; |
| 111 | +4. Open the video stream which will show up in the UI Element. |
| 112 | + |
| 113 | +```html |
| 114 | +<!-- Define an element to hold the UI element --> |
| 115 | +<div id="enhancerUIContainer" style="width:1280px;height:720px;"></div> |
| 116 | +<script> |
| 117 | + (async () => { |
| 118 | + // Initializes the license for using the SDK in the application. |
| 119 | + Dynamsoft.License.LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9"); |
| 120 | + // A default UI will be used if no parameter is passed to 'CameraView.createInstance()'. |
| 121 | + let cameraView = await Dynamsoft.DCE.CameraView.createInstance(); |
| 122 | + // Create 'CameraEnhancer' instance and pass 'cameraView' to it. |
| 123 | + let cameraEnhancer = await Dynamsoft.DCE.CameraEnhancer.createInstance(cameraView); |
| 124 | + // Get 'CameraView' instance's UI and append it to DOM. |
| 125 | + document.querySelector("#enhancerUIContainer").append(cameraView.getUIElement()); |
| 126 | + // Open the camera and start streaming the video on the page. |
| 127 | + await cameraEnhancer.open(); |
| 128 | + })(); |
| 129 | +</script> |
| 130 | +``` |
| 131 | + |
| 132 | +### Capture image frames |
| 133 | + |
| 134 | +There are two ways to capture image frames with Dynamsoft Camera Enhancer. |
| 135 | + |
| 136 | +- Capture a single latest frame |
| 137 | + |
| 138 | + This is done with the method [fetchImage()](https://www.dynamsoft.com/camera-enhancer/docs/web/programming/javascript/api-reference/acquisition.html#fetchimage) as shown in the following code snippet: |
| 139 | + |
| 140 | + ```javascript |
| 141 | + let img = cameraEnhancer.fetchImage(); |
| 142 | + document.body.appendChild(img.toCanvas()); |
| 143 | + ``` |
| 144 | + |
| 145 | +- Start a capture loop that captures multiple frames at a steady interval into a buffer and then read frames from the buffer. The following code snippet shows how it works: |
| 146 | + |
| 147 | + ```javascript |
| 148 | + // Capture an image frame every 2 seconds and subsequently append it to the buffer. |
| 149 | + cameraEnhancer.setImageFetchInterval(2000); |
| 150 | + // The event "frameAddedToBuffer" is triggered whenever a new frame is added to the buffer. |
| 151 | + cameraEnhancer.on("frameAddedToBuffer", () => { |
| 152 | + let img = cameraEnhancer.getImage(); |
| 153 | + console.log(img); |
| 154 | + }); |
| 155 | + // Start the built-in fetching loop. |
| 156 | + cameraEnhancer.startFetching(); |
| 157 | + ``` |
| 158 | + |
| 159 | +### Use ImageEditorView |
| 160 | + |
| 161 | +The other view class ImageEditorView is designed to show a single image. The following code snippet shows a single frame in an `ImageEditorView` instance and draws a rectangle on the image: |
| 162 | + |
| 163 | +```html |
| 164 | +<div id="imageEditorContainer" style="width:1280px;height:720px;"></div> |
| 165 | +``` |
| 166 | + |
| 167 | +```javascript |
| 168 | +// The default UI will be used if no parameter is provided to 'ImageEditorView.createInstance()'. |
| 169 | +let editorView = await Dynamsoft.DCE.ImageEditorView.createInstance(); |
| 170 | +// Retrieve the UI of the 'editorView' instance and append it to the DOM. |
| 171 | +document.querySelector("#imageEditorContainer").append(editorView.getUIElement()); |
| 172 | +// When the video is already playing, capture an image and draw it on the ImageEditorView. |
| 173 | +let img = cameraEnhancer.fetchImage(); |
| 174 | +editorView.setOriginalImage(img); |
| 175 | +// Draw a rectangle on the image. |
| 176 | +let drawingLayer = editorView.createDrawingLayer(); |
| 177 | +let rect = new Dynamsoft.DCE.RectDrawingItem( |
| 178 | + { |
| 179 | + x: 100, |
| 180 | + y: 100, |
| 181 | + width: 1070, |
| 182 | + height: 520 |
| 183 | + }); |
| 184 | +drawingLayer.addDrawingItems([rect]); |
| 185 | +``` |
| 186 | + |
| 187 | +Once the rectangle appears on the image, you can click to select it and adjust its position, size, etc. |
| 188 | + |
| 189 | +Read more about [How to draw shapes](https://www.dynamsoft.com/camera-enhancer/docs/web/programming/javascript/user-guide/features/draw-shapes.html). |
| 190 | + |
| 191 | +### Customize the UI |
| 192 | + |
| 193 | +Dynamsoft Camera Enhancer (DCE) offers extensive customization options. If you only need to adjust the UI without delving into other features, this section is for you. |
| 194 | + |
| 195 | +**Use the built-in UI** |
| 196 | + |
| 197 | +DCE includes several pre-defined UIs in the `DCE-LIB-PACKAGE/dist/xxxx.ui.html` (or `node_modules/dynamsoft-camera-enhancer/dist/xxxx.ui.html`). |
| 198 | + |
| 199 | +* `dce.ui.html`: The default UI used by CameraView.createInstance() for simple use cases. |
| 200 | +* `dce.mobile-native.ui.html`: A mobile-inspired UI with auto flash, suitable for most websites. Activate it with `CameraView.createInstance('@engineResourcePath/dce.mobile-native.ui.html')`. |
| 201 | + |
| 202 | +When `cameraEnhancer.open()` is called, the UI elements are dynamically bound to functions based on their class names, such as `dce-xxxx`. |
| 203 | + |
| 204 | +**Modify the UI dynamically** |
| 205 | + |
| 206 | +To customize the UI, call `view.getUIElement()` to access the HTMLElement containing all UI elements. Before `cameraEnhancer.open()`, you can modify the UI by editing CSS, adding/removing elements, or restructuring the HTML. This usually works without issues, though testing is recommended. |
| 207 | + |
| 208 | +After `cameraEnhancer.open()`, UI adjustments are possible via JavaScript, but handle elements with `dce-` prefixed class names carefully, as they may be linked to specific logic. |
| 209 | + |
| 210 | +**Define the UI in a separate HTML** |
| 211 | + |
| 212 | +For less dynamic but more structured customization, create a copy of the desired `xxxx.ui.html`, modify it as needed, and store it in your project's `public` folder. Use this customized UI with `CameraView.createInstance('PATH-TO/xxxx.ui.html')`. |
| 213 | + |
| 214 | +**Integrate HTML into Your Project** |
| 215 | + |
| 216 | +Alternatively, `CameraView.createInstance()` accepts an `HTMLElement` directly. This allows you to build and manage the UI within your webpage. For example, set the UI using `CameraView.createInstance(document.getElementById('my-custom-ui'))`. |
| 217 | + |
| 218 | +Let's see the following example. |
| 219 | + |
| 220 | +**Practise: customize base on `dce.ui.html`** |
| 221 | + |
| 222 | +Start by opening dce.ui.html in your editor to review its content. You can copy several elements from this file. |
| 223 | + |
| 224 | +Next, create a new HTML page, beginning by embedding only the video: |
| 225 | + |
| 226 | +```html |
| 227 | +<div id="enhancerUIContainer" style="width:1280px;height:720px;background:#ddd;" > |
| 228 | + <div class="dce-video-container" style="width:100%;height:100%;"></div> |
| 229 | +</div> |
| 230 | +<script> |
| 231 | + (async () => { |
| 232 | + // Initializes the license for using the SDK in the application. |
| 233 | + Dynamsoft.License.LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9"); |
| 234 | + // Create 'CameraView' instance and pass the element "enhancerUIContainer" as its UI. |
| 235 | + let cameraView = await Dynamsoft.DCE.CameraView.createInstance(document.getElementById("enhancerUIContainer")); |
| 236 | + let cameraEnhancer = await Dynamsoft.DCE.CameraEnhancer.createInstance(cameraView); |
| 237 | + await cameraEnhancer.open(); |
| 238 | + })(); |
| 239 | +</script> |
| 240 | +``` |
| 241 | + |
| 242 | +> The video element will automatically be created and appended to the `dce-video-container` div. |
| 243 | +
|
| 244 | +Next, add the camera and resolution list. If the classes match the default ones (`dce-sel-camera` and `dce-sel-resolution`), the SDK will populate them and manage switching. |
| 245 | + |
| 246 | +```html |
| 247 | +<div id="enhancerUIContainer" style="position:relative;width:1280px;height:720px;background:#ddd;" > |
| 248 | + <div class="dce-video-container" style="width:100%;height:100%;"></div> |
| 249 | + <div style="position: absolute;left: 0;top: 0;"> |
| 250 | + <select class="dce-sel-camera" style="display: block;"></select> |
| 251 | + </div> |
| 252 | +</div> |
| 253 | +``` |
| 254 | + |
| 255 | +```html |
| 256 | +<div id="enhancerUIContainer" style="position:relative;width:1280px;height:720px;background:#ddd;" > |
| 257 | + <div class="dce-video-container" style="width:100%;height:100%;"></div> |
| 258 | + <div style="position:absolute;left:0;top:0;"> |
| 259 | + <select class="dce-sel-camera" style="display:block;"></select> |
| 260 | + <select class="dce-sel-resolution" style="display:block;margin-top:5px;"></select> |
| 261 | + </div> |
| 262 | +</div> |
| 263 | +``` |
| 264 | + |
| 265 | +> By default, only 3 hard-coded resolutions (1920 x 1080, 1280 x 720, 640 x 480) are available. You can show a customized set of options by hardcoding them. |
| 266 | +
|
| 267 | +```html |
| 268 | +<select class="dce-sel-resolution"> |
| 269 | + <option class="dce-opt-gotResolution" value="got"></option> |
| 270 | + <option data-width="1920" data-height="1080">1920x1080</option> |
| 271 | + <option data-width="1280" data-height="720">1280x720</option> |
| 272 | + <option data-width="640" data-height="480">640x480</option> |
| 273 | +</select> |
| 274 | +``` |
| 275 | + |
| 276 | +> Ensure the selected resolution is supported by the camera. If not, the closest supported resolution will be used. The `dce-opt-gotResolution` class shows the **actual resolution**. |
| 277 | +
|
| 278 | +**End of the section: customize the UI** |
| 279 | + |
| 280 | +This concludes the UI customization section. Whether using built-in UIs or custom HTML, the process is straightforward. |
| 281 | + |
| 282 | +## Hosting the SDK |
| 283 | + |
| 284 | +### Step One: Deploy the dist folder |
| 285 | + |
| 286 | +Once you have downloaded the SDK, you can locate the "dist" directory and copy it to your project (usually as part of your website / web application). The following shows some of the files in this directory: |
| 287 | + |
| 288 | +- `dce.js` // The main SDK file |
| 289 | +- `dce.mjs` // For using the SDK as a module (`<script type="module">`) |
| 290 | +- `dce.ui.html` // Defines the default cameraEnhancer UI |
| 291 | + |
| 292 | +### Step Two: Configure the Server |
| 293 | + |
| 294 | +- Enable HTTPS |
| 295 | + |
| 296 | + To use the SDK, you must access your website / web application via a secure HTTPS connection. This is due to browser security restrictions which only grant camera video streaming access to a [secure context](https://developer.mozilla.org/en-US/docs/Web/Security/Secure_Contexts). |
| 297 | + |
| 298 | + > For convenience, self-signed certificates can be used during development and testing. You can also use "http://localhost" which allows camera access without an SSL certificate. |
| 299 | +
|
| 300 | +### Step Three: Add the SDK from the server |
| 301 | + |
| 302 | +Now that the SDK is hosted on your server, you can add it accordingly. |
| 303 | + |
| 304 | +```html |
| 305 | +<script src="https://www.yourwebsite.com/PATH-TO-THE-SDK/dynamsoft-camera-enhancer/dist/dce.js"></script> |
| 306 | +``` |
| 307 | + |
| 308 | +## FAQ |
| 309 | + |
| 310 | +### Can I open the web page directly from the hard drive? |
| 311 | + |
| 312 | +Yes, for simple testing purposes, it's ok to open the file directly from the hard drive (`file://`). However, you might encounter some issues in doing so (cannot access camera, etc.). The recommendation is to deploy this page to your web server and run it over **HTTPS** or use "http://localhost" during development. |
| 313 | + |
| 314 | +If you don't have a ready-to-use web server but have a package manager like *npm* or *yarn*, you can set up a simple HTTP server in minutes. Check out [`http-server` on npm](https://www.npmjs.com/package/http-server) or [`http-server` on yarn](https://yarnpkg.com/package/http-server). |
| 315 | + |
| 316 | +### Why can't I use my camera? |
| 317 | + |
| 318 | +If you open the web page as `http://` , the camera may not work and you see a warnings or an error in the browser console. This is usually because the API [getUserMedia](https://developer.mozilla.org/en-US/docs/Web/API/MediaDevices/getUserMedia) requires HTTPS to access the camera. |
| 319 | + |
| 320 | +To make sure your web application can access the camera, please configure your web server to support HTTPS. The following links may help. |
| 321 | + |
| 322 | +- NGINX: [Configuring HTTPS servers](https://nginx.org/en/docs/http/configuring_https_servers.html) |
| 323 | +- IIS: [Create a Self Signed Certificate in IIS](https://aboutssl.org/how-to-create-a-self-signed-certificate-in-iis/) |
| 324 | +- Tomcat: [Setting Up SSL on Tomcat in 5 minutes](https://dzone.com/articles/setting-ssl-tomcat-5-minutes) |
| 325 | +- Node.js: [npm tls](https://nodejs.org/docs/v0.4.1/api/tls.html) |
| 326 | + |
| 327 | +## API Documentation |
| 328 | + |
| 329 | +You can check out the detailed documentation about the APIs of the SDK at |
| 330 | +[https://www.dynamsoft.com/camera-enhancer/docs/programming/javascript/api-reference/index.html](https://www.dynamsoft.com/camera-enhancer/docs/web/programming/javascript/api-reference/index.html). |
| 331 | + |
| 332 | +## System Requirements |
| 333 | + |
| 334 | +DCE requires the following features to work: |
| 335 | + |
| 336 | +* Secure context (HTTPS deployment) |
| 337 | + |
| 338 | + When deploying your application / website for production, make sure to serve it via a secure HTTPS connection. This is required for two reasons |
| 339 | + |
| 340 | + * Access to the camera video stream is only granted in a security context. Most browsers impose this restriction. |
| 341 | + > Some browsers like Chrome may grant the access for `http://127.0.0.1` and `http://localhost` or even for pages opened directly from the local disk (`file:///...`). This can be helpful for temporary development and test. |
| 342 | + |
| 343 | + * Dynamsoft License requires a secure context to work. |
| 344 | + |
| 345 | +* `MediaDevices`/`getUserMedia` |
| 346 | + |
| 347 | + This API is only required for in-browser video streaming. |
| 348 | + |
| 349 | +* `getSettings` |
| 350 | + |
| 351 | + This API inspects the video input which is a `MediaStreamTrack` object about its constrainable properties. |
| 352 | + |
| 353 | +The following table is a list of supported browsers based on the above requirements: |
| 354 | + |
| 355 | + | Browser Name | Version | |
| 356 | + | :----------: | :-----: | |
| 357 | + | Chrome | v59+ | |
| 358 | + | Firefox | v68+ | |
| 359 | + | Edge | v79+ | |
| 360 | + | Safari | v11+ | |
| 361 | + |
| 362 | +Apart from the browsers, the operating systems may impose some limitations of their own that could restrict the use of the SDK. Browser compatibility ultimately depends on whether the browser on that particular operating system supports the features listed above. |
| 363 | + |
| 364 | +<!-- ## Release Notes |
| 365 | +
|
| 366 | +Learn about what are included in each release at [https://www.dynamsoft.com/camera-enhancer/docs/programming/javascript/release-note/index.html](https://www.dynamsoft.com/camera-enhancer/docs/web/programming/javascript/release-note/index.html). --> |
| 367 | + |
| 368 | +## Next Steps |
| 369 | + |
| 370 | +Now that you have got the SDK integrated, you can choose to move forward in the following directions |
| 371 | + |
| 372 | +1. Check out the [official samples on Github](https://github.com/Dynamsoft/camera-enhancer-javascript-samples). |
| 373 | +2. Learn about the available [APIs](https://www.dynamsoft.com/camera-enhancer/docs/web/programming/javascript/api-reference/index.html). |
| 374 | +3. Learn more about the [Shape Drawing Feature](https://www.dynamsoft.com/camera-enhancer/docs/web/programming/javascript/user-guide/features/draw-shapes.html). |
0 commit comments