diff --git a/android/src/main/java/io/twentysixty/rn/eidreader/EIdReaderModule.kt b/android/src/main/java/io/twentysixty/rn/eidreader/EIdReaderModule.kt index 6795fdc..183a261 100644 --- a/android/src/main/java/io/twentysixty/rn/eidreader/EIdReaderModule.kt +++ b/android/src/main/java/io/twentysixty/rn/eidreader/EIdReaderModule.kt @@ -285,22 +285,19 @@ class EIdReaderModule(reactContext: ReactApplicationContext) : } } - @ReactMethod - fun imageDataUrlToJpegDataUrl(dataUrl:String, promise: Promise){ + @ReactMethod(isBlockingSynchronousMethod = true) + fun imageDataUrlToJpegDataUrl(dataUrl:String): String { try { val dataSplit = dataUrl.split(";base64,") if(dataSplit.size != 2){ - promise.reject("Cannot imageDataUrlToJpegDataUrl image because is not a valid dataurl") - return + throw Error("Cannot imageDataUrlToJpegDataUrl image because is not a valid dataurl") } val mimeType = dataSplit[0].split(":")[1] if(!mimeType.startsWith("image/")){ - promise.reject("Couldn't convert $mimeType to JPEG") - return + throw Error("Couldn't convert $mimeType to JPEG") } if(mimeType == "image/jpeg"){ - promise.resolve(dataUrl) - return + return dataUrl } val dataContent = dataSplit[1] val bitmapUtil = BitmapUtil(reactApplicationContext) @@ -310,14 +307,12 @@ class EIdReaderModule(reactContext: ReactApplicationContext) : val byteArrayOutputStream = ByteArrayOutputStream() nfcImage.bitmap!!.compress(Bitmap.CompressFormat.JPEG, 70, byteArrayOutputStream) val bytes = byteArrayOutputStream.toByteArray() - promise.resolve("data:image/jpeg;base64,"+ Base64.encodeToString(bytes, Base64.CRLF)) - return + return "data:image/jpeg;base64,"+ Base64.encodeToString(bytes, Base64.CRLF) } - else promise.reject("Cannot imageDataUrlToJpegDataUrl image") + else throw Error("Cannot imageDataUrlToJpegDataUrl image") } catch (e: IOException) { - promise.reject("Cannot imageDataUrlToJpegDataUrl image") - return + throw Error("Cannot imageDataUrlToJpegDataUrl image") } } diff --git a/example/src/App.tsx b/example/src/App.tsx index 65fa98e..30fa39b 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -15,9 +15,13 @@ import { lena } from './data'; export default function App() { const [result, setResult] = React.useState(); - const [convertedImage, setConvertedImage] = React.useState< - string | undefined - >(undefined); + + let convertedImage; + try { + convertedImage = EIdReader.imageDataUrlToJpegDataUrl(lena); + } catch (error) { + console.error(error); + } React.useEffect(() => { EIdReader.addOnTagDiscoveredListener(() => { @@ -54,16 +58,6 @@ export default function App() { }); }; - const imageDataUrlToJpegDataUrl = () => { - EIdReader.imageDataUrlToJpegDataUrl(lena) - .then((data) => { - setConvertedImage(`${data}`); - }) - .catch((error) => { - console.error('error', error); - }); - }; - const stopReading = () => { EIdReader.stopReading(); }; @@ -100,12 +94,6 @@ export default function App() { - - Convert - Start Reading diff --git a/ios/EidReader.mm b/ios/EidReader.mm index e6e8a87..688cf09 100644 --- a/ios/EidReader.mm +++ b/ios/EidReader.mm @@ -20,9 +20,7 @@ @interface RCT_EXTERN_MODULE(EIdReader, RCTEventEmitter) RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(stopReading); -RCT_EXTERN_METHOD(imageDataUrlToJpegDataUrl:(NSString)dataUrl - withResolver:(RCTPromiseResolveBlock)resolve - withRejecter:(RCTPromiseRejectBlock)reject); +RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(imageDataUrlToJpegDataUrl:(NSString)dataUrl); + (BOOL)requiresMainQueueSetup { diff --git a/ios/EidReader.swift b/ios/EidReader.swift index 8efe074..842bf7a 100644 --- a/ios/EidReader.swift +++ b/ios/EidReader.swift @@ -92,36 +92,34 @@ class EIdReader: RCTEventEmitter { } } + + enum EidReaderError: Error { + case invalidInput(String) + case processingError(String) + } - @objc(imageDataUrlToJpegDataUrl:withResolver:withRejecter:) - func imageDataUrlToJpegDataUrl( - dataUrl: NSString, - resolve: @escaping RCTPromiseResolveBlock, - reject: @escaping RCTPromiseRejectBlock - ) { + @objc(imageDataUrlToJpegDataUrl:withError:) + func imageDataUrlToJpegDataUrl(dataUrl: NSString) throws -> String { let dataSplit = (dataUrl as String).components(separatedBy: ";base64,") if(dataSplit.count != 2){ - reject("@ConvertError", "Cannot imageDataUrlToJpegDataUrl image because is not a valid dataurl", nil) - return + throw EidReaderError.invalidInput("Cannot imageDataUrlToJpegDataUrl image because is not a valid dataurl") } if let mimeType = dataSplit.first?.replacingOccurrences(of: "data:", with: ""){ if(!mimeType.hasPrefix("image/")){ - reject("@ConvertError", "Couldn't convert \(mimeType) to JPEG", nil) - return + throw EidReaderError.invalidInput("Couldn't convert \(mimeType) to JPEG") } if(mimeType == "image/jpeg"){ - resolve(dataUrl) - return + return dataUrl as String } let dataContent = dataSplit[1] if let newData = Data(base64Encoded: dataContent) { if let jpegData = UIImage(data: newData)?.jpegData(compressionQuality: 1.0)?.base64EncodedString(){ - resolve("data:image/jpeg;base64,\(jpegData)") - return + return "data:image/jpeg;base64,\(jpegData)" + } } } - reject("@ConvertError", "Convert image data URL to JPEG image data url error", nil) + throw EidReaderError.processingError("Convert image data URL to JPEG image data url error") } @objc(stopReading) diff --git a/src/index.tsx b/src/index.tsx index 46e241e..3e74d28 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -81,7 +81,7 @@ export default class EIdReader { return EIdReaderNativeModule.openNfcSettings(); } - static imageDataUrlToJpegDataUrl(dataUrl: string): Promise { + static imageDataUrlToJpegDataUrl(dataUrl: string): string { return EIdReaderNativeModule.imageDataUrlToJpegDataUrl(dataUrl); }