Skip to content

Commit 49cd730

Browse files
committed
react: improve image loading logic; add DQA/OCR examples into stored data page
1 parent c19b147 commit 49cd730

File tree

3 files changed

+59
-6
lines changed

3 files changed

+59
-6
lines changed

react-js/src/pages/StoredDataDetailsPage.tsx

Lines changed: 50 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import { useEffect, useState } from "react";
2-
import { Box } from "@mui/material";
2+
import { Box, Button, CircularProgress } from "@mui/material";
33
import { SBStoreCroppedDetectionResult } from "scanbot-web-sdk/@types";
44

55
import SBSDKService from "../service/SBSDKService";
@@ -11,6 +11,7 @@ export default function StorageDetailsPage() {
1111

1212
const [item, setItem] = useState<SBStoreCroppedDetectionResult | null>(null);
1313
const [base64Image, setBase64Image] = useState<string | undefined>(undefined);
14+
const [isLoading, setIsLoading] = useState(false);
1415

1516
useEffect(() => {
1617

@@ -26,15 +27,43 @@ export default function StorageDetailsPage() {
2627
}
2728

2829
async function loadItem() {
29-
const result = await SBSDKService.SDK.storage.getCroppedDetectionResult(itemId);
30+
setIsLoading(true);
31+
const sdk = await SBSDKService.awaitSDK();
32+
const result = await sdk.storage.getCroppedDetectionResult(itemId);
3033
setItem(result);
3134
const base64 = await ImageUtils.createBase64Image(result);
3235
setBase64Image(base64);
36+
setIsLoading(false);
3337
}
3438

3539
loadItem();
3640
}, []);
3741

42+
async function runDQA() {
43+
if (!item) {
44+
return;
45+
}
46+
setIsLoading(true);
47+
const image = item.croppedImage ?? item.originalImage;
48+
const analyzer = await SBSDKService.SDK.createDocumentQualityAnalyzer({})
49+
const result = await analyzer.analyze(image);
50+
setIsLoading(false);
51+
console.log("Document quality analysis: ", result);
52+
}
53+
54+
async function runOCR() {
55+
if (!item) {
56+
return;
57+
}
58+
59+
setIsLoading(true);
60+
const image = item.croppedImage ?? item.originalImage;
61+
const engine = await SBSDKService.SDK.createOcrEngine()
62+
const result = await engine.performOcr(image);
63+
setIsLoading(false);
64+
console.log("OCR result: ", result);
65+
}
66+
3867
return (
3968
<Box style={{
4069
display: "flex",
@@ -47,9 +76,25 @@ export default function StorageDetailsPage() {
4776
<TopBar title={"Document Details"} isBackNavigationEnabled={true} />
4877
<h2 style={{ color: TextColor }}>Storage Details</h2>
4978
<h3 style={{ color: TextColor }}>Item ID: {item?.id}</h3>
50-
<img src={base64Image}
51-
alt={`Storage image ${item?.id}`}
52-
style={{ width: "90%", maxHeight: 500, objectFit: "contain" }}
79+
{base64Image && <Box style={{
80+
display: "flex",
81+
flexDirection: "column",
82+
alignItems: "center",
83+
width: "100%",
84+
height: "100%",
85+
}}>
86+
<img src={base64Image}
87+
alt={`Storage image ${item?.id}`}
88+
style={{ width: "90%", maxHeight: 500, objectFit: "contain" }}
89+
/>
90+
<Box style={{ paddingLeft: "5%", paddingTop: 10 }}>
91+
<Button onClick={runDQA}>Run Document Quality Analysis</Button>
92+
<Button onClick={runOCR}>Run Optical Character Recognition</Button>
93+
</Box>
94+
</Box>}
95+
<CircularProgress
96+
style={{ position: "absolute", top: "40%", visibility: isLoading ? "visible" : "hidden" }}
97+
color="primary"
5398
/>
5499
</Box>
55100
)

react-js/src/pages/StoredDataPage.tsx

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ export default function StoredDataPage() {
1313
useEffect(() => {
1414

1515
async function loadStorageData() {
16-
const result = await SBSDKService.SDK.storage.getCroppedDetectionResults(false);
16+
const sdk = await SBSDKService.awaitSDK();
17+
const result = await sdk.storage.getCroppedDetectionResults(false);
1718
setDetectionResults(result);
1819
}
1920

react-js/src/service/SBSDKService.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,13 @@ export default class SBSDKService {
4949
return this.sdk;
5050
}
5151

52+
public static async awaitSDK() {
53+
if (!this.sdk) {
54+
await this.initialize();
55+
}
56+
return this.sdk;
57+
}
58+
5259
private static sdk: ScanbotSDK;
5360

5461
}

0 commit comments

Comments
 (0)