Skip to content

Commit

Permalink
fix: resolve annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
DanielFRico committed Oct 24, 2024
1 parent 05d7a9c commit 54c3846
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -286,29 +286,37 @@ 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)
val bytes = byteArrayOutputStream.toByteArray()
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
}
}
Expand Down
6 changes: 3 additions & 3 deletions example/src/App.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -54,8 +54,8 @@ export default function App() {
});
};

const convertJp2000ToJpeg = () => {
EIdReader.convertJp2000ToJpeg(lena)
const imageDataUrlToJpegDataUrl = () => {
EIdReader.imageDataUrlToJpegDataUrl(lena)
.then((data) => {
setConvertedImage(`${data}`);
})
Expand Down Expand Up @@ -101,7 +101,7 @@ export default function App() {
<View style={styles.box}>
<View style={styles.buttonContainer}>
<TouchableOpacity
onPress={convertJp2000ToJpeg}
onPress={imageDataUrlToJpegDataUrl}
style={styles.button}
>
<Text style={styles.buttonText}>Convert</Text>
Expand Down
2 changes: 1 addition & 1 deletion ios/EidReader.mm
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
35 changes: 22 additions & 13 deletions ios/EidReader.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
4 changes: 2 additions & 2 deletions src/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ export default class EIdReader {
return EIdReaderNativeModule.openNfcSettings();
}

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

private static addListener(
Expand Down

0 comments on commit 54c3846

Please sign in to comment.