Introducción a la Integración Nativa en Android

1

Integra APIs Nativas en Android

2

Creación proyecto base

Google Maps SDK

3

Cómo crear una API key para Google Maps en Google Cloud

4

Creación de marcadores en Google Maps con Jetpack Compose

5

Cómo dibujar rutas en mapas usando polilíneas en Jetpack Compose

6

Cómo enfocar automáticamente mapas usando Camera Update Factory

Quiz: Google Maps SDK

Servicios de Localización

7

Uso de Flows en Kotlin para Controlar Intervalos de Tiempo y Emisión de Datos

8

Cómo simular ubicación en emuladores y dispositivos reales

9

Creación de Modelos y Cálculos de Localización con Clean Architecture

10

Implementación de Localización en Android Usando Flows

11

Inyección de dependencia para seguimiento de localización en Android

12

Uso de StateFlows para rastrear ubicación en aplicaciones Android

13

Location Tracker

14

Implementación de Location Tracker con Inyección de Dependencias

Quiz: Servicios de Localización

Integración Maps con Localización

15

Integración de mapas dinámicos con CameraPositionState en Android

16

Creación y uso de polilíneas en mapas con datos reales

17

Creación de una pantalla de mapa con Intents y estados en Jetpack Compose

18

Creación de un ViewModel para Seguimiento de Localización en Android

Quiz: Integración Maps con Localización

Manejo de permisos

19

Gestión de permisos en Android para localización, cámara y notificaciones

20

Cómo implementar diálogos para solicitar permisos en Android

21

Manejo de permisos de localización y notificación en Android

22

Cómo gestionar permisos en Android con Jetpack Compose

Quiz: Manejo de permisos

Integración cámara

23

Integración de cámara en Android con Photo Handler y manejo de permisos

24

Convierte Bitmaps a ByteArrays en Android con Kotlin

25

Creación de intents y estados UI para cámara en Android con Kotlin

26

Implementación de funciones clave en ViewModel para cámara Android

27

Integrar CámaraX en Jetpack Compose para Android

28

Captura y previsualización de fotos en Android con Jetpack Compose

29

Cómo Mostrar Fotos en Marcadores de Ubicación en Mapas con Jetpack Compose

Quiz: Integración cámara

Servicios en Android

30

Implementación de servicios en Android: normal services y foreground services

31

Implementar Foreground Services en Android para Persistencia en Segundo Plano

Quiz: Servicios en Android

Transmisiones en Android (Broadcast)

32

Implementación de BroadcastReceiver en Android para Escuchar Eventos del Sistema

33

Pruebas finales y cierre

You don't have access to this class

Keep learning! Join and start boosting your career

Aprovecha el precio especial y haz tu profesión a prueba de IA

Antes: $249

Currency
$209
Suscríbete

Termina en:

0 Días
5 Hrs
16 Min
43 Seg

Captura y previsualización de fotos en Android con Jetpack Compose

28/33
Resources

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.

How to create a PhotoPreviewScreen using Jetpack Compose?

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:

  • A ByteArray with the captured image.
  • A lambda called OnSaved, in charge of saving the photo.
  • A lambda called OnCanceled, to cancel the operation and return to the capture view.

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().

What logic applies to our CameraScreenRoot?

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:

  • When a preview image exists, display PhotoPreviewScreen, using the data obtained from ViewModel.
PhotoPreviewScreen(    PhotoBytes  = previewPhoto,    OnSaved  =  { ViewModel.Action(CameraIntent.SavePhoto)  },    OnCancel  =  { ViewModel.Action(CameraIntent.CancelPreview)  }) )
  • Otherwise, show the main capture view(CameraScreen).

How to rotate the captured image correctly?

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.

How to definitively implement this solution?

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.

What permissions do I need to request?

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

Sort by:

Want to see more contributions, questions and answers from the community?