Asegúrate de tener las dependencias necesarias en tu archivo build.gradle
1 |
implementation("com.google.zxing:core:3.4.1") |
MainActivity.kt
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 |
package com.example.myapplication import android.graphics.Bitmap import android.graphics.Color import android.os.Bundle import androidx.activity.ComponentActivity import androidx.activity.compose.setContent import androidx.compose.foundation.Image import androidx.compose.foundation.layout.* import androidx.compose.material3.Button import androidx.compose.material3.ExperimentalMaterial3Api import androidx.compose.material3.Text import androidx.compose.material3.TextField import androidx.compose.runtime.* import androidx.compose.ui.Alignment import androidx.compose.ui.Modifier import androidx.compose.ui.graphics.asImageBitmap import androidx.compose.ui.layout.ContentScale import androidx.compose.ui.platform.LocalContext import androidx.compose.ui.tooling.preview.Preview import androidx.compose.ui.unit.dp import com.google.zxing.BarcodeFormat import com.google.zxing.WriterException import com.google.zxing.common.BitMatrix import com.google.zxing.qrcode.QRCodeWriter import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.launch import kotlinx.coroutines.withContext class MainActivity : ComponentActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContent { MyApp() } } } @OptIn(ExperimentalMaterial3Api::class) @Composable fun MyApp() { val context = LocalContext.current var text by remember { mutableStateOf("") } var bitmap by remember { mutableStateOf<Bitmap?>(null) } val coroutineScope = rememberCoroutineScope() Column( modifier = Modifier .fillMaxSize() .padding(16.dp), verticalArrangement = Arrangement.Center, horizontalAlignment = Alignment.CenterHorizontally ) { TextField( value = text, onValueChange = { text = it }, label = { Text("Enter text for QR code") }, modifier = Modifier.fillMaxWidth() ) Spacer(modifier = Modifier.height(16.dp)) DisposableEffect(Unit) { onDispose { bitmap = null // Limpiamos el bitmap existente } } Button( onClick = { if (text.isNotEmpty()) { bitmap = null // Limpiamos el bitmap existente coroutineScope.launch { val generatedBitmap = generateQRCode(context, text) bitmap = generatedBitmap } } } ) { Text("Generate QR Code") } Spacer(modifier = Modifier.height(16.dp)) if (bitmap != null) { Image( bitmap = bitmap!!.asImageBitmap(), contentDescription = "QR Code", modifier = Modifier.size(200.dp), contentScale = ContentScale.Fit ) } } } suspend fun generateQRCode( context: android.content.Context, text: String ): Bitmap? { return withContext(Dispatchers.Default) { val qrCodeWriter = QRCodeWriter() try { val bitMatrix: BitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, 512, 512) val width: Int = bitMatrix.width val height: Int = bitMatrix.height val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.RGB_565) for (x in 0 until width) { for (y in 0 until height) { bitmap.setPixel( x, y, if (bitMatrix[x, y]) Color.BLACK else Color.WHITE ) } } bitmap } catch (e: WriterException) { e.printStackTrace() null } } } @Preview(showBackground = true) @Composable fun DefaultPreview() { MyApp() } |