-
Notifications
You must be signed in to change notification settings - Fork 45
Expand file tree
/
Copy pathshow-result-texts-on-the-video.html
More file actions
179 lines (159 loc) · 8.53 KB
/
show-result-texts-on-the-video.html
File metadata and controls
179 lines (159 loc) · 8.53 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta name="description" content="Read barcodes from camera with Dynamsoft Barcode Reader and display result texts as overlays on video." />
<meta name="keywords" content="read barcode from camera, overlay" />
<link rel="canonical" href="https://demo.dynamsoft.com/Samples/DBR/JS/use-case/show-result-texts-on-the-video.html" />
<title>Dynamsoft Barcode Reader Sample - Display Barcode Results as Video Overlays</title>
<style>
.bubble-box-container {
position: absolute;
display: flex;
justify-content: center;
}
.bubble-box-container>.bubble-box {
position: absolute;
width: max-content;
max-width: 20rem;
background: white;
border-radius: 4px;
padding: 4px;
word-break: break-all;
word-wrap: break-word;
box-shadow: 5px 5px 5px 3px rgba(0, 0, 0, 0.2);
transform: translateY(calc(-100% - 4px));
}
.bubble-box-container>.bubble-box::after {
content: "";
position: absolute;
bottom: -8px;
left: calc(50% - 8px);
border-left: 8px solid transparent;
border-top: 8px solid white;
border-right: 8px solid transparent;
}
</style>
</head>
<body>
<h1>Display Barcode Results as Video Overlays</h1>
<h3>Scan barcodes to display results as overlays on video</h3>
<div id="camera-view-container" style="width: 100%; height: 90vh"></div>
<div id="results"></div>
<script src="https://cdn.jsdelivr.net/npm/dynamsoft-barcode-reader-bundle@10.5.3000/dist/dbr.bundle.min.js"></script>
<!-- If the network is unstable or you prefer to self-host the SDK, uncomment the line below to load it locally -->
<!-- <script src="../../../distributables/dbr.bundle.js"></script> -->
<script>
/** LICENSE ALERT - README
* To use the library, you need to first specify a license key using the API "initLicense()" as shown below.
*/
Dynamsoft.License.LicenseManager.initLicense("DLS2eyJvcmdhbml6YXRpb25JRCI6IjIwMDAwMSJ9");
/**
* You can visit https://www.dynamsoft.com/customer/license/trialLicense?utm_source=samples&product=dbr&package=js to get your own trial license good for 30 days.
* Note that if you downloaded this sample from Dynamsoft while logged in, the above license key may already be your own 30-day trial license.
* For more information, see https://www.dynamsoft.com/barcode-reader/docs/web/programming/javascript/user-guide/index.html?ver=10.5.3000&cVer=true#specify-the-license&utm_source=samples or contact support@dynamsoft.com.
* LICENSE ALERT - THE END
*/
// Optional. Used to load wasm resources in advance, reducing latency between video playing and barcode decoding.
Dynamsoft.Core.CoreModule.loadWasm(["DBR"]);
// // If the network is unstable or you prefer to self-host the SDK, uncomment the line below to define the root path of the engine files
// Dynamsoft.Core.CoreModule.engineResourcePaths.rootDirectory = "../../../distributables/";
const resultsContainer = document.querySelector("#results");
(async () => {
try {
// Create a `CameraEnhancer` instance for camera control and a `CameraView` instance for UI control.
const cameraView = await Dynamsoft.DCE.CameraView.createInstance();
const cameraEnhancer = await Dynamsoft.DCE.CameraEnhancer.createInstance(cameraView);
// Get default UI and append it to DOM.
document.querySelector("#camera-view-container").append(cameraView.getUIElement());
// Create a `CaptureVisionRouter` instance and set `CameraEnhancer` instance as its image source.
const cvRouter = await Dynamsoft.CVR.CaptureVisionRouter.createInstance();
cvRouter.setInput(cameraEnhancer);
// Define a callback for results.
const bodyStyle = getComputedStyle(document.body);
cvRouter.addResultReceiver({
onCapturedResultReceived: (result) => {
// Reset results
resultsContainer.innerText = "";
// Determine if body `position` is `static`, `relative`, `absolute`, etc.
const isBodyStyleStatic = bodyStyle.position === "static";
let bodyStaticTopOffset, bodyStaticLeftOffset;
if (isBodyStyleStatic) {
const bodyRect = document.body.getBoundingClientRect();
bodyStaticTopOffset = bodyRect.top + parseFloat(bodyStyle.borderTopWidth);
bodyStaticLeftOffset = bodyRect.left + parseFloat(bodyStyle.borderLeftWidth);
}
// loop through each barcode result
for (let item of result.items) {
if (item.type != Dynamsoft.Core.EnumCapturedResultItemType.CRIT_BARCODE) {
continue; // Skip processing if the result is not a barcode
}
// Get the points of the quadrilateral surrounding the scanned barcode
const p = item.location.points;
// Calculate the top-left and bottom-right coordinates of the quadrilateral
// This will help in positioning the result overlay on top of the scanned barcode
const barcodeTopLeft = {
x: Math.min(p[0].x, p[1].x, p[2].x, p[3].x),
y: Math.min(p[0].y, p[1].y, p[2].y, p[3].y),
};
const barcodeBottomRight = {
x: Math.max(p[0].x, p[1].x, p[2].x, p[3].x),
y: Math.max(p[0].y, p[1].y, p[2].y, p[3].y),
};
// Create an `absolute` positioned container to overlay on top of the video
const bubbleBoxContainer = document.createElement("div");
bubbleBoxContainer.className = "bubble-box-container";
// Create the overlay element to display decoded barcode results
const bubbleBoxOverlay = document.createElement("div");
bubbleBoxOverlay.className = "bubble-box";
bubbleBoxOverlay.innerText = item.text;
bubbleBoxContainer.append(bubbleBoxOverlay);
// Position the overlay container relative to the document or viewport
if (isBodyStyleStatic) {
/**
* 'convertToPageCoordinates()' is used to converts coordinate of a barcode location to the coordinate relative to the document.
* Then we can place a div element according to the converted coordinate.
*/
const pageTopLeft = cameraEnhancer.convertToPageCoordinates(barcodeTopLeft);
const pageBottomRight = cameraEnhancer.convertToPageCoordinates(barcodeBottomRight);
const pageMidX = (pageTopLeft.x + pageBottomRight.x) / 2;
bubbleBoxContainer.style.top = `${pageTopLeft.y}px`;
bubbleBoxContainer.style.left = `${pageMidX}px`;
} else {
// if you set body `position` as `relative`, `absolute`, and so on,
// we'll have to use set the position relative to the viewport.
/**
* 'convertToClientCoordinates()' is used to converts coordinate of a barcode location to the coordinate relative to the viewport.
* Then we can place a div element according to the converted coordinate.
*/
const clientTopLeft = cameraEnhancer.convertToClientCoordinates(barcodeTopLeft);
const clientBottomRight = cameraEnhancer.convertToClientCoordinates(barcodeBottomRight);
const clientMidX = (clientTopLeft.x + clientBottomRight.x) / 2;
bubbleBoxContainer.style.top = `${clientTopLeft.y - bodyStaticTopOffset}px`;
bubbleBoxContainer.style.left = `${clientMidX - bodyStaticLeftOffset}px`;
}
resultsContainer.append(bubbleBoxContainer);
/**
* You can also add more information, such as displaying product images.
*/
}
},
});
// Filter out unchecked and duplicate results.
const filter = new Dynamsoft.Utility.MultiFrameResultCrossFilter();
// Filter out unchecked barcodes.
filter.enableResultCrossVerification("barcode", true);
// Open camera and start scanning single barcode.
await cameraEnhancer.open();
cameraView.setScanLaserVisible(true);
await cvRouter.startCapturing("ReadBarcodes_SpeedFirst");
} catch (ex) {
let errMsg = ex.message || ex;
console.error(errMsg);
alert(errMsg);
}
})();
</script>
</body>
</html>