added ImageUtils to decode photo

This commit is contained in:
0xturboblitz
2023-07-25 20:18:28 +02:00
parent f013fa6941
commit b01a55d4b5
3 changed files with 58 additions and 114 deletions

View File

@@ -1,57 +1,57 @@
// /*
// * Copyright 2016 - 2022 Anton Tananaev (anton.tananaev@gmail.com)
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
// package io.tradle.nfc
/*
* Copyright 2016 - 2022 Anton Tananaev (anton.tananaev@gmail.com)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package io.tradle.nfc
// import android.content.Context
// import android.graphics.Bitmap
// import android.graphics.BitmapFactory
// import com.gemalto.jp2.JP2Decoder
// import org.jnbis.WsqDecoder
// import java.io.InputStream
import android.content.Context
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import com.gemalto.jp2.JP2Decoder
import org.jnbis.WsqDecoder
import java.io.InputStream
// object ImageUtil {
object ImageUtil {
// fun decodeImage(context: Context?, mimeType: String, inputStream: InputStream?): Bitmap {
// return if (mimeType.equals("image/jp2", ignoreCase = true) || mimeType.equals(
// "image/jpeg2000",
// ignoreCase = true
// )
// ) {
// JP2Decoder(inputStream).decode()
// } else if (mimeType.equals("image/x-wsq", ignoreCase = true)) {
// val wsqDecoder = WsqDecoder()
// val bitmap = wsqDecoder.decode(inputStream)
// val byteData = bitmap.pixels
// val intData = IntArray(byteData.size)
// for (j in byteData.indices) {
// intData[j] = 0xFF000000.toInt() or
// (byteData[j].toInt() and 0xFF shl 16) or
// (byteData[j].toInt() and 0xFF shl 8) or
// (byteData[j].toInt() and 0xFF)
// }
// Bitmap.createBitmap(
// intData,
// 0,
// bitmap.width,
// bitmap.width,
// bitmap.height,
// Bitmap.Config.ARGB_8888
// )
// } else {
// BitmapFactory.decodeStream(inputStream)
// }
// }
// }
fun decodeImage(context: Context?, mimeType: String, inputStream: InputStream?): Bitmap {
return if (mimeType.equals("image/jp2", ignoreCase = true) || mimeType.equals(
"image/jpeg2000",
ignoreCase = true
)
) {
JP2Decoder(inputStream).decode()
} else if (mimeType.equals("image/x-wsq", ignoreCase = true)) {
val wsqDecoder = WsqDecoder()
val bitmap = wsqDecoder.decode(inputStream)
val byteData = bitmap.pixels
val intData = IntArray(byteData.size)
for (j in byteData.indices) {
intData[j] = 0xFF000000.toInt() or
(byteData[j].toInt() and 0xFF shl 16) or
(byteData[j].toInt() and 0xFF shl 8) or
(byteData[j].toInt() and 0xFF)
}
Bitmap.createBitmap(
intData,
0,
bitmap.width,
bitmap.width,
bitmap.height,
Bitmap.Config.ARGB_8888
)
} else {
BitmapFactory.decodeStream(inputStream)
}
}
}

View File

@@ -26,7 +26,6 @@ import android.nfc.Tag
import android.nfc.tech.IsoDep
import android.os.AsyncTask
import android.os.Bundle
// import android.preference.PreferenceManager
import android.text.Editable
import android.text.TextWatcher
import android.util.Base64
@@ -34,9 +33,7 @@ import android.util.Log
import android.widget.EditText
import androidx.appcompat.app.AppCompatActivity
// import com.google.android.material.snackbar.Snackbar
// import com.io.tradle.nfc.ImageUtil.decodeImage
// import com.wdullaer.materialdatetimepicker.date.DatePickerDialog
import io.tradle.nfc.ImageUtil.decodeImage
import net.sf.scuba.smartcards.CardService
import org.apache.commons.io.IOUtils
@@ -75,7 +72,6 @@ import java.security.spec.PSSParameterSpec
import java.text.ParseException
import java.text.SimpleDateFormat
import java.util.*
import java.security.PublicKey
import java.security.spec.X509EncodedKeySpec
import javax.crypto.Cipher
@@ -86,6 +82,7 @@ import okhttp3.MediaType.Companion.toMediaType
import okhttp3.OkHttpClient
import okhttp3.Request
import okhttp3.RequestBody.Companion.toRequestBody
import com.google.gson.Gson;
import com.facebook.react.ReactPackage
@@ -330,14 +327,12 @@ class RNPassportReaderModule(private val reactContext: ReactApplicationContext)
Log.d(TAG, "other data :")
var data = sodFile.dataGroupHashes
Log.d(TAG, "sodFile.docSigningCertificate: ${sodFile.docSigningCertificate}")
Log.d(TAG, "publicKey: ${sodFile.docSigningCertificate.publicKey}")
Log.d(TAG, "publicKey: ${sodFile.docSigningCertificate.publicKey.toString()}")
Log.d(TAG, "publicKey: ${sodFile.docSigningCertificate.publicKey.format}")
Log.d(TAG, "publicKey: ${Base64.encodeToString(sodFile.docSigningCertificate.publicKey.encoded, Base64.DEFAULT)}")
Log.d(TAG, "sodFile.docSigningCertificate: ${gson.toJson(sodFile.docSigningCertificate)}")
val hexMap = sodFile.dataGroupHashes.mapValues { (_, value) ->
value.joinToString("") { "%02x".format(it) }
@@ -385,8 +380,8 @@ class RNPassportReaderModule(private val reactContext: ReactApplicationContext)
val buffer = ByteArray(imageLength)
dataInputStream.readFully(buffer, 0, imageLength)
val inputStream: InputStream = ByteArrayInputStream(buffer, 0, imageLength)
// bitmap = decodeImage(this@MainActivity, faceImageInfo.mimeType, inputStream)
// imageBase64 = Base64.encodeToString(buffer, Base64.DEFAULT)
bitmap = decodeImage(reactContext, faceImageInfo.mimeType, inputStream)
imageBase64 = Base64.encodeToString(buffer, Base64.DEFAULT)
}
} catch (e: Exception) {
return e

View File

@@ -1,51 +0,0 @@
// /*
// * Copyright 2016 - 2022 Anton Tananaev (anton.tananaev@gmail.com)
// *
// * Licensed under the Apache License, Version 2.0 (the "License");
// * you may not use this file except in compliance with the License.
// * You may obtain a copy of the License at
// *
// * http://www.apache.org/licenses/LICENSE-2.0
// *
// * Unless required by applicable law or agreed to in writing, software
// * distributed under the License is distributed on an "AS IS" BASIS,
// * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// * See the License for the specific language governing permissions and
// * limitations under the License.
// */
// package io.tradle.nfc
// import android.os.Bundle
// import android.widget.ImageView
// import android.widget.TextView
// import androidx.appcompat.app.AppCompatActivity
// class ResultActivity : AppCompatActivity() {
// override fun onCreate(savedInstanceState: Bundle?) {
// super.onCreate(savedInstanceState)
// setContentView(R.layout.activity_result)
// findViewById<TextView>(R.id.output_first_name).text = intent.getStringExtra(KEY_FIRST_NAME)
// findViewById<TextView>(R.id.output_last_name).text = intent.getStringExtra(KEY_LAST_NAME)
// findViewById<TextView>(R.id.output_gender).text = intent.getStringExtra(KEY_GENDER)
// findViewById<TextView>(R.id.output_state).text = intent.getStringExtra(KEY_STATE)
// findViewById<TextView>(R.id.output_nationality).text = intent.getStringExtra(KEY_NATIONALITY)
// findViewById<TextView>(R.id.output_passive_auth).text = intent.getStringExtra(KEY_PASSIVE_AUTH)
// findViewById<TextView>(R.id.output_chip_auth).text = intent.getStringExtra(KEY_CHIP_AUTH)
// if (intent.hasExtra(KEY_PHOTO)) {
// @Suppress("DEPRECATION")
// findViewById<ImageView>(R.id.view_photo).setImageBitmap(intent.getParcelableExtra(KEY_PHOTO))
// }
// }
// companion object {
// const val KEY_FIRST_NAME = "firstName"
// const val KEY_LAST_NAME = "lastName"
// const val KEY_GENDER = "gender"
// const val KEY_STATE = "state"
// const val KEY_NATIONALITY = "nationality"
// const val KEY_PHOTO = "photo"
// const val KEY_PHOTO_BASE64 = "photoBase64"
// const val KEY_PASSIVE_AUTH = "passiveAuth"
// const val KEY_CHIP_AUTH = "chipAuth"
// }
// }