|
| 1 | +import type { SkPoint } from '@shopify/react-native-skia'; |
| 2 | +import { PaintStyle, PointMode, Skia, vec } from '@shopify/react-native-skia'; |
| 3 | +import { useEffect } from 'react'; |
| 4 | +import { StyleSheet, Text } from 'react-native'; |
| 5 | +import type { PointVector } from 'react-native-fast-opencv'; |
| 6 | +import { |
| 7 | + ColorConversionCodes, |
| 8 | + ContourApproximationModes, |
| 9 | + MorphShapes, |
| 10 | + MorphTypes, |
| 11 | + ObjectType, |
| 12 | + OpenCV, |
| 13 | + RetrievalModes, |
| 14 | +} from 'react-native-fast-opencv'; |
| 15 | +import { |
| 16 | + Camera, |
| 17 | + useCameraDevice, |
| 18 | + useCameraPermission, |
| 19 | + useSkiaFrameProcessor, |
| 20 | +} from 'react-native-vision-camera'; |
| 21 | +import { useResizePlugin } from 'vision-camera-resize-plugin'; |
| 22 | + |
| 23 | +const paint = Skia.Paint(); |
| 24 | +const border = Skia.Paint(); |
| 25 | + |
| 26 | +paint.setStyle(PaintStyle.Fill); |
| 27 | +paint.setColor(Skia.Color(0x66_e7_a6_49)); |
| 28 | +border.setStyle(PaintStyle.Fill); |
| 29 | +border.setColor(Skia.Color(0xff_e7_a6_49)); |
| 30 | +border.setStrokeWidth(4); |
| 31 | + |
| 32 | +type Point = { x: number; y: number }; |
| 33 | + |
| 34 | +export function DocumentDetection() { |
| 35 | + const device = useCameraDevice('back'); |
| 36 | + const { hasPermission, requestPermission } = useCameraPermission(); |
| 37 | + |
| 38 | + const { resize } = useResizePlugin(); |
| 39 | + |
| 40 | + useEffect(() => { |
| 41 | + requestPermission(); |
| 42 | + }, [requestPermission]); |
| 43 | + |
| 44 | + const frameProcessor = useSkiaFrameProcessor((frame) => { |
| 45 | + 'worklet'; |
| 46 | + |
| 47 | + const ratio = 500 / frame.width; |
| 48 | + const height = frame.height * ratio; |
| 49 | + const width = frame.width * ratio; |
| 50 | + |
| 51 | + const resized = resize(frame, { |
| 52 | + dataType: 'uint8', |
| 53 | + pixelFormat: 'bgr', |
| 54 | + scale: { |
| 55 | + height: height, |
| 56 | + width: width, |
| 57 | + }, |
| 58 | + }); |
| 59 | + |
| 60 | + const source = OpenCV.frameBufferToMat(height, width, 3, resized); |
| 61 | + |
| 62 | + OpenCV.invoke( |
| 63 | + 'cvtColor', |
| 64 | + source, |
| 65 | + source, |
| 66 | + ColorConversionCodes.COLOR_BGR2GRAY |
| 67 | + ); |
| 68 | + |
| 69 | + const kernel = OpenCV.createObject(ObjectType.Size, 4, 4); |
| 70 | + const blurKernel = OpenCV.createObject(ObjectType.Size, 7, 7); |
| 71 | + const structuringElement = OpenCV.invoke( |
| 72 | + 'getStructuringElement', |
| 73 | + MorphShapes.MORPH_ELLIPSE, |
| 74 | + kernel |
| 75 | + ); |
| 76 | + |
| 77 | + OpenCV.invoke( |
| 78 | + 'morphologyEx', |
| 79 | + source, |
| 80 | + source, |
| 81 | + MorphTypes.MORPH_OPEN, |
| 82 | + structuringElement |
| 83 | + ); |
| 84 | + OpenCV.invoke( |
| 85 | + 'morphologyEx', |
| 86 | + source, |
| 87 | + source, |
| 88 | + MorphTypes.MORPH_CLOSE, |
| 89 | + structuringElement |
| 90 | + ); |
| 91 | + OpenCV.invoke('GaussianBlur', source, source, blurKernel, 0); |
| 92 | + OpenCV.invoke('Canny', source, source, 75, 100); |
| 93 | + |
| 94 | + const contours = OpenCV.createObject(ObjectType.PointVectorOfVectors); |
| 95 | + |
| 96 | + OpenCV.invoke( |
| 97 | + 'findContours', |
| 98 | + source, |
| 99 | + contours, |
| 100 | + RetrievalModes.RETR_LIST, |
| 101 | + ContourApproximationModes.CHAIN_APPROX_SIMPLE |
| 102 | + ); |
| 103 | + |
| 104 | + const contoursMats = OpenCV.toJSValue(contours); |
| 105 | + |
| 106 | + let greatestPolygon: PointVector | undefined; |
| 107 | + let greatestArea = 0; |
| 108 | + |
| 109 | + for (let index = 0; index < contoursMats.array.length; index++) { |
| 110 | + const contour = OpenCV.copyObjectFromVector(contours, index); |
| 111 | + const { value: area } = OpenCV.invoke('contourArea', contour, false); |
| 112 | + |
| 113 | + if (area > 2000 && area > greatestArea) { |
| 114 | + const peri = OpenCV.invoke('arcLength', contour, true); |
| 115 | + const approx = OpenCV.createObject(ObjectType.PointVector); |
| 116 | + |
| 117 | + OpenCV.invoke('approxPolyDP', contour, approx, 0.1 * peri.value, true); |
| 118 | + |
| 119 | + greatestPolygon = approx; |
| 120 | + greatestArea = area; |
| 121 | + } |
| 122 | + } |
| 123 | + |
| 124 | + frame.render(); |
| 125 | + |
| 126 | + if (greatestPolygon) { |
| 127 | + const points: Point[] = OpenCV.toJSValue(greatestPolygon).array; |
| 128 | + |
| 129 | + if (points.length === 4) { |
| 130 | + const path = Skia.Path.Make(); |
| 131 | + const pointsToShow: SkPoint[] = []; |
| 132 | + |
| 133 | + const lastPointX = (points[3]?.x || 0) / ratio; |
| 134 | + const lastPointY = (points[3]?.y || 0) / ratio; |
| 135 | + |
| 136 | + path.moveTo(lastPointX, lastPointY); |
| 137 | + pointsToShow.push(vec(lastPointX, lastPointY)); |
| 138 | + |
| 139 | + for (let index = 0; index < 4; index++) { |
| 140 | + const pointX = (points[index]?.x || 0) / ratio; |
| 141 | + const pointY = (points[index]?.y || 0) / ratio; |
| 142 | + |
| 143 | + path.lineTo(pointX, pointY); |
| 144 | + pointsToShow.push(vec(pointX, pointY)); |
| 145 | + } |
| 146 | + |
| 147 | + path.close(); |
| 148 | + |
| 149 | + frame.drawPath(path, paint); |
| 150 | + frame.drawPoints(PointMode.Polygon, pointsToShow, border); |
| 151 | + } |
| 152 | + } |
| 153 | + |
| 154 | + OpenCV.clearBuffers(); |
| 155 | + }, []); |
| 156 | + |
| 157 | + if (!hasPermission) { |
| 158 | + return <Text>No permission</Text>; |
| 159 | + } |
| 160 | + |
| 161 | + if (device == null) { |
| 162 | + return <Text>No device</Text>; |
| 163 | + } |
| 164 | + |
| 165 | + return ( |
| 166 | + <Camera |
| 167 | + style={StyleSheet.absoluteFill} |
| 168 | + device={device} |
| 169 | + isActive={true} |
| 170 | + frameProcessor={frameProcessor} |
| 171 | + /> |
| 172 | + ); |
| 173 | +} |
0 commit comments