Search code examples
androidandroid-camerax

Android camerax take picture into variable


I need to save a taken picture in android into a variable (string) this is my code:

    var imageCapture = ImageCapture.Builder().build()
    val captureMode = ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY
    val flashMode = ImageCapture.FLASH_MODE_AUTO
    val aspectRatio = AspectRatio.RATIO_16_9
    val metrics = DisplayMetrics().also { previewView.display.getRealMetrics(it) }
    val screenSize = Size(metrics.widthPixels, metrics.heightPixels)

    imageCapture = ImageCapture.Builder()
        .setCaptureMode(captureMode)
        .setTargetAspectRatio(aspectRatio)
        .setFlashMode(flashMode)
        .setTargetResolution(screenSize)
        .setTargetName("CameraConference")
        .build()

imageCapture has a method called takePicture I can't understand how to use imageCapture.takePicture so the output of it will be a variable (string)

Thanks


Solution

  • You can use the ImageCapture#takePicture(Executor executor, ImageCapture.OnImageCapturedCallback callback) API to return an in-memory picture. The returned object is an ImageProxy, which include the actual bytes as well as how the bytes should be transformed to present to end users. ImageProxy#getPlanes() contain the buffer bytes.

    To convert the ImageProxy bytes to a Bitmap, please see the code sample here.