Skip to content

Commit

Permalink
feat: make imageDataUrlToJpegDataUrl sync (#3)
Browse files Browse the repository at this point in the history
* feat: convert imageDataUrlToJpegDataUrl to synchronous method"

* feat: convert imageDataUrlToJpegDataUrl to synchronous method

* feat: convert imageDataUrlToJpegDataUrl to synchronous method
  • Loading branch information
DanielFRico authored Oct 25, 2024
1 parent b8defbf commit 1e114c0
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 53 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
2 changes: 1 addition & 1 deletion example/src/data.ts

Large diffs are not rendered by default.

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
24 changes: 8 additions & 16 deletions ios/EidReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -92,36 +92,28 @@ class EIdReader: RCTEventEmitter {
}

}

@objc(imageDataUrlToJpegDataUrl:withResolver:withRejecter:)
func imageDataUrlToJpegDataUrl(
dataUrl: NSString,
resolve: @escaping RCTPromiseResolveBlock,
reject: @escaping RCTPromiseRejectBlock
) {

@objc(imageDataUrlToJpegDataUrl:)
func imageDataUrlToJpegDataUrl(dataUrl: NSString) -> 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
return nil
}
if let mimeType = dataSplit.first?.replacingOccurrences(of: "data:", with: ""){
if(!mimeType.hasPrefix("image/")){
reject("@ConvertError", "Couldn't convert \(mimeType) to JPEG", nil)
return
return nil
}
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)
return nil
}

@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 1e114c0

Please sign in to comment.