Skip to content

Commit fe08e70

Browse files
committed
Update README.md
1 parent bd32a4a commit fe08e70

File tree

1 file changed

+84
-24
lines changed

1 file changed

+84
-24
lines changed

2.ui-tweaking/README.md

Lines changed: 84 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,106 @@
1-
# UI Customization Sample Set
1+
# JavaScript UI Customization Samples
22

3-
In this sample set, we will focus on the library's flexible UI that allows developers to create the look and feel for their web application that they desire. To learn more about UI customization, please refer to its corresponding [section](https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=latest#customize-the-ui) in the user guide.
3+
Dynamsoft Barcode Reader JavaScript SDK (hereafter called "the library") provides a built-in GUI to help developers build an interactive barcode reading interface.
44

5-
Let's begin by breaking down each of the samples in this set
5+
In this sample set, we will focus on the library's flexible UI that allows developers to create the look and feel for their web application that they desire. To learn more about UI customization, please refer to its corresponding [section](https://www.dynamsoft.com/barcode-reader/docs/web/programming/javascript/user-guide/?ver=latest#customize-the-ui-optional) in the user guide.
66

7-
## Use the Default Camera UI and Display Barcode Results in Real-Time
7+
## Default GUI
88

9-
In this sample, we perform simple tweak of the default UI to include a couple of `span` elements that will display the barcode text and barcode format as they are detected via video. Our basic 'Hello World' samples displayed the results in the console or via an alert box, so this sample will offer a more user-friendly way of showing the results as they are found.
9+
The following official sample demonstrates the default GUI built into the library.
1010

11-
In the `onFrameRead` event function, a pair of `span` elements are created for each found barcode. The `span` elements are then populated with the barcode text and the barcode format.
11+
* <a target = "_blank" href="https://demo.dynamsoft.com/Samples/DBR/JS/2.ui-tweaking/1.read-video-show-result.html">Use the Default Camera UI - Demo</a>
12+
* <a target = "_blank" href="https://github.com/Dynamsoft/barcode-reader-javascript-samples/blob/main/2.ui-tweaking/1.read-video-show-result.html">Use the Default Camera UI - Source Code</a>
1213

13-
This is a simple tweak to the UI, but offers a much less intrusive way of displaying the barcode results as they are acquired.
14+
**GUI Elements**
1415

15-
## Hide Display Controls
16+
If you check the GUI on the demo page, you will find that it consists of the following elements
1617

17-
As explained in the user guide, the default UI comes with 3 core components: a video viewer, a source select dropdown, and a resolution select dropdown. The UI also comes with a built-in close button which allows the user to close the video viewer and barcode scanner.
18+
* The video element that streams the video from a camera
19+
* A laser beam moving vertically which indicates the working status of the library
20+
* A dropdown box for selecting a camera
21+
* A dropdown box for specifying the resolution for the selected camera
22+
* A close button that closes the GUI when clicked
1823

19-
However, you might encounter a situation where you do not require those extra components and would like to simply display the video viewer, and perhaps automatically close it when a barcode is found.
24+
There are a few other elements
2025

21-
In this sample, we've chosen to hide the source select dropdown, the resolution select, and the close button which leaves only the video viewer when `scanner.show()` is called.
26+
* Before the video starts streaming, there is a spinner that indicates the loading process
27+
* When a barcode is found, the location of the barcode is highlighted in the video stream
28+
* If no camera is found on the device or camera access is denied on the page, the GUI will show a camera icon, which, when clicked, allows the user to select a local image or invoke the system camera interface
2229

23-
## Set up External Controls
30+
## Hide Built-in Controllers
2431

25-
In the last sample, we chose to hide the extra control options that come with the default UI. This time around, we will choose to hide the default control elements that come with the UI and set up our own controls instead using simple HTML elements.
32+
As mentioned above, the default UI comes with quite a few elements. Some of them might not fit the style of your own application. The following code snippet removes the camera selector, the resolution selector, the close button as well as changes the backgournd color.
2633

27-
First off, the external elements must be included in the HTML body, whether they are created programatically in JS or declared manually in the HTML code. In our sample, the following external controls are set up:
34+
```html
35+
<div id="UIElement">
36+
<span id='lib-load' style='font-size:x-large' hidden>Loading Library...</span>
37+
<div id="div-ui-container" style="width:100%;height:100%;">
38+
<div class="dce-video-container" style="position:relative;width:100%;height:100%;"></div>
39+
</div>
40+
</div>
41+
```
2842

29-
* `cam_selector_switch` and `options_camera`: These two elements will function together to show all the currently connected and available cameras.
30-
* `res_selector_switch` and `options_resolution`: These two elements will function together to show all the currently connected and available cameras.
31-
* `toggleScanRect`: Cycles through various scan regions, where the scan region will be a subset of the total video viewer. This control is not found in the default UI of the scanner, but can be easily configured using the `region` property of the `RuntimeSettings`. In this sample, we assigned a set of 5 scan regions and each time the user clicks the button, the library cycles to the next scan region.
32-
* `toggleScanLight`: Controls whether the scan "light" is toggled on. Please note that this light does not actually improve the performance of the scanning process, but offers a user-friendly visual representation of the scanning process that is ocurring.
33-
* `toggleSound`: Controls whether the library beeps or not on a found barcode.
43+
```javascript
44+
await scanner.setUIElement(document.getElementById('div-ui-container'));
45+
```
3446

35-
These are just a few of the external controls that you can easily set up using the library API, offering your users more control over the scanner.
47+
Official sample:
3648

37-
## Changing Size of Video Viewer
49+
* <a target = "_blank" href="https://demo.dynamsoft.com/Samples/DBR/JS/2.ui-tweaking/2.read-video-no-extra-control.html">Hide Built-in Controllers - Demo</a>
50+
* <a target = "_blank" href="https://github.com/Dynamsoft/barcode-reader-javascript-samples/blob/main/2.ui-tweaking/2.read-video-no-extra-control.html">Hide Built-in Controllers - Source Code</a>
3851

39-
In this sample, we explore how to switch the video viewer from occupying a limited screen area to a full screen view.
52+
## Use External Controllers
4053

41-
This customization does not involve any of the library API directly, and is done simply via `class` of the viewer, as shown in the click event listener of the `fitPage` button or the `exitFullPage` function.
54+
Based on the previous sample, you can build your own UI elements to control the barcode scanning process.
4255

43-
## Customize the Camera UI
56+
For example, the following code adds a camera selector
57+
58+
```html
59+
<select id="cameraList"></select>
60+
```
61+
62+
```javascript
63+
async function updateCameras() {
64+
let cameras = await scanner.getAllCameras();
65+
let cameraList = document.getElementById('cameraList');
66+
cameraList.options.length = 0;
67+
for (let camera of cameras) {
68+
let opt = new Option(camera.label, camera.deviceId);
69+
cameraList.options.add(opt);
70+
}
71+
}
72+
document.getElementById('cameraList').onchange = async function() {
73+
try {
74+
await scanner.setCurrentCamera(document.getElementById('cameraList').value);
75+
} catch (ex) {
76+
alert('Play video failed: ' + (ex.message || ex));
77+
}
78+
};
79+
```
80+
81+
For more related customization, check out the following official sample:
82+
83+
* <a target = "_blank" href="https://demo.dynamsoft.com/Samples/DBR/JS/2.ui-tweaking/3.read-video-with-external-control.html">Use External Controllers - Demo</a>
84+
* <a target = "_blank" href="https://github.com/Dynamsoft/barcode-reader-javascript-samples/blob/main/2.ui-tweaking/3.read-video-with-external-control.html">Use External Controllers - Source Code</a>
85+
86+
## Enlarge the Video Stream
87+
88+
The library is usually used on mobile devices which have small screens. When scanning barcodes with the mobile camera, the video stream will be limited in the video element and may not be clear enough. Ideally, we should use the whole screen to play the video and find barcodes.
89+
90+
The GUI is pure HTML, so changing the size of the element is very easy. For example, the following enlarges the element to be the full size of the viewport.
91+
92+
```javascript
93+
document.getElementById('UIElement').style.height = '100vh';
94+
document.getElementById('UIElement').style.width = '100vw';
95+
document.getElementById('UIElement').style.position = 'absolute';
96+
```
97+
98+
Check out the following sample on how to enlarge the video stream to read a barcode and then change it back to its normal size.
99+
100+
* <a target = "_blank" href="https://demo.dynamsoft.com/Samples/DBR/JS/2.ui-tweaking/4.difference-video-size.html">Enlarge the Video Stream - Demo</a>
101+
* <a target = "_blank" href="https://github.com/Dynamsoft/barcode-reader-javascript-samples/blob/main/2.ui-tweaking/4.difference-video-size.html">Enlarge the Video Stream - Source Code</a>
102+
103+
## Customize the Default Ui
44104

45105
In the last sample of the set, we show a customized viewer that differs greatly from the default one which demonstrates the possibility in the customization.
46106

0 commit comments

Comments
 (0)