From 54c3846ad5076f0ac422341d3201bc6b85a78fda Mon Sep 17 00:00:00 2001 From: Daniel Fernando Rico Date: Thu, 24 Oct 2024 12:32:21 -0500 Subject: [PATCH] fix: resolve annotations --- .../rn/eidreader/EIdReaderModule.kt | 22 ++++++++---- example/src/App.tsx | 6 ++-- ios/EidReader.mm | 2 +- ios/EidReader.swift | 35 ++++++++++++------- src/index.tsx | 4 +-- 5 files changed, 43 insertions(+), 26 deletions(-) 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 0107bbf..6795fdc 100644 --- a/android/src/main/java/io/twentysixty/rn/eidreader/EIdReaderModule.kt +++ b/android/src/main/java/io/twentysixty/rn/eidreader/EIdReaderModule.kt @@ -286,18 +286,26 @@ class EIdReaderModule(reactContext: ReactApplicationContext) : } @ReactMethod - fun convertJp2000ToJpeg(data:String, promise: Promise){ + fun imageDataUrlToJpegDataUrl(dataUrl:String, promise: Promise){ try { - val dataSplit = data.split(";base64,") + val dataSplit = dataUrl.split(";base64,") + if(dataSplit.size != 2){ + promise.reject("Cannot imageDataUrlToJpegDataUrl image because is not a valid dataurl") + return + } val mimeType = dataSplit[0].split(":")[1] - if(mimeType != "image/jp2"){ - promise.resolve(data) + if(!mimeType.startsWith("image/")){ + promise.reject("Couldn't convert $mimeType to JPEG") + return + } + if(mimeType == "image/jpeg"){ + promise.resolve(dataUrl) return } val dataContent = dataSplit[1] val bitmapUtil = BitmapUtil(reactApplicationContext) val decoded = Base64.decode(dataContent,Base64.DEFAULT) - val nfcImage = bitmapUtil.getImage(decoded.inputStream(), decoded.size,"image/jp2") + val nfcImage = bitmapUtil.getImage(decoded.inputStream(), decoded.size, mimeType) if (nfcImage.bitmap != null) { val byteArrayOutputStream = ByteArrayOutputStream() nfcImage.bitmap!!.compress(Bitmap.CompressFormat.JPEG, 70, byteArrayOutputStream) @@ -305,10 +313,10 @@ class EIdReaderModule(reactContext: ReactApplicationContext) : promise.resolve("data:image/jpeg;base64,"+ Base64.encodeToString(bytes, Base64.CRLF)) return } - else promise.reject("Cannot convertJp2000ToJpeg image") + else promise.reject("Cannot imageDataUrlToJpegDataUrl image") } catch (e: IOException) { - promise.reject("Cannot convertJp2000ToJpeg image") + promise.reject("Cannot imageDataUrlToJpegDataUrl image") return } } diff --git a/example/src/App.tsx b/example/src/App.tsx index a194684..65fa98e 100644 --- a/example/src/App.tsx +++ b/example/src/App.tsx @@ -54,8 +54,8 @@ export default function App() { }); }; - const convertJp2000ToJpeg = () => { - EIdReader.convertJp2000ToJpeg(lena) + const imageDataUrlToJpegDataUrl = () => { + EIdReader.imageDataUrlToJpegDataUrl(lena) .then((data) => { setConvertedImage(`${data}`); }) @@ -101,7 +101,7 @@ export default function App() { Convert diff --git a/ios/EidReader.mm b/ios/EidReader.mm index e876160..e6e8a87 100644 --- a/ios/EidReader.mm +++ b/ios/EidReader.mm @@ -20,7 +20,7 @@ @interface RCT_EXTERN_MODULE(EIdReader, RCTEventEmitter) RCT_EXTERN__BLOCKING_SYNCHRONOUS_METHOD(stopReading); -RCT_EXTERN_METHOD(convertJp2000ToJpeg:(NSString)data +RCT_EXTERN_METHOD(imageDataUrlToJpegDataUrl:(NSString)dataUrl withResolver:(RCTPromiseResolveBlock)resolve withRejecter:(RCTPromiseRejectBlock)reject); diff --git a/ios/EidReader.swift b/ios/EidReader.swift index 97b55ab..8efe074 100644 --- a/ios/EidReader.swift +++ b/ios/EidReader.swift @@ -93,26 +93,35 @@ class EIdReader: RCTEventEmitter { } - @objc(convertJp2000ToJpeg:withResolver:withRejecter:) - func convertJp2000ToJpeg( - _data: NSString, + @objc(imageDataUrlToJpegDataUrl:withResolver:withRejecter:) + func imageDataUrlToJpegDataUrl( + dataUrl: NSString, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock ) { - let dataSplit = (_data as String).components(separatedBy: ";base64,") - let mimeType = dataSplit.first?.replacingOccurrences(of: "data:", with: "") - if(mimeType != "image/jp2"){ - resolve(_data) + 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 } - 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 + if let mimeType = dataSplit.first?.replacingOccurrences(of: "data:", with: ""){ + if(!mimeType.hasPrefix("image/")){ + reject("@ConvertError", "Couldn't convert \(mimeType) to JPEG", nil) + return + } + if(mimeType == "image/jpeg"){ + resolve(dataUrl) + return + } + 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 + } } } - reject("@ConvertError", "Convert from JP2 to JPEG error", nil) + reject("@ConvertError", "Convert image data URL to JPEG image data url error", nil) } @objc(stopReading) diff --git a/src/index.tsx b/src/index.tsx index 8aeaeee..46e241e 100644 --- a/src/index.tsx +++ b/src/index.tsx @@ -81,8 +81,8 @@ export default class EIdReader { return EIdReaderNativeModule.openNfcSettings(); } - static convertJp2000ToJpeg(data: string): Promise { - return EIdReaderNativeModule.convertJp2000ToJpeg(data); + static imageDataUrlToJpegDataUrl(dataUrl: string): Promise { + return EIdReaderNativeModule.imageDataUrlToJpegDataUrl(dataUrl); } private static addListener(