Skip to content

Commit 114e89c

Browse files
committed
update react sample
1 parent ef8424d commit 114e89c

File tree

12 files changed

+109
-211
lines changed

12 files changed

+109
-211
lines changed

hello-world/react/src/App.css

Lines changed: 40 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,52 @@
1-
.App-logo {
2-
height: 40vmin;
3-
pointer-events: none;
4-
}
51

6-
@media (prefers-reduced-motion: no-preference) {
7-
.App-logo {
8-
animation: App-logo-spin infinite 20s linear;
9-
}
10-
}
112

12-
.App-header {
13-
background-color: #282c34;
14-
min-height: 100vh;
3+
.title {
154
display: flex;
16-
flex-direction: column;
17-
align-items: center;
185
justify-content: center;
19-
font-size: calc(10px + 2vmin);
20-
color: white;
6+
align-items: center;
7+
margin-top: 20px;
218
}
9+
/* .title .title-text {
2210
23-
.App-link {
24-
color: #61dafb;
11+
} */
12+
.title .title-logo {
13+
width: 60px;
14+
height: 60px;
15+
animation: retate 5s infinite linear;
16+
}
17+
.top-btns {
18+
width: 30%;
19+
margin: 20px auto;
20+
}
21+
.top-btns button {
22+
display: inline-block;
23+
border: 1px solid black;
24+
padding: 5px 15px;
25+
background-color: transparent;
26+
cursor: pointer;
27+
}
28+
.top-btns button:first-child {
29+
border-top-left-radius: 10px;
30+
border-bottom-left-radius: 10px;
31+
border-right: transparent;
32+
}
33+
.top-btns button:nth-child(2) {
34+
border-top-right-radius: 10px;
35+
border-bottom-right-radius: 10px;
36+
border-left: transparent;
37+
}
38+
39+
@media screen and (max-width: 500px) {
40+
.top-btns {
41+
width: 70%;
42+
}
2543
}
2644

27-
@keyframes App-logo-spin {
45+
@keyframes retate {
2846
from {
29-
transform: rotate(0deg);
47+
transform: rotate(0deg);
3048
}
3149
to {
32-
transform: rotate(360deg);
50+
transform: rotate(360deg);
3351
}
34-
}
52+
}

hello-world/react/src/App.test.tsx

Lines changed: 0 additions & 9 deletions
This file was deleted.

hello-world/react/src/App.tsx

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,31 @@
1-
import HelloWorld from './components/HelloWorld/HelloWorld';
1+
import React from 'react';
2+
import reactLogo from './assets/logo.svg';
3+
import VideoCapture from './components/VideoCapture/VideoCapture';
4+
import ImageCapture from './components/ImageCapture/ImageCapture';
25
import './App.css';
36

4-
function App() {
5-
return (
6-
<div className="App">
7-
<HelloWorld></HelloWorld>
8-
</div>
9-
);
7+
class App extends React.Component {
8+
state = {
9+
mode: "video"
10+
};
11+
12+
render() {
13+
return (
14+
<div className="App">
15+
<div className='App'>
16+
<div className='title'>
17+
<h2 className='title-text'>Hello World for React</h2>
18+
<img className='title-logo' src={reactLogo} alt="logo"></img>
19+
</div>
20+
<div className='top-btns'>
21+
<button onClick={() => { this.setState({ mode: "video" }) }} style={{ backgroundColor: this.state.mode === "video" ? "rgb(255, 174, 55)" : "#fff" }}>Video Capture</button>
22+
<button onClick={() => { this.setState({ mode: "image" }) }} style={{ backgroundColor: this.state.mode === "image" ? "rgb(255, 174, 55)" : "#fff" }}>Image Capture</button>
23+
</div>
24+
{this.state.mode === "video" ? <VideoCapture /> : <ImageCapture />}
25+
</div>
26+
</div>
27+
);
28+
}
1029
}
1130

1231
export default App;

hello-world/react/src/assets/logo.svg

Lines changed: 1 addition & 0 deletions
Loading

hello-world/react/src/components/HelloWorld/HelloWorld.css

Lines changed: 0 additions & 27 deletions
This file was deleted.

hello-world/react/src/components/HelloWorld/HelloWorld.tsx

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,18 @@
1-
.div-image-capture {
1+
.capture-img {
2+
width: 100%;
3+
height: 100%;
4+
font-family:Consolas,Monaco,Lucida Console,Liberation Mono,DejaVu Sans Mono,Bitstream Vera Sans Mono,Courier New, monospace;
5+
}
6+
7+
.capture-img .img-ipt {
8+
width: 80%;
9+
height: 100%;
210
display: flex;
311
justify-content: center;
4-
align-items: center;
5-
width: 100%;
612
border: 1px solid black;
13+
margin: 0 auto;
14+
}
15+
16+
.capture-img .result-area {
17+
margin-top: 20px;
718
}

hello-world/react/src/components/ImageCapture/ImageCapture.tsx

Lines changed: 18 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -5,67 +5,47 @@ import "../../cvr"; // import side effects. The license, engineResourcePath, so
55
import "./ImageCapture.css";
66

77
class ImageCapture extends React.Component {
8-
pInit: Promise<CaptureVisionRouter> | null = null;
9-
pDestroy: Promise<void> | null = null;
8+
pRouter: Promise<CaptureVisionRouter> | null = null;
9+
resRef: React.RefObject<HTMLDivElement> = React.createRef();
10+
iptRef: React.RefObject<HTMLInputElement> = React.createRef();
1011

1112
async init(): Promise<CaptureVisionRouter> {
1213
const router = await CaptureVisionRouter.createInstance();
1314
return router;
1415
}
1516

16-
async destroy(): Promise<void> {
17-
if (this.pInit) {
18-
const router = await this.pInit;
19-
router.dispose();
20-
}
21-
}
22-
23-
decodeImg = async (e: React.ChangeEvent<HTMLInputElement>) => {
17+
captureImage = async (e: any) => {
2418
try {
25-
const router = await this.pInit;
26-
// Decode selected image with 'ReadBarcodes_SpeedFirst' template.
27-
const result = await router!.capture(
28-
e.target.files![0],
29-
"ReadBarcodes_SpeedFirst"
30-
);
31-
let texts = "";
19+
this.resRef.current!.innerText = "";
20+
const router = await this.pRouter;
21+
const result = await router!.capture(e.target.files[0]);
3222
for (let item of result.items) {
33-
console.log((item as BarcodeResultItem).text);
34-
texts += (item as BarcodeResultItem).text + "\n";
23+
let _item = item as BarcodeResultItem;
24+
console.log(_item.text);
25+
this.resRef.current!.innerText += `${_item.formatString} : ${_item.text}\n`
3526
}
36-
if (texts !== "") alert(texts);
37-
if (!result.items.length) alert("No barcode found");
27+
this.iptRef.current!.value = '';
3828
} catch (ex: any) {
3929
let errMsg = ex.message || ex;
4030
console.error(errMsg);
4131
alert(errMsg);
4232
}
43-
e.target.value = "";
44-
};
33+
}
4534

4635
async componentDidMount() {
47-
// In 'development', React runs setup and cleanup one extra time before the actual setup in Strict Mode.
48-
if (this.pDestroy) {
49-
await this.pDestroy;
50-
this.pInit = this.init();
51-
} else {
52-
this.pInit = this.init();
53-
}
36+
this.pRouter = CaptureVisionRouter.createInstance();
5437
}
5538

5639
async componentWillUnmount() {
57-
await (this.pDestroy = this.destroy());
58-
console.log("ImageCapture Component Unmount");
40+
(await this.pRouter)!.dispose();
41+
console.log('ImageCapture Component Unmount');
5942
}
6043

6144
render() {
6245
return (
63-
<div className="div-image-capture">
64-
<input
65-
type="file"
66-
accept=".jpg,.jpeg,.icon,.gif,.svg,.webp,.png,.bmp"
67-
onChange={this.decodeImg}
68-
/>
46+
<div className="capture-img">
47+
<div className="img-ipt"><input type="file" ref={this.iptRef} onChange={this.captureImage} /></div>
48+
<div className="result-area" ref={this.resRef}></div>
6949
</div>
7050
);
7151
}

hello-world/react/src/index.css

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,10 @@
1-
body {
1+
* {
22
margin: 0;
3-
font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', 'Roboto', 'Oxygen',
4-
'Ubuntu', 'Cantarell', 'Fira Sans', 'Droid Sans', 'Helvetica Neue',
5-
sans-serif;
6-
-webkit-font-smoothing: antialiased;
7-
-moz-osx-font-smoothing: grayscale;
3+
padding: 0;
4+
box-sizing: border-box;
85
}
96

10-
code {
11-
font-family: source-code-pro, Menlo, Monaco, Consolas, 'Courier New',
12-
monospace;
13-
}
7+
html, body {
8+
text-align: center;
9+
min-width: 350px;
10+
}

hello-world/react/src/index.tsx

Lines changed: 3 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,10 @@
11
import React from 'react';
22
import ReactDOM from 'react-dom/client';
3-
import './index.css';
43
import App from './App';
5-
import reportWebVitals from './reportWebVitals';
4+
import './index.css';
5+
import "./cvr";
66

77
const root = ReactDOM.createRoot(
88
document.getElementById('root') as HTMLElement
99
);
10-
root.render(
11-
<React.StrictMode>
12-
<App />
13-
</React.StrictMode>
14-
);
15-
16-
// If you want to start measuring performance in your app, pass a function
17-
// to log results (for example: reportWebVitals(console.log))
18-
// or send to an analytics endpoint. Learn more: https://bit.ly/CRA-vitals
19-
reportWebVitals();
10+
root.render(<App />);

hello-world/react/src/reportWebVitals.ts

Lines changed: 0 additions & 15 deletions
This file was deleted.

hello-world/react/src/setupTests.ts

Lines changed: 0 additions & 5 deletions
This file was deleted.

0 commit comments

Comments
 (0)