Introducción a la Integración Nativa en Android
Integra APIs Nativas en Android
Creación proyecto base
Google Maps SDK
Cómo crear una API key para Google Maps en Google Cloud
Creación de marcadores en Google Maps con Jetpack Compose
Cómo dibujar rutas en mapas usando polilíneas en Jetpack Compose
Cómo enfocar automáticamente mapas usando Camera Update Factory
Quiz: Google Maps SDK
Servicios de Localización
Uso de Flows en Kotlin para Controlar Intervalos de Tiempo y Emisión de Datos
Cómo simular ubicación en emuladores y dispositivos reales
Creación de Modelos y Cálculos de Localización con Clean Architecture
Implementación de Localización en Android Usando Flows
Inyección de dependencia para seguimiento de localización en Android
Uso de StateFlows para rastrear ubicación en aplicaciones Android
Location Tracker
Implementación de Location Tracker con Inyección de Dependencias
Quiz: Servicios de Localización
Integración Maps con Localización
Integración de mapas dinámicos con CameraPositionState en Android
Creación y uso de polilíneas en mapas con datos reales
Creación de una pantalla de mapa con Intents y estados en Jetpack Compose
Creación de un ViewModel para Seguimiento de Localización en Android
Quiz: Integración Maps con Localización
Manejo de permisos
Gestión de permisos en Android para localización, cámara y notificaciones
Cómo implementar diálogos para solicitar permisos en Android
Manejo de permisos de localización y notificación en Android
Cómo gestionar permisos en Android con Jetpack Compose
Quiz: Manejo de permisos
Integración cámara
Integración de cámara en Android con Photo Handler y manejo de permisos
Convierte Bitmaps a ByteArrays en Android con Kotlin
Creación de intents y estados UI para cámara en Android con Kotlin
Implementación de funciones clave en ViewModel para cámara Android
Integrar CámaraX en Jetpack Compose para Android
Captura y previsualización de fotos en Android con Jetpack Compose
Cómo Mostrar Fotos en Marcadores de Ubicación en Mapas con Jetpack Compose
Quiz: Integración cámara
Servicios en Android
Implementación de servicios en Android: normal services y foreground services
Implementar Foreground Services en Android para Persistencia en Segundo Plano
Quiz: Servicios en Android
Transmisiones en Android (Broadcast)
Implementación de BroadcastReceiver en Android para Escuchar Eventos del Sistema
Pruebas finales y cierre
You don't have access to this class
Keep learning! Join and start boosting your career
Capturing and previewing images from the camera in Android apps with Jetpack Compose is a practical and easy to implement feature. Here's how to use a composable to display preview images before saving or discarding them.
Jetpack Compose allows you to structure effective interfaces quickly. To display an image obtained by the camera, we will use a composable called PhotoPreviewScreen. This view receives:
To convert the ByteArray into something viewable, we use a previously created extension function that converts this array into a Bitmap-like image:
bytesArray.AsImageBitmap().
In the main screen named CameraScreenRoot, the first step is to bind to a ViewModel:
val UIState by ViewModel.UIState.collectAsState()val previewFoto by ViewModel.PreviewFoto.collectAsState().
The interface is mainly composed of a container(Box) with full size that displays a preview image if available:
PhotoPreviewScreen( PhotoBytes = previewPhoto, OnSaved = { ViewModel.Action(CameraIntent.SavePhoto) }, OnCancel = { ViewModel.Action(CameraIntent.CancelPreview) }) )
When a photo is taken through the camera API, by default its orientation comes rotated 90 degrees, which requires an additional transformation. This is achieved by applying a rotation matrix in degrees provided by the camera:
val matrix = Matrix().apply { postRotate(imageProxy.imageInfo.rotationDegrees.toFloat())}val rotatedBitmap = Bitmap.createBitmap( imageProxy.toBitmap(), 0, 0, imageProxy.width, imageProxy.height, matrix, true)ViewModel.Action(CameraIntent.TakenPicture(rotatedBitmap.toByteArray())))
Then, this rotated bitmap converted to ByteArray is the one sent to the ViewModel for previewing.
In the main activity(MainActivity) the basic capture screen is replaced by the one including preview:
CameraScreenRoot()
This logic starts the capture and immediate visual display of the captured photos, allowing to accept or reject each image taken according to what the user decides.
As an additional, but indispensable step, are the permissions that guarantee the proper and secure operation of the camera. The handling of permissions follows the same pattern previously used in screens such as MapScreen. Review that procedure step by step and practice integrating it here, taking advantage of reference resources, such as the project repository, in case of doubts.
Go ahead and implement this useful feature in your app and let us know how it went!
Contributions 0
Questions 0
Want to see more contributions, questions and answers from the community?