Skip to content

Commit

Permalink
fix the return type of detect
Browse files Browse the repository at this point in the history
  • Loading branch information
xulihang committed Feb 19, 2024
1 parent a059bf2 commit ee60689
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 11 deletions.
32 changes: 22 additions & 10 deletions example/src/components/Scanner.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,16 @@ export default function Scanner(props:ScannerProps) {
const camera = useRef<Camera|null>(null)
const [isActive,setIsActive] = useState(true);
const [hasPermission, setHasPermission] = useState(false);
const detectionResults = useSharedValue([] as DetectedQuadResult[]);
const [detectionResults,setDetectionResults] = useState([] as DetectedQuadResult[]);
const convertAndSetResults = (records:Record<string,DetectedQuadResult>) => {
let results:DetectedQuadResult[] = [];
for (let index = 0; index < Object.keys(records).length; index++) {
const result = records[Object.keys(records)[index]];
results.push(result);
}
setDetectionResults(results);
}
const convertAndSetResultsJS = Worklets.createRunInJsFn(convertAndSetResults);
const frameWidth = useSharedValue(1920);
const frameHeight = useSharedValue(1080);
const [viewBox,setViewBox] = useState("0 0 1080 1920");
Expand Down Expand Up @@ -63,8 +72,8 @@ export default function Scanner(props:ScannerProps) {

const updateViewBoxJS = Worklets.createRunInJsFn(updateViewBox);
const updatePointsData = () => {
if (detectionResults.value.length>0) {
let result = detectionResults.value[0];
if (detectionResults.length>0) {
let result = detectionResults[0];
if (result) {
let location = result.location;
let pointsData = location.points[0].x + "," + location.points[0].y + " ";
Expand Down Expand Up @@ -92,7 +101,7 @@ export default function Scanner(props:ScannerProps) {
console.log("using camera");
setTaken(true);
takenShared.value = true;
await sleep(1000);
await sleep(500);
photo.current = await camera.current.takePhoto();
if (photo.current) {
console.log(photo.current);
Expand All @@ -116,10 +125,10 @@ export default function Scanner(props:ScannerProps) {
}

const checkIfSteady = async () => {
if (detectionResults.value.length == 0) {
if (detectionResults.length == 0) {
return;
}
let result = detectionResults.value[0];
let result = detectionResults[0];
console.log("previousResults");
console.log(previousResults);
if (result) {
Expand Down Expand Up @@ -156,6 +165,11 @@ export default function Scanner(props:ScannerProps) {
return false;
}

useEffect(() => {
updateViewBox();
updatePointsData();
}, [detectionResults]);

const frameProcessor = useFrameProcessor((frame) => {
'worklet'
console.log("detect frame");
Expand All @@ -166,12 +180,10 @@ export default function Scanner(props:ScannerProps) {
try {
const results = DDN.detect(frame);
console.log(results);
if (results.length>0) {
if (Object.keys(results).length>0) {
frameWidth.value = frame.width;
frameHeight.value = frame.height;
detectionResults.value = results;
updateViewBoxJS();
updatePointsDataJS();
convertAndSetResultsJS(results);
}
} catch (error) {
console.log(error);
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ const plugin = VisionCameraProxy.initFrameProcessorPlugin('detect')
/**
* Detect documents from the camera preview
*/
export function detect(frame: Frame,template?: string): DetectedQuadResult[] {
export function detect(frame: Frame,template?: string): Record<string,DetectedQuadResult> {
'worklet'
if (plugin == null) throw new Error('Failed to load Frame Processor Plugin "detect"!')
if (template) {
Expand Down

0 comments on commit ee60689

Please sign in to comment.