Skip to content

Commit 2ef2cdc

Browse files
author
Cube
authored
Merge pull request #123 from Dynamsoft/_dev
Dev
2 parents e4be2cd + 707976c commit 2ef2cdc

File tree

1 file changed

+32
-39
lines changed

1 file changed

+32
-39
lines changed

4.use-case/3.show-result-texts-on-the-video.html

Lines changed: 32 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,18 +21,9 @@
2121
https://www.dynamsoft.com/barcode-reader/programming/javascript/user-guide/?ver=latest#host-the-library-yourself-recommended
2222
-->
2323
<script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/dbr.js"></script>
24-
<style>
25-
* {
26-
margin: 0;
27-
padding: 0;
28-
}
29-
body {
30-
position: relative;
31-
}
32-
</style>
3324
</head>
3425
<body>
35-
<div id="div-ui-container" style="width: 100%; height: 100vh"></div>
26+
<div id="div-ui-container" style="width: 100%; height: 90vh"></div>
3627
<div id="div-text-containers"></div>
3728
<script>
3829
if (location.protocol === "file:") {
@@ -80,48 +71,50 @@
8071
divTextContainers.innerText = "";
8172

8273
for (let result of results) {
74+
const l = result.localizationResult;
8375
const p1 = {
84-
x: Math.min(
85-
result.localizationResult.x1,
86-
result.localizationResult.x2,
87-
result.localizationResult.x3,
88-
result.localizationResult.x4
89-
),
90-
y: Math.min(
91-
result.localizationResult.y1,
92-
result.localizationResult.y2,
93-
result.localizationResult.y3,
94-
result.localizationResult.y4
95-
),
76+
x: Math.min(l.x1, l.x2, l.x3, l.x4),
77+
y: Math.min(l.y1, l.y2, l.y3, l.y4),
9678
};
9779
const p2 = {
98-
x: Math.max(
99-
result.localizationResult.x1,
100-
result.localizationResult.x2,
101-
result.localizationResult.x3,
102-
result.localizationResult.x4
103-
),
104-
y: Math.max(
105-
result.localizationResult.y1,
106-
result.localizationResult.y2,
107-
result.localizationResult.y3,
108-
result.localizationResult.y4
109-
),
80+
x: Math.max(l.x1, l.x2, l.x3, l.x4),
81+
y: Math.max(l.y1, l.y2, l.y3, l.y4),
11082
};
83+
11184
/**
11285
* 'convertToPageCoordinates()' is used to converts coordinate of a barcode location to the coordinate related to the document.
11386
* Then we can place a div element according to the converted coordinate.
11487
*/
11588
const convertedP1 = scanner.convertToPageCoordinates(p1);
11689
const convertedP2 = scanner.convertToPageCoordinates(p2);
90+
11791
textContainer = document.createElement("div");
118-
textContainer.style.position = "absolute";
119-
textContainer.style.left = convertedP1.x + "px";
120-
textContainer.style.right = document.body.clientWidth - convertedP2.x + "px";
121-
textContainer.style.bottom = document.body.clientHeight - convertedP1.y + "px";
122-
textContainer.style.color = "#FE8E14";
92+
const bodyStyle = getComputedStyle(document.body);
93+
if (bodyStyle.position !== "static") {
94+
const rect = document.body.getBoundingClientRect();
95+
const borderWidth = parseInt(bodyStyle.borderWidth);
96+
Object.assign(textContainer.style, {
97+
position: "absolute",
98+
left: convertedP1.x - rect.left - borderWidth + "px",
99+
right: rect.right - convertedP2.x - borderWidth + "px",
100+
bottom: rect.bottom - convertedP1.y - borderWidth + "px",
101+
color: "#FE8E14",
102+
});
103+
} else {
104+
Object.assign(textContainer.style, {
105+
position: "absolute",
106+
left: convertedP1.x + "px",
107+
right: document.documentElement.clientWidth - convertedP2.x + "px",
108+
bottom: document.documentElement.clientHeight - convertedP1.y + "px",
109+
color: "#FE8E14",
110+
});
111+
}
123112
textContainer.innerText = result.barcodeText;
124113
divTextContainers.append(textContainer);
114+
115+
/**
116+
* You can also add more information, such as displaying product images.
117+
*/
125118
}
126119
};
127120

0 commit comments

Comments
 (0)