Skip to content

Instantly share code, notes, and snippets.

@pengj
pengj / ComposeNavigation.kt
Last active September 16, 2021 07:36
Create Bottom navigation with Jetpack Compose
@Composable
fun MainScreen() {
val navController = rememberNavController()
Scaffold(
bottomBar = { BottomNav(navController) }
) {
Navigation(navController = navController)
}
}
@pengj
pengj / XMLActivity.xml
Created September 7, 2021 16:01
Bottom navigation view
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/nav_view"
class TextAnalyzer(private val onTextDetected: (MLText) -> Unit) : ImageAnalysis.Analyzer {
private val setting = MLLocalTextSetting.Factory()
.setOCRMode(MLLocalTextSetting.OCR_TRACKING_MODE)
.setLanguage("en")
.create()
private val analyzer = MLAnalyzerFactory.getInstance().getLocalTextAnalyzer(setting)
@SuppressLint("UnsafeOptInUsageError")
override fun analyze(imageProxy: ImageProxy) {
@Composable
fun ImageButton(uri: Uri? = null,
imageClicked: (Uri) -> Unit) {
Box(modifier = Modifier.fillMaxSize()) {
uri?.let { imageUri ->
Image(
painter = rememberGlidePainter(
imageUri,
fadeIn = true
),
private fun capture() {
// Create output file to hold the image
val photoFile = createFile(outputFolder, FILENAME, PHOTO_EXTENSION)
// Create output options object which contains file + metadata
val outputOptions = ImageCapture.OutputFileOptions.Builder(photoFile)
.build()
imageCapture.takePicture(outputOptions, Executors.newSingleThreadExecutor(),
object : ImageCapture.OnImageSavedCallback {
class GestureAnalyzer(private val onGestureDetected: (Int) -> Unit) : ImageAnalysis.Analyzer {
private val analyzer = MLGestureAnalyzerFactory
.getInstance().gestureAnalyzer
private var inprogress = false
@SuppressLint("UnsafeExperimentalUsageError")
override fun analyze(imageProxy: ImageProxy) {
if (inprogress) {
@pengj
pengj / ImageAnalysis.kt
Created March 30, 2021 21:42
Image Analysis setup in CameraX
val imageAnalysis = ImageAnalysis.Builder()
.setBackpressureStrategy(ImageAnalysis.STRATEGY_KEEP_ONLY_LATEST)
.build()
.apply {
setAnalyzer(executor,analyzer)
}
cameraProvider.bindToLifecycle(
lifecycleOwner,
cameraSelector,
@pengj
pengj / SimpleCameraPreview.kt
Created March 30, 2021 21:16
Simple compose function for CameraX Preview
@Composable
fun SimpleCameraPreview() {
val lifecycleOwner = LocalLifecycleOwner.current
val context = LocalContext.current
val cameraProviderFuture = remember { ProcessCameraProvider.getInstance(context) }
AndroidView(
factory = { ctx ->
val previewView = PreviewView(ctx)
val executor = ContextCompat.getMainExecutor(ctx)
@pengj
pengj / DirectionProvider.kt
Last active February 16, 2021 22:50
Direction Provider and internal class
interface DirectionProvider {
fun start()
fun stop()
fun destroy()
}
internal class HuaweiVoiceProvider(
private val context: Context,
private val voiceDirectionExtractor: VoiceDirectionExtractor,
private val onDirectionListener: (direction: Direction) -> Unit,