|
1 | 1 | import { useState } from 'react';
|
2 |
| -import { Button, Image, SafeAreaView, Text } from 'react-native'; |
3 |
| -import { |
4 |
| - ColorConversionCodes, |
5 |
| - ContourApproximationModes, |
6 |
| - DataTypes, |
7 |
| - ObjectType, |
8 |
| - OpenCV, |
9 |
| - RetrievalModes, |
10 |
| - type Rect, |
11 |
| -} from 'react-native-fast-opencv'; |
| 2 | +import { Button, Image, SafeAreaView } from 'react-native'; |
| 3 | +import { DataTypes, ObjectType, OpenCV } from 'react-native-fast-opencv'; |
12 | 4 | import { launchImageLibrary, type Asset } from 'react-native-image-picker';
|
13 |
| -import { useRunOnJS, useWorklet, Worklets } from 'react-native-worklets-core'; |
| 5 | +import { useRunOnJS, useWorklet } from 'react-native-worklets-core'; |
| 6 | +import { BorderTypes } from '../../src/constants/Core'; |
14 | 7 |
|
15 | 8 | export function ImageExample() {
|
16 | 9 | const [photo, setPhoto] = useState<Asset | null>(null);
|
17 |
| - const [b64, setB64] = useState<string>(''); |
| 10 | + const [result, setResult] = useState<string>(''); |
18 | 11 |
|
19 |
| - const test = async () => { |
| 12 | + const getImageFromGallery = async () => { |
20 | 13 | const result = await launchImageLibrary({
|
21 | 14 | mediaType: 'photo',
|
22 | 15 | includeBase64: true,
|
23 | 16 | });
|
24 | 17 | setPhoto(result.assets?.at(0) || null);
|
25 | 18 | };
|
26 | 19 |
|
27 |
| - const process = async () => { |
28 |
| - if (photo && photo.base64 && photo.width && photo.height) { |
| 20 | + const setImage = useRunOnJS((data: string) => { |
| 21 | + setResult(data); |
| 22 | + }, []); |
| 23 | + |
| 24 | + const worklet = useWorklet('default', () => { |
| 25 | + 'worklet'; |
| 26 | + if (photo?.base64) { |
29 | 27 | const src = OpenCV.base64ToMat(photo.base64);
|
30 | 28 | const dst = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U);
|
| 29 | + const kernel = OpenCV.createObject(ObjectType.Size, 20, 20); |
| 30 | + const point = OpenCV.createObject(ObjectType.Point, 0, 0); |
31 | 31 |
|
32 |
| - await OpenCV.invokeAsync( |
33 |
| - 'cvtColor', |
| 32 | + OpenCV.invoke( |
| 33 | + 'blur', |
34 | 34 | src,
|
35 | 35 | dst,
|
36 |
| - ColorConversionCodes.COLOR_BGR2HSV |
| 36 | + kernel, |
| 37 | + point, |
| 38 | + BorderTypes.BORDER_DEFAULT |
37 | 39 | );
|
| 40 | + const dstResult = OpenCV.toJSValue(dst); |
38 | 41 |
|
39 |
| - const lowerBound = OpenCV.createObject(ObjectType.Scalar, 40, 40, 40); |
40 |
| - const upperBound = OpenCV.createObject(ObjectType.Scalar, 100, 255, 255); |
41 |
| - |
42 |
| - await OpenCV.invokeAsync('inRange', dst, lowerBound, upperBound, dst); |
43 |
| - |
44 |
| - // const channels = OpenCV.createObject(ObjectType.MatVector); |
45 |
| - // OpenCV.invoke('split', dst, channels); |
46 |
| - // const grayChannel = OpenCV.copyObjectFromVector(channels, 0); |
47 |
| - |
48 |
| - // const contours = OpenCV.createObject(ObjectType.MatVector); |
49 |
| - |
50 |
| - // OpenCV.invoke( |
51 |
| - // 'findContours', |
52 |
| - // grayChannel, |
53 |
| - // contours, |
54 |
| - // RetrievalModes.RETR_TREE, |
55 |
| - // ContourApproximationModes.CHAIN_APPROX_SIMPLE |
56 |
| - // ); |
| 42 | + setImage(dstResult.base64); |
57 | 43 |
|
58 |
| - // const contoursMats = OpenCV.toJSValue(contours); |
59 |
| - |
60 |
| - // const rectangles: Rect[] = []; |
61 |
| - |
62 |
| - // for (let i = 0; i < contoursMats.array.length; i++) { |
63 |
| - // const contour = OpenCV.copyObjectFromVector(contours, i); |
64 |
| - // const { value: area } = OpenCV.invoke('contourArea', contour, false); |
65 |
| - |
66 |
| - // if (area > 3000) { |
67 |
| - // const rect = OpenCV.invoke('boundingRect', contour); |
68 |
| - // rectangles.push(rect); |
69 |
| - // } |
70 |
| - // } |
71 |
| - |
72 |
| - // console.log(rectangles); |
73 |
| - |
74 |
| - // const result = OpenCV.toJSValue(dst); |
75 |
| - // setB64(result.base64); |
76 |
| - // console.log(array.length); |
| 44 | + OpenCV.clearBuffers(); // IMPORTANT |
77 | 45 | }
|
78 |
| - }; |
79 |
| - |
80 |
| - const [base64, setBase64] = useState<string>(''); |
81 |
| - |
82 |
| - const setImage = useRunOnJS((data: string) => { |
83 |
| - setBase64(data); |
84 |
| - }, []); |
85 |
| - |
86 |
| - const worklet = useWorklet( |
87 |
| - 'default', |
88 |
| - () => { |
89 |
| - 'worklet'; |
90 |
| - if (photo && photo.base64 && photo.width && photo.height) { |
91 |
| - const src = OpenCV.base64ToMat(photo.base64); |
92 |
| - const dst = OpenCV.createObject(ObjectType.Mat, 0, 0, DataTypes.CV_8U); |
93 |
| - |
94 |
| - OpenCV.invoke('cvtColor', src, dst, ColorConversionCodes.COLOR_BGR2HSV); |
95 |
| - |
96 |
| - const lowerBound = OpenCV.createObject(ObjectType.Scalar, 40, 40, 40); |
97 |
| - const upperBound = OpenCV.createObject( |
98 |
| - ObjectType.Scalar, |
99 |
| - 100, |
100 |
| - 255, |
101 |
| - 255 |
102 |
| - ); |
103 |
| - |
104 |
| - OpenCV.invoke('inRange', dst, lowerBound, upperBound, dst); |
105 |
| - |
106 |
| - const result = OpenCV.toJSValue(dst); |
107 |
| - |
108 |
| - setImage(result.base64); |
109 |
| - } |
110 |
| - }, |
111 |
| - [] |
112 |
| - ); |
| 46 | + }); |
113 | 47 |
|
114 | 48 | return (
|
115 | 49 | <SafeAreaView style={{ backgroundColor: 'white', flex: 1 }}>
|
116 |
| - <Button title="Select photo" onPress={test} /> |
117 |
| - <Text> |
118 |
| - URI: {photo?.uri} {photo?.height} {photo?.width} |
119 |
| - </Text> |
| 50 | + <Button title="Select photo" onPress={getImageFromGallery} /> |
120 | 51 | <Button title="Process" onPress={() => worklet()} />
|
121 |
| - {photo?.base64 && photo?.width && photo?.height && ( |
122 |
| - <Image |
123 |
| - source={{ uri: 'data:image/jpg;base64,' + photo.base64 }} |
124 |
| - height={300} |
125 |
| - width={300} |
126 |
| - /> |
127 |
| - )} |
128 |
| - {b64 && photo?.width && photo?.height && ( |
| 52 | + |
| 53 | + {result && ( |
129 | 54 | <Image
|
130 |
| - source={{ uri: 'data:image/png;base64,' + b64 }} |
131 |
| - height={300} |
132 |
| - width={300} |
| 55 | + source={{ uri: 'data:image/png;base64,' + result }} |
| 56 | + height={530} |
| 57 | + width={390} |
133 | 58 | />
|
134 | 59 | )}
|
135 | 60 | </SafeAreaView>
|
|
0 commit comments