Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: convert imageDataUrlToJpegDataUrl to synchronous method #3

Merged
merged 3 commits into from
Oct 25, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@

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,23 +58,13 @@
});
};

const imageDataUrlToJpegDataUrl = () => {
EIdReader.imageDataUrlToJpegDataUrl(lena)
.then((data) => {
setConvertedImage(`${data}`);
})
.catch((error) => {
console.error('error', error);
});
};

const stopReading = () => {
EIdReader.stopReading();
};

const openNfcSettings = async () => {
try {
const result = await EIdReader.openNfcSettings();

Check warning on line 67 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

'result' is already declared in the upper scope on line 17 column 10
console.log(result);
} catch (e) {
console.log(e);
Expand All @@ -79,7 +73,7 @@

const isNfcSupported = async () => {
try {
const result = await EIdReader.isNfcSupported();

Check warning on line 76 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

'result' is already declared in the upper scope on line 17 column 10
console.log(result);
} catch (e) {
console.log(e);
Expand All @@ -88,7 +82,7 @@

const isNfcEnabled = async () => {
try {
const result = await EIdReader.isNfcEnabled();

Check warning on line 85 in example/src/App.tsx

View workflow job for this annotation

GitHub Actions / lint

'result' is already declared in the upper scope on line 17 column 10
console.log(result);
} catch (e) {
console.log(e);
Expand All @@ -100,12 +94,6 @@
<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
Loading