|
1 | | -import React, {useEffect} from 'react'; |
2 | | -import {Button, Image, Platform, StyleSheet, View} from 'react-native'; |
3 | | -import {ImageManager, imageDataToBase64} from 'dynamsoft-capture-vision-react-native'; |
4 | | -import {ExternalCachesDirectoryPath, TemporaryDirectoryPath} from 'react-native-fs'; |
| 1 | +import React, {useCallback, useEffect, useState} from 'react'; |
| 2 | +import { |
| 3 | + Alert, |
| 4 | + Image, |
| 5 | + Platform, |
| 6 | + StyleSheet, |
| 7 | + Text, |
| 8 | + TouchableOpacity, |
| 9 | + View, |
| 10 | +} from 'react-native'; |
| 11 | +import { |
| 12 | + ImageIO, |
| 13 | + ImageProcessor, |
| 14 | + imageDataToBase64, |
| 15 | +} from 'dynamsoft-capture-vision-react-native'; |
| 16 | +import { |
| 17 | + ExternalCachesDirectoryPath, |
| 18 | + TemporaryDirectoryPath, |
| 19 | +} from 'react-native-fs'; |
| 20 | +import {StackNavigation} from './App.tsx'; |
| 21 | +import {useFocusEffect} from '@react-navigation/native'; |
5 | 22 |
|
6 | | -export const NormalizedImage = () => { |
7 | | - const base64Str = imageDataToBase64(global.normalizedImage); |
| 23 | +const ColourMode = { |
| 24 | + colour: 'colour', |
| 25 | + grayscale: 'grayscale', |
| 26 | + binary: 'binary', |
| 27 | +}; |
| 28 | + |
| 29 | +export const NormalizedImage = ({navigation}: StackNavigation) => { |
| 30 | + const [base64, setBase64] = useState(''); |
| 31 | + |
| 32 | + useFocusEffect( |
| 33 | + useCallback(() => { |
| 34 | + global.showingImage = global.deskewedImage; |
| 35 | + setBase64(imageDataToBase64(global.showingImage)); |
| 36 | + }, []), |
| 37 | + ); |
8 | 38 |
|
9 | 39 | useEffect(() => { |
10 | 40 | return () => { |
11 | | - if (global.normalizedImage && typeof global.normalizedImage.release === 'function') { |
12 | | - global.normalizedImage.release(); |
13 | | - } |
| 41 | + global.originalImage?.release(); |
| 42 | + global.deskewedImage?.release(); |
| 43 | + global.showingImage?.release(); |
14 | 44 | }; |
15 | 45 | }, []); |
16 | | - return <View style={{flex: 1}}> |
17 | | - <Image source={{uri: 'data:image/png;base64,' + base64Str}} style={styles.fullScreen} resizeMode="contain"/> |
18 | | - <Button title={'Save to file'} onPress={() => { |
19 | | - const imageManager = new ImageManager(); |
20 | | - let savedPath = (Platform.OS === 'ios' ? TemporaryDirectoryPath : ExternalCachesDirectoryPath) + '/normalize.png'; |
21 | | - imageManager.saveToFile(global.normalizedImage, savedPath, true); |
22 | | - // @ts-ignore |
23 | | - window.alert('Has been saved to ' + savedPath); |
24 | | - }}/> |
25 | | - </View>; |
| 46 | + |
| 47 | + const changeColourMode = (mode: string) => { |
| 48 | + if (global.showingImage && global.showingImage !== global.deskewedImage) { |
| 49 | + global.showingImage.release(); |
| 50 | + } |
| 51 | + switch (mode) { |
| 52 | + case ColourMode.colour: |
| 53 | + global.showingImage = global.deskewedImage; |
| 54 | + break; |
| 55 | + case ColourMode.grayscale: |
| 56 | + global.showingImage = new ImageProcessor().convertToGray( |
| 57 | + global.deskewedImage, |
| 58 | + )!!; |
| 59 | + break; |
| 60 | + case ColourMode.binary: |
| 61 | + global.showingImage = new ImageProcessor().convertToBinaryLocal( |
| 62 | + global.deskewedImage, |
| 63 | + /*blockSize = */ 0, |
| 64 | + /*compensation = */ 10, |
| 65 | + /*invert = */ false, |
| 66 | + )!!; |
| 67 | + break; |
| 68 | + } |
| 69 | + setBase64(imageDataToBase64(global.showingImage)); |
| 70 | + }; |
| 71 | + |
| 72 | + const onTabPress = (index: number) => { |
| 73 | + switch (index) { |
| 74 | + case 0: //'Back to Edit' |
| 75 | + navigation.navigate('Editor'); |
| 76 | + break; |
| 77 | + case 1: //'Switch Colour' |
| 78 | + Alert.alert( |
| 79 | + '', |
| 80 | + 'Select Colour Mode', |
| 81 | + [ColourMode.colour, ColourMode.grayscale, ColourMode.binary].map( |
| 82 | + mode => ({ |
| 83 | + text: mode, |
| 84 | + onPress: () => changeColourMode(mode), |
| 85 | + }), |
| 86 | + ), |
| 87 | + ); |
| 88 | + break; |
| 89 | + case 2: //'Export' |
| 90 | + const imageIO = new ImageIO(); |
| 91 | + let savedPath = |
| 92 | + (Platform.OS === 'ios' |
| 93 | + ? TemporaryDirectoryPath |
| 94 | + : ExternalCachesDirectoryPath) + `/normalize_${Date.now()}.png`; |
| 95 | + imageIO.saveToFile(global.showingImage, savedPath, true); |
| 96 | + Alert.alert('Successfully saved', 'Has been saved to ' + savedPath); |
| 97 | + break; |
| 98 | + } |
| 99 | + }; |
| 100 | + |
| 101 | + return ( |
| 102 | + <View style={styles.fullScreen}> |
| 103 | + <Image |
| 104 | + source={{uri: 'data:image/png;base64,' + base64}} |
| 105 | + style={styles.fullScreen} |
| 106 | + resizeMode="contain" |
| 107 | + /> |
| 108 | + <View style={styles.bottomBar}> |
| 109 | + {[ |
| 110 | + {emoji: '✏️', label: 'Back to Edit'}, |
| 111 | + {emoji: '🎨', label: 'Switch Colour'}, |
| 112 | + {emoji: '💾', label: 'Export'}, |
| 113 | + ].map((item, index) => ( |
| 114 | + <TouchableOpacity |
| 115 | + key={index} |
| 116 | + style={styles.tab} |
| 117 | + onPress={() => onTabPress(index)}> |
| 118 | + <Text style={styles.emoji}>{item.emoji}</Text> |
| 119 | + <Text style={styles.label}>{item.label}</Text> |
| 120 | + </TouchableOpacity> |
| 121 | + ))} |
| 122 | + </View> |
| 123 | + </View> |
| 124 | + ); |
26 | 125 | }; |
27 | 126 |
|
28 | 127 | const styles = StyleSheet.create({ |
29 | | - fullScreen: {flex: 0.9}, |
| 128 | + fullScreen: {flex: 1}, |
| 129 | + bottomBar: { |
| 130 | + flexDirection: 'row', |
| 131 | + justifyContent: 'space-around', |
| 132 | + alignItems: 'center', |
| 133 | + borderWidth: 1, |
| 134 | + paddingVertical: 10, |
| 135 | + }, |
| 136 | + tab: { |
| 137 | + alignItems: 'center', |
| 138 | + flex: 1, |
| 139 | + }, |
| 140 | + emoji: { |
| 141 | + fontSize: 24, |
| 142 | + }, |
| 143 | + label: { |
| 144 | + fontSize: 12, |
| 145 | + marginTop: 4, |
| 146 | + color: '#333', |
| 147 | + }, |
30 | 148 | }); |
0 commit comments