Users can generate QR codes with their name & email using native Android code that automatically clears on logout/new login for security.
This commit is contained in:
@@ -72,6 +72,11 @@ def enableProguardInReleaseBuilds = false
|
||||
*/
|
||||
def jscFlavor = 'io.github.react-native-community:jsc-android:2026004.+'
|
||||
|
||||
repositories {
|
||||
google()
|
||||
mavenCentral()
|
||||
}
|
||||
|
||||
android {
|
||||
ndkVersion rootProject.ext.ndkVersion
|
||||
buildToolsVersion rootProject.ext.buildToolsVersion
|
||||
@@ -116,4 +121,7 @@ dependencies {
|
||||
} else {
|
||||
implementation jscFlavor
|
||||
}
|
||||
|
||||
// QR Code generation
|
||||
implementation 'com.google.zxing:core:3.5.1'
|
||||
}
|
||||
|
||||
@@ -4,6 +4,12 @@ import com.facebook.react.bridge.ReactApplicationContext
|
||||
import com.facebook.react.bridge.ReactContextBaseJavaModule
|
||||
import com.facebook.react.bridge.ReactMethod
|
||||
import com.facebook.react.bridge.Promise
|
||||
import android.graphics.Bitmap
|
||||
import android.graphics.Color
|
||||
import android.util.Base64
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.nio.charset.StandardCharsets
|
||||
import java.security.MessageDigest
|
||||
|
||||
class MyNativeModule(private val reactContext: ReactApplicationContext)
|
||||
: ReactContextBaseJavaModule(reactContext) {
|
||||
@@ -16,4 +22,36 @@ class MyNativeModule(private val reactContext: ReactApplicationContext)
|
||||
fun greet(name: String, promise: Promise) {
|
||||
promise.resolve("Hello $name from Android(Kotlin)!")
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
fun generateQRCode(data: String, width: Int, height: Int, promise: Promise) {
|
||||
try {
|
||||
val bitmap = createQRCodeBitmap(data, width, height)
|
||||
val base64Image = bitmapToBase64(bitmap)
|
||||
promise.resolve(base64Image)
|
||||
} catch (e: Exception) {
|
||||
promise.reject("QR_CODE_ERROR", "Failed to generate QR code: ${e.message}")
|
||||
}
|
||||
}
|
||||
|
||||
private fun createQRCodeBitmap(data: String, width: Int, height: Int): Bitmap {
|
||||
// Use ZXing library for QR code generation
|
||||
val qrCodeWriter = com.google.zxing.qrcode.QRCodeWriter()
|
||||
val bitMatrix = qrCodeWriter.encode(data, com.google.zxing.BarcodeFormat.QR_CODE, width, height)
|
||||
|
||||
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565)
|
||||
for (y in 0 until height) {
|
||||
for (x in 0 until width) {
|
||||
bitmap.setPixel(x, y, if (bitMatrix[x, y]) Color.BLACK else Color.WHITE)
|
||||
}
|
||||
}
|
||||
return bitmap
|
||||
}
|
||||
|
||||
private fun bitmapToBase64(bitmap: Bitmap): String {
|
||||
val outputStream = ByteArrayOutputStream()
|
||||
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
|
||||
val byteArray = outputStream.toByteArray()
|
||||
return "data:image/png;base64," + Base64.encodeToString(byteArray, Base64.DEFAULT)
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user