Last active
March 22, 2022 09:24
-
-
Save onliniak/35da0c0babe17f94afdf13f8c14b5b8a to your computer and use it in GitHub Desktop.
Android Compose Jetpack
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.onliniak.qr_scanner | |
import android.os.Bundle | |
import android.util.Log | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.rememberLauncherForActivityResult | |
import androidx.activity.compose.setContent | |
import androidx.activity.result.contract.ActivityResultContracts | |
import androidx.activity.result.launch | |
import androidx.compose.material.Button | |
import androidx.compose.material.MaterialTheme | |
import androidx.compose.material.Surface | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.tooling.preview.Preview | |
import com.google.mlkit.vision.barcode.Barcode | |
import com.google.mlkit.vision.barcode.BarcodeScannerOptions | |
import com.google.mlkit.vision.barcode.BarcodeScanning | |
import com.google.mlkit.vision.common.InputImage | |
import io.github.onliniak.qr_scanner.ui.theme.QR_ScannerTheme | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
QR_ScannerTheme { | |
// A surface container using the 'background' color from the theme | |
Surface(color = MaterialTheme.colors.background) { | |
Greeting() | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
fun Greeting() { | |
//https://developer.android.com/reference/kotlin/androidx/activity/compose/package-summary#rememberlauncherforactivityresult | |
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.TakePicturePreview()) { | |
val options = BarcodeScannerOptions.Builder() | |
.setBarcodeFormats( | |
Barcode.FORMAT_QR_CODE, | |
Barcode.FORMAT_AZTEC) | |
.build() | |
val scanner = BarcodeScanning.getClient(options) | |
scanner.process(InputImage.fromBitmap(it, 0)) | |
.addOnSuccessListener { barcodes -> | |
for (barcode in barcodes) { | |
val bounds = barcode.boundingBox | |
val corners = barcode.cornerPoints | |
val rawValue = barcode.rawValue | |
Log.i("bounds", bounds.toString()) | |
Log.i("corners", corners.toString()) | |
Log.i("rawValue", rawValue) | |
// See API reference for complete list of supported types | |
// when (barcode.valueType) { | |
// Barcode.TYPE_WIFI -> { | |
// val ssid = barcode.wifi!!.ssid | |
// val password = barcode.wifi!!.password | |
// val type = barcode.wifi!!.encryptionType | |
// } | |
// Barcode.TYPE_URL -> { | |
// val title = barcode.url!!.title | |
// val url = barcode.url!!.url | |
// } | |
// } | |
} | |
} | |
.addOnFailureListener { e -> | |
Log.w("problem", e.toString()) | |
} | |
} | |
Button(onClick = { launcher.launch() }) { | |
Text(text = "Take a picture") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Na chwilę obecną OpenCV to najgorsza biblioteka. | |
// Radzi sobie jedynie z tekstem. | |
package io.github.onliniak.qr_scanner | |
import android.os.Bundle | |
// import android.util.Log | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.rememberLauncherForActivityResult | |
import androidx.activity.compose.setContent | |
import androidx.activity.result.contract.ActivityResultContracts | |
import androidx.activity.result.launch | |
import androidx.compose.foundation.layout.Column | |
import androidx.compose.material.Button | |
import androidx.compose.material.MaterialTheme | |
import androidx.compose.material.Surface | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.* | |
import androidx.compose.ui.tooling.preview.Preview | |
import io.github.onliniak.qr_scanner.ui.theme.QR_ScannerTheme | |
import org.opencv.android.OpenCVLoader | |
import org.opencv.android.Utils | |
import org.opencv.core.Mat | |
import org.opencv.objdetect.QRCodeDetector | |
data class CounterState(val counter: String = "") | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
OpenCVLoader.initDebug() | |
setContent { | |
QR_ScannerTheme { | |
// A surface container using the 'background' color from the theme | |
Surface(color = MaterialTheme.colors.background) { | |
Greeting() | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
fun Greeting() { | |
var counterState by remember { mutableStateOf(CounterState()) } | |
// Start camera app (for example Google Camera or whatever) | |
val launcher = rememberLauncherForActivityResult(ActivityResultContracts.TakePicturePreview()){bitmap -> | |
// We need picture in specific "OpenCV Mat" format | |
// Convert android's bitmap to OpenCV Mat | |
// Output as mat variable | |
val mat = Mat() | |
Utils.bitmapToMat(bitmap, mat) | |
// Decoded string | |
val photo = QRCodeDetector().detectAndDecode(mat) | |
counterState = counterState.copy(counter = photo) | |
} | |
Column { | |
Button(onClick = { launcher.launch() }) { | |
Text(text = "Take a picture") | |
} | |
Text(text = "QR: " + counterState.counter) | |
} | |
} | |
@Preview(showBackground = true) | |
@Composable | |
fun DefaultPreview() { | |
QR_ScannerTheme { | |
Greeting() | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.onliniak.qr_scanner | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.material.MaterialTheme | |
import androidx.compose.material.Surface | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.tooling.preview.Preview | |
import io.github.onliniak.qr_scanner.ui.theme.QR_ScannerTheme | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
QR_ScannerTheme { | |
// A surface container using the 'background' color from the theme | |
Surface(color = MaterialTheme.colors.background) { | |
Greeting("Android") | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
fun Greeting(name: String) { | |
Text(text = "Hello $name!") | |
} | |
@Preview(showBackground = true) | |
@Composable | |
fun DefaultPreview() { | |
QR_ScannerTheme { | |
Greeting("Android") | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package io.github.onliniak.qr_scanner | |
import android.os.Bundle | |
import androidx.activity.ComponentActivity | |
import androidx.activity.compose.setContent | |
import androidx.compose.material.MaterialTheme | |
import androidx.compose.material.Surface | |
import androidx.compose.material.Text | |
import androidx.compose.runtime.* | |
import androidx.compose.runtime.Composable | |
import androidx.compose.ui.tooling.preview.Preview | |
import io.github.onliniak.qr_scanner.ui.theme.QR_ScannerTheme | |
// https://github.com/vinaygaba/Learn-Jetpack-Compose-By-Example/blob/master/app/src/main/java/com/example/jetpackcompose/state/StateActivity.kt | |
data class CounterState(val counter: Int = 0) | |
class MainActivity : ComponentActivity() { | |
override fun onCreate(savedInstanceState: Bundle?) { | |
super.onCreate(savedInstanceState) | |
setContent { | |
QR_ScannerTheme { | |
// A surface container using the 'background' color from the theme | |
Surface(color = MaterialTheme.colors.background) { | |
Greeting() | |
} | |
} | |
} | |
} | |
} | |
@Composable | |
fun Greeting() { | |
var counterState by remember { mutableStateOf(CounterState()) } | |
Button( | |
onClick = { | |
counterState = counterState.copy(counter = counterState.counter + 1) | |
}) { | |
Text(text = "Counter value is ${counterState.counter}") | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment