Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

takePicture require executor on CameraX (1.0.0-alpha06)

After update from

androidx.camera:camera-core:1.0.0-alpha03

to

androidx.camera:camera-core:1.0.0-alpha06

signatures of methods setTargetAspectRatio (in ImageCaptureConfig.Builder) and takePicture (in ImageCapture) have been changed.

Official documentation and info in web doesn't show how to use new methods (how to specify executor).

Code which broken after update:

...
val captureConfig = ImageCaptureConfig.Builder()
    .setTargetAspectRatioCustom(Rational(1, 1)) //this method changed
    .setFlashMode(flashMode)
    .setLensFacing(lensFacing)
    .build()

val capture = ImageCapture(captureConfig)

binding.takeAPhoto.setOnClickListener {
    ...
    val imageFile = createTempFile(System.currentTimeMillis().toString(), ".jpg")
    capture.takePicture(imageFile, object : ImageCapture.OnImageSavedListener { //this method also changed

        override fun onImageSaved(file: File) {
            ...
        }

        override fun onError(useCaseError: ImageCapture.UseCaseError, message: String, cause: Throwable?) {
            ...
        })
    }
}

Does anyone have (or know where to find) example of how to use new methods? Thanks in advance

like image 332
Peter Staranchuk Avatar asked Oct 14 '19 09:10

Peter Staranchuk


2 Answers

The official Google Codelabs which obviously have been updated recently use: Executors.newSingleThreadExecutor()

Reference: https://codelabs.developers.google.com/codelabs/camerax-getting-started/#4

Edit: Since @kos's response also makes sense to me, I've added these two official Android docs references:

https://developer.android.com/reference/java/util/concurrent/Executors.html#newSingleThreadExecutor()

https://developer.android.com/reference/java/util/concurrent/Executors.html#newCachedThreadPool()

This way every reader of this topic can make up his/her own mind with respect to executors.

FURTHER EDIT: There are crucial API changes since 1.0.0-alpha07 so I studied some of the docs. There's a GitHub sample showing executor retrieval like so mainExecutor = ContextCompat.getMainExecutor(requireContext())(Source)

If some of you already implemented CameraX and it works fine, I'd definitely wait for the beta release as recommended by Android's release notes

like image 146
postfixNotation Avatar answered Sep 24 '22 07:09

postfixNotation


You can do it like this.

imageCapture.takePicture(file, { it.run() }, object : ImageCapture.OnImageSavedListener {
    override fun onImageSaved(file: File) {}
    override fun onError(useCaseError: ImageCapture.ImageCaptureError, message: String, cause: Throwable?) {}
})
like image 36
axierjhtjz Avatar answered Sep 22 '22 07:09

axierjhtjz