Skip to content

Commit

Permalink
feat: convert imageDataUrlToJpegDataUrl to synchronous method
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielFRico committed Oct 25, 2024
1 parent 6b3f60e commit 5f0401c
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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")
}
}

Expand Down
26 changes: 7 additions & 19 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@ import { lena } from './data';

export default function App() {
const [result, setResult] = React.useState<EIdReadResult>();
const [convertedImage, setConvertedImage] = React.useState<
string | undefined
>(undefined);

let convertedImage;
try {
convertedImage = EIdReader.imageDataUrlToJpegDataUrl(lena);
} catch (error) {
console.error(error);
}

React.useEffect(() => {
EIdReader.addOnTagDiscoveredListener(() => {
Expand Down Expand Up @@ -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();
};
Expand Down Expand Up @@ -100,12 +94,6 @@ export default function App() {
<ScrollView style={styles.container}>
<View style={styles.box}>
<View style={styles.buttonContainer}>
<TouchableOpacity
onPress={imageDataUrlToJpegDataUrl}
style={styles.button}
>
<Text style={styles.buttonText}>Convert</Text>
</TouchableOpacity>
<TouchableOpacity onPress={startReading} style={styles.button}>
<Text style={styles.buttonText}>Start Reading</Text>
</TouchableOpacity>
Expand Down
4 changes: 1 addition & 3 deletions ios/EidReader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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
{
Expand Down
28 changes: 13 additions & 15 deletions ios/EidReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 1 addition & 1 deletion src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ export default class EIdReader {
return EIdReaderNativeModule.openNfcSettings();
}

static imageDataUrlToJpegDataUrl(dataUrl: string): Promise<string> {
static imageDataUrlToJpegDataUrl(dataUrl: string): string {
return EIdReaderNativeModule.imageDataUrlToJpegDataUrl(dataUrl);
}

Expand Down

0 comments on commit 5f0401c

Please sign in to comment.