From fbf40108af2c179af8ca0e4afe44d1679513f96d Mon Sep 17 00:00:00 2001 From: Fs-Gen <49970510+Fs-gen@users.noreply.github.com> Date: Fri, 10 Jan 2025 16:50:50 +0700 Subject: [PATCH] Update PhotoRecognizer.ts fixed bug if no text recognized --- src/PhotoRecognizer.ts | 32 ++++++++++++++++++++++++-------- 1 file changed, 24 insertions(+), 8 deletions(-) diff --git a/src/PhotoRecognizer.ts b/src/PhotoRecognizer.ts index fd5c99c..854a1c0 100644 --- a/src/PhotoRecognizer.ts +++ b/src/PhotoRecognizer.ts @@ -1,18 +1,34 @@ -import { NativeModules, Platform } from 'react-native'; +import { NativeModules, Platform, ToastAndroid } from 'react-native'; import type { PhotoOptions, Text } from './types'; export async function PhotoRecognizer(options: PhotoOptions): Promise { const { PhotoRecognizerModule } = NativeModules; const { uri, orientation } = options; + if (!uri) { throw Error("Can't resolve img uri"); } - if (Platform.OS === 'ios') { - return await PhotoRecognizerModule.process( - uri.replace('file://', ''), - orientation || 'portrait' - ); - } else { - return await PhotoRecognizerModule.process(uri); + + try { + let result; + if (Platform.OS === 'ios') { + result = await PhotoRecognizerModule.process( + uri.replace('file://', ''), + orientation || 'portrait' + ); + } else { + result = await PhotoRecognizerModule.process(uri); + } + + + if (Object.keys(result).length === 0) { + ToastAndroid.show("No text recognized", ToastAndroid.SHORT); + console.log('No text detected'); + } else { + return result; + } + } + catch (error) { + console.error('Error during photo recognition: ', error); } }