How does CameraX library can turn ON/OFF the torch? - android

I am developing a feature with the possibility of switching the torch into ON/OFF states. Some days ago, we saw a new library from Google in io2019. I came up with an idea, why not use it.
After some time, I don't see any possibilities to use the only torch from the library.
Even in the official documentation, I wasn't able to find any good pieces of information for me, what's more, the sample app from their also don't have to handle my case.
Do you have something in mind what is easy to implement or perhaps you know how to do it with CameraX?
I am worried about using camera or camera2 because the amount of code to be paste is terrible.
Links:
[1] https://developer.android.com/training/camerax
[2] https://proandroiddev.com/android-camerax-preview-analyze-capture-1b3f403a9395
[3] https://github.com/android/camera/tree/master/CameraXBasic
[4] https://github.com/android/camera/tree/master/CameraXBasic
CameraX is an Android Jetpack library that was built with the intent to make camera development easier.

androidx.camera:camera-core:1.0.0-alpha10
You can check is torch available or not with this:
val camera = cameraProvider.bindToLifecycle(lifecycleOwner, cameraSelector, preview, imageAnalyzer)
camera.cameraInfo.hasFlashUnit()
And you can enable torch with:
camera.cameraControl.enableTorch(true)

2021 syntax.
Turn on torch on Android, using Java.
Your typical camera preview code (such as from the google example) generally ends like this:
cameraProvider.bindToLifecycle((LifecycleOwner)this,
cameraSelector, imageAnalysis, preview);
to turn on/off the torch...
Camera cam = cameraProvider.bindToLifecycle((LifecycleOwner)this,
cameraSelector, imageAnalysis, preview);
if ( cam.getCameraInfo().hasFlashUnit() ) {
cam.getCameraControl().enableTorch(true); // or false
}
and that's it!

2022 Syntax
imageCapture = ImageCapture.Builder()
.setFlashMode(ImageCapture.FLASH_MODE_ON)
.build()
val camera = cameraProvider.bindToLifecycle(
this, cameraSelector, preview, imageCapture, imageAnalyzer)
if (camera.cameraInfo.hasFlashUnit()) {
camera.cameraControl.enableTorch(true)
}

This is one way you can do it (Kotlin). If there is a better way please let me know. Following code assumes you have already established the availability of flash on the device.
Declare a flashMode var
private var flashMode: Int = ImageCapture.FLASH_MODE_OFF
In updateCameraUI set a listener
controls.findViewById<ImageButton>(R.id.flash_button).setOnClickListener {
when (flashMode) {
ImageCapture.FLASH_MODE_OFF ->
flashMode = ImageCapture.FLASH_MODE_ON
ImageCapture.FLASH_MODE_ON ->
flashMode = ImageCapture.FLASH_MODE_AUTO
ImageCapture.FLASH_MODE_AUTO ->
flashMode = ImageCapture.FLASH_MODE_OFF
}
// Re-bind use cases to include changes
bindCameraUseCases()
}
In bindCameraUseCases set the flash mode
imageCapture = ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
.setTargetAspectRatio(screenAspectRatio)
.setTargetResolution(screenSize)
.setTargetRotation(rotation)
.setFlashMode(flashMode)
.build()

I can't comment so I'm answering to expand on yevhen_69's answer.
Setting enabledTorch(true) didn't work for me either, however I found I had to set enableTorch(true) after the call to CameraX.bindToLifecycle
val previewConfig = PreviewConfig.Builder().apply {
setLensFacing(lensFacing)
// Any setup
setTargetRotation(viewFinder.display.rotation)
}.build()
val preview = Preview(previewConfig)
CameraX.bindToLifecycle(this, preview)
preview.enableTorch(true)
However on a side note, CameraX is still in Alpha so its advisable still to use Camera2 API.

// CameraX
def cameraXVersion = "1.0.0-beta07"
implementation "androidx.camera:camera-camera2:$cameraXVersion"
implementation "androidx.camera:camera-lifecycle:$cameraXVersion"
implementation "androidx.camera:camera-view:1.0.0-alpha14"
private fun initializeFlashButton() = with(binding) {
camera?.apply {
if (cameraInfo.hasFlashUnit()) {
flashButton.setOnClickListener {
flashButton.visibility = View.VISIBLE
cameraControl.enableTorch(cameraInfo.torchState.value == TorchState.OFF)
}
} else {
flashButton.visibility = View.GONE
}
cameraInfo.torchState.observe(viewLifecycleOwner) { torchState ->
if (torchState == TorchState.OFF) {
flashButton.setImageResource(R.drawable.ic_flash)
} else {
flashButton.setImageResource(R.drawable.ic_flash_active)
}
}
}
}
You need execute this method after initialize camera object

Use CameraControl as global variable and boolean for turn off and on.
lateinit var cameraControl: CameraControl
private var flashFlag: Boolean = true
Turn off and on by click listener.
flashFlag = !flashFlag
cameraControl.enableTorch(flashFlag)
In this function I have started the camera preview.
private fun startCamera() {
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
// Used to bind the lifecycle of cameras to the lifecycle owner
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
// Preview
val preview = Preview.Builder()
.build()
.also {
it.setSurfaceProvider(binding.cameraView.surfaceProvider)
}
// Select back camera as a default
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
try {
// Unbind use cases before rebinding
cameraProvider.unbindAll()
// Bind use cases to camera
val camera = cameraProvider.bindToLifecycle(
this, cameraSelector, preview
)
cameraControl = camera.cameraControl
cameraControl.enableTorch(flashFlag)
} catch (exc: Exception) {
Log.e(TAG, "Use case binding failed", exc)
}
}, ContextCompat.getMainExecutor(this))
}

val previewConfig = PreviewConfig.Builder().apply {
setLensFacing(lensFacing)
// Any setup
setTargetRotation(viewFinder.display.rotation)
}.build()
val preview = Preview(previewConfig)
preview.enableTorch(true)

androidx.camera:camera-core:1.0.0-alpha06
CameraX new release provide these features. CameraInfo added with check Flash Available and Sensor Rotation APIs, refer this link
try {
CameraInfo cameraInfo = CameraX.getCameraInfo(currentCameraLensFacing);
LiveData<Boolean> isFlashAvailable = cameraInfo.isFlashAvailable();
flashToggle.setVisibility(isFlashAvailable.getValue() ? View.VISIBLE : View.INVISIBLE);
} catch (CameraInfoUnavailableException e) {
Log.w(TAG, "Cannot get flash available information", e);
flashToggle.setVisibility(View.VISIBLE);
}

You can enable the torch on the Preview object.
https://developer.android.com/reference/androidx/camera/core/Preview.html#enableTorch(boolean)
And you can set the flash mode (on/off/auto) on the ImageCapture object or on the config builder associated.
https://developer.android.com/reference/androidx/camera/core/ImageCapture.html#setFlashMode(androidx.camera.core.FlashMode)
https://developer.android.com/reference/androidx/camera/core/ImageCaptureConfig.Builder.html#setFlashMode(androidx.camera.core.FlashMode)

Related

How to increase image quality with android cameraX library?

I am creating an application which must implement its own camera.
I use the cameraX library provided by google.
I noticed that there is a difference between the quality of the image captured by my own application, and the image captured by the camera application installed on my phone.
although the 2 photos are captured with the same conditions (light, position...)
especially when I zoom the photo, the details of the image become more blurry to the image captured by my application
(in my own case, my phone is Google Pixel 5)
Please see these 2 photos to see the difference
Image by phone camera
Image by my app
And this is my code
/**
* Initialize CameraX, and prepare to bind the camera use cases
*/
private fun setupCamera()
{
val cameraProviderFuture : ListenableFuture<ProcessCameraProvider> = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener({
cameraProvider = cameraProviderFuture.get()
lensFacing = when
{
hasBackCamera() -> CameraSelector.LENS_FACING_BACK
hasFrontCamera() -> CameraSelector.LENS_FACING_FRONT
else -> throw IllegalStateException("Back and front camera are unavailable")
}
bindCameraUseCases()
setupCameraGestures()
}, ContextCompat.getMainExecutor(this))
}
/**
* Declare and bind preview, capture and analysis use cases.
*/
private fun bindCameraUseCases()
{
lifecycleScope.launch {
val cameraProvider : ProcessCameraProvider = cameraProvider ?: throw IllegalStateException("Camera initialization failed.")
// Try to apply extensions like HDR, NIGHT ##########################################
val extensionsManager : ExtensionsManager = ExtensionsManager.getInstanceAsync(this#ImageCaptureActivity, cameraProvider).await()
val defaultCameraSelector : CameraSelector = CameraSelector.Builder()
.requireLensFacing(lensFacing)
.build()
val finalCameraSelector : CameraSelector = if (extensionsManager.isExtensionAvailable(defaultCameraSelector, ExtensionMode.AUTO))
{
extensionsManager.getExtensionEnabledCameraSelector(defaultCameraSelector, ExtensionMode.AUTO)
}
else
{
defaultCameraSelector
}
// Get screen metrics used to setup camera for full screen resolution
val metrics : DisplayMetrics = resources.displayMetrics
val screenAspectRatio : Int = aspectRatio(metrics.widthPixels, metrics.heightPixels)
val rotation : Int = binding.cameraPreview.display.rotation
preview = Preview.Builder()
// We request aspect ratio but no resolution
.setTargetAspectRatio(screenAspectRatio)
// Set initial target rotation
.setTargetRotation(rotation)
.build()
imageCapture = ImageCapture.Builder()
// We request aspect ratio but no resolution to match preview config, but letting
// CameraX optimize for whatever specific resolution best fits our use cases
.setTargetAspectRatio(screenAspectRatio)
// Set initial target rotation, we will have to call this again if rotation changes
// during the lifecycle of this use case
.setTargetRotation(rotation)
.setCaptureMode(ImageCapture.CAPTURE_MODE_MAXIMIZE_QUALITY)
.setJpegQuality(100)
.build()
imageAnalyzer = ImageAnalysis.Builder()
// We request aspect ratio but no resolution
.setTargetAspectRatio(screenAspectRatio)
.build()
imageAnalyzer?.setAnalyzer(cameraExecutor, LuminosityAnalyzer {})
// Must unbind the use-cases before rebinding them
cameraProvider.unbindAll()
try
{
// A variable number of use-cases can be passed here -
// camera provides access to CameraControl & CameraInfo
camera = cameraProvider.bindToLifecycle(this#ImageCaptureActivity, finalCameraSelector, preview, imageCapture, imageAnalyzer)
// Attach the viewfinder's surface provider to preview use case
preview?.setSurfaceProvider(binding.cameraPreview.surfaceProvider)
}
catch (exception : Exception)
{
exception.printStackTrace()
}
}
}
/**
* [androidx.camera.core.ImageAnalysisConfig] requires enum value of [androidx.camera.core.AspectRatio].
* Currently it has values of 4:3 & 16:9.
*
* Detecting the most suitable ratio for dimensions provided in #params by counting absolute
* of preview ratio to one of the provided values.
*
* #param width - preview width
* #param height - preview height
* #return suitable aspect ratio
*/
private fun aspectRatio(width : Int, height : Int) : Int
{
val previewRatio : Double = max(width, height).toDouble() / min(width, height)
return if (abs(previewRatio - RATIO_4_3_VALUE) <= abs(previewRatio - RATIO_16_9_VALUE))
{
AspectRatio.RATIO_4_3
}
else
{
AspectRatio.RATIO_16_9
}
}
fun captureImage()
{
if (!permissionsOk()) return
// Get a stable reference of the modifiable image capture use case
imageCapture?.let { imageCapture ->
// Create output file to hold the image
val photoFile : File = storageUtils.createFile(
baseFolder = getOutputPath(),
fileName = System.currentTimeMillis().toString(),
fileExtension = StorageUtils.PHOTO_EXTENSION)
// Setup image capture metadata
val metadata : Metadata = Metadata().also {
// Mirror image when using the front camera
it.isReversedHorizontal = lensFacing == CameraSelector.LENS_FACING_FRONT
it.location = locationManager.lastKnownLocation
}
// Create output options object which contains file + metadata
val outputOptions : ImageCapture.OutputFileOptions = ImageCapture.OutputFileOptions.Builder(photoFile)
.setMetadata(metadata)
.build()
imagesAdapter.addImage(photoFile)
// Setup image capture listener which is triggered after photo has been taken
imageCapture.takePicture(outputOptions, cameraExecutor, object : ImageCapture.OnImageSavedCallback
{
override fun onImageSaved(output : ImageCapture.OutputFileResults)
{
val savedUri : Uri = output.savedUri ?: return
StorageUtils.showInGallery(savedUri.path)
binding.list.post {
imagesAdapter.addImage(savedUri.toFile())
binding.list.smoothScrollToPosition(imagesAdapter.itemCount)
}
}
override fun onError(exception : ImageCaptureException)
{
exception.printStackTrace()
}
})
binding.cameraPreview.postDelayed({
binding.backgroundEffect.isVisible = true
binding.cameraPreview.postDelayed({
binding.backgroundEffect.isVisible = false
}, AppUtils.VERY_FAST_ANIMATION_MILLIS)
}, AppUtils.FAST_ANIMATION_MILLIS)
}
}
How can I improve the quality of my images? Is there any thing I should do? is there a special filter or algorithm?
i need your help please
if you took photo on Pixel probably using default cam app (GCam) - this app is fulfilled with quaility improvements backed up by some AI. tough task to comptetite with the biggest in quality... try to take a photo with some 3rd party like OpenCamera and compare this picture with one got by your app
You can use CameraX Extension feature to enable HDR & Low light.
this improves the image quality significantly.

How to take multiple images using CameraX?

My use case is to take two images at one time. first one at 2x zoom and second one at 1x zoom level. Also, I want to save images to files.
My idea of doing it was to take the first image at 2x zoom and when the image is saved set the zoom level at 1x and take the second image when the lens has zoomed to 1x zoom level.
However, when I take the first image the preview is stuck at the first image and callback from setting 1x zoom never happens.
This is how I create the capture use cases.
private void createImageCaptureUseCases() {
ImageCapture imageCapture1 = new ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
.build();
ImageCapture imageCapture2 = new ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
.build();
imageCaptureUseCases.clear();
imageCaptureUseCases.add(imageCapture1);
imageCaptureUseCases.add(imageCapture2);
This is how I first start the camera session.
ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(getContext());
cameraProviderFuture.addListener(() -> {
try {
cameraProvider = cameraProviderFuture.get();
preview = new Preview.Builder().build();
cameraSelector = new CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build();
Camera camera = cameraProvider.bindToLifecycle(
((LifecycleOwner) this),
cameraSelector,
preview,
imageCapture);
camera.getCameraControl().setZoomRatio(2f);
preview.setSurfaceProvider(previewView.createSurfaceProvider(camera.getCameraInfo()));
} catch (InterruptedException | ExecutionException e) {}
}, ContextCompat.getMainExecutor(getContext()));
this is how the capture images is called.
private void captureImage(ImageCapture imageCapture) {
File pictureFile = ImageUtils.createImageFile(getActivity());
ImageCapture.OutputFileOptions options = new
ImageCapture.OutputFileOptions.Builder(pictureFile).build();
final Activity activity = getActivity();
imageCapture.takePicture(options, ContextCompat.getMainExecutor(activity),
new ImageCapture.OnImageSavedCallback() {
#Override
public void onImageSaved(#NonNull ImageCapture.OutputFileResults outputFileResults){
Log.i("my tag", "image Saved: " + pictureFile.getAbsolutePath());
int index = imageCaptureUseCases.indexOf(imageCapture);
cameraProvider.unbind(imageCapture);
if (index < imageCaptureUseCases.size() - 1) {
Camera camera = cameraProvider.bindToLifecycle(
(LifecycleOwner) activity,
cameraSelector,
imageCaptureUseCases.get(index + 1));
ListenableFuture future = camera.getCameraControl().setZoomRatio(1f);
future.addListener(() -> captureImage(imageCaptureUseCases.get(index + 1)),
ContextCompat.getMainExecutor(activity));
} else {
createImageCaptureUseCases();
cameraProvider.unbindAll();
Camera camera = cameraProvider.bindToLifecycle(
(LifecycleOwner) activity,
cameraSelector,
preview,
imageCaptureUseCases.get(0));
camera.getCameraControl().setZoomRatio(2f);
}
}
#Override
public void onError(#NonNull ImageCaptureException exception) {
Log.i("my tag", "image save error: " + pictureFile.getAbsolutePath());
}
});
}
You don't need multiple ImageCapture instances to capture an image with 2 zoom ratios, you can use the same instance, ImageCapture handles taking a picture and saving it/providing it, irrelevant to parameters such as the zoom ratio.
Looking at your code sample, it seems you might not being binding a Preview use case the second time you try to capture an image (with a different zoom ratio). This would explain why your preview is getting stuck after the first image capture. Keep in mind that an ImageCapture use case cannot be bound on its own, it must be bound with at least 1 Preview or ImageAnalysis use case.
Below is a sample to capture 2 images, each with a different zoom ratio. The code contains some repetition, and is all in 1 block, so it can definitely be improved.
private fun setUpCamera() {
val mainExecutor = ContextCompat.getMainExecutor(this)
val cameraProviderFuture = ProcessCameraProvider.getInstance(this)
cameraProviderFuture.addListener(Runnable {
// Wait for the camera provider to be retrieved
val cameraProvider = cameraProviderFuture.get()
// Build your use cases
val preview = Preview.Builder().build()
val imageCapture = ImageCapture.Builder().build()
// Get a camera selector to use
val cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA
// Bind the use cases to a lifecycle
val camera = cameraProvider.bindToLifecycle(this, cameraSelector, preview, imageCapture)
// Set the preview surface provider
preview.setSurfaceProvider(previewView.createSurfaceProvider(camera.cameraInfo))
// Set the zoom ratio for the first photo
val cameraControl = camera.cameraControl
cameraControl.setZoomRatio(1F)
// When the previewView is clicked, take the photos
previewView.setOnClickListener {
imageCapture.takePicture(createOutputFilesOptions(), mainExecutor, object : ImageCapture.OnImageSavedCallback {
override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
// First image captured and saved successfully
Log.d(TAG, "OnImageSavedCallback.onImageSaved: Image saved with zoom ratio 1F")
// Set a new zoom ratio for the second image capture
cameraControl.setZoomRatio(2F)
// Capture the second picture with a different zoom ratio
imageCapture.takePicture(createOutputFilesOptions(), mainExecutor, object : ImageCapture.OnImageSavedCallback {
override fun onImageSaved(outputFileResults: ImageCapture.OutputFileResults) {
// Second image captured and saved successfully
Log.d(TAG, "OnImageSavedCallback.onImageSaved: Image saved with zoom ratio 2F")
}
override fun onError(exception: ImageCaptureException) {
Log.e(TAG, "OnImageSavedCallback.onError", exception)
}
})
}
override fun onError(exception: ImageCaptureException) {
Log.e(TAG, "OnImageSavedCallback.onError", exception)
}
})
}
}, mainExecutor)
}
}

ImageCapture doesn't care about zoomRatio on Camera

I'm willing to implement zoom feature in my app with CameraX API. I followed this medium post to implement pinch to zoom and it works.
The problem is when I retrieve the captured image in onCaptureSuccess callback, The image is not zoomed.
Here is the code I use to implement zoom on Camera in onCreate():
//ZOOM
val listener = object : ScaleGestureDetector.SimpleOnScaleGestureListener() {
override fun onScale(detector: ScaleGestureDetector): Boolean {
val zoomRatio = camera?.cameraInfo?.zoomState?.value?.zoomRatio ?: 0f
val scale = zoomRatio * detector.scaleFactor
camera?.cameraControl?.setZoomRatio(scale)
return true
}
}
scaleDetector = ScaleGestureDetector(context, listener)
And in method "bindCameraUseCases()" :
previewCamera.setOnTouchListener { _, event ->
scaleDetector.onTouchEvent(event)
}
The full method if needed :
/** Declare and bind preview, capture and analysis use cases */
fun bindCameraUseCases() {
// Get screen metrics used to setup camera for full screen resolution
val metrics = DisplayMetrics().also { previewCamera.display.getRealMetrics(it) }
Log.d(TAG, "Screen metrics: ${metrics.widthPixels} x ${metrics.heightPixels}")
val rotation = previewCamera.display.rotation
// Bind the CameraProvider to the LifeCycleOwner
val cameraSelector = CameraSelector.Builder().requireLensFacing(lensFacing).build()
val cameraProviderFuture = ProcessCameraProvider.getInstance(requireContext())
cameraProviderFuture.addListener(Runnable {
// CameraProvider
val cameraProvider: ProcessCameraProvider = cameraProviderFuture.get()
// Preview
preview = Preview.Builder()
.setTargetRotation(rotation)
.build()
previewCamera.preferredImplementationMode =
PreviewView.ImplementationMode.TEXTURE_VIEW // when setting to TEXTURE_VIEW, preview doesnt take full screen on back pressed
previewCamera.setOnTouchListener { _, event ->
scaleDetector.onTouchEvent(event)
}
// Default PreviewSurfaceProvider
preview?.setSurfaceProvider(previewCamera.createSurfaceProvider(camera?.cameraInfo))
val screenAspectRatio = aspectRatio(metrics.widthPixels, metrics.heightPixels)
// ImageCapture
imageCapture = ImageCapture.Builder()
.setCaptureMode(ImageCapture.CAPTURE_MODE_MINIMIZE_LATENCY)
.setTargetAspectRatio(screenAspectRatio)
.setTargetRotation(rotation)
.build()
// ImageAnalysis
imageAnalyzer = ImageAnalysis.Builder()
.setTargetAspectRatio(screenAspectRatio)
.setTargetRotation(rotation)
.build()
cameraProvider.unbindAll()
try {
camera = cameraProvider.bindToLifecycle(
this as LifecycleOwner, cameraSelector, preview, imageCapture, imageAnalyzer
)
} catch (exc: Exception) {
Log.e(TAG, "Use case binding failed", exc)
}
}, mainExecutor)
}
As I mentionned, zoom is working but then in onCaptureSucess, ImageProxy is not zoomed.
override fun onCaptureSuccess(image: ImageProxy) {
image.use { image ->
savedBitmap = image.imageProxyToBitmap()
///...
}
}
Here is the extension function to retrieve bitmap from imageProxy :
fun ImageProxy.imageProxyToBitmap(): Bitmap {
val buffer = this.planes[0].buffer
buffer.rewind()
val bytes = ByteArray(buffer.capacity())
buffer.get(bytes)
val bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.size)
val matrix = Matrix()
matrix.postRotate(90f)
return Bitmap.createBitmap(bitmap, 0, 0,bitmap.width,bitmap.height, matrix, true)
}
Here are my dependencies :
// CameraX core library
def camerax_version = "1.0.0-beta02"
implementation "androidx.camera:camera-core:$camerax_version"
// CameraX Camera2 extensions
implementation "androidx.camera:camera-camera2:$camerax_version"
// CameraX Lifecycle library
implementation "androidx.camera:camera-lifecycle:$camerax_version"
// CameraX View class
implementation "androidx.camera:camera-view:1.0.0-alpha09"
Thank you for your help 🙏
not sure why this happens but we are unable to reproduce the issue.
In my test, ImageCapture always captures the image with zoom applied.
Currently I suspect this could a device issue. It could be helpful if you can provide the device name. It will also be helpful if you can verify it on other devices.

CameraX preview not showing anything when front camera is selected

I am trying to use CameraX library to capture image using front camera. But when the camera is launched, preview is showing nothing but black screen. Here is the configurations:
val previewConfig = PreviewConfig.Builder().apply {
setTargetResolution(Size(640, 480))
setLensFacing(CameraX.LensFacing.FRONT)
}.build()
preview = Preview(previewConfig)
preview.setOnPreviewOutputUpdateListener {
val parent = viewFinder.parent as ViewGroup
parent.removeView(viewFinder)
parent.addView(viewFinder, 0)
viewFinder.surfaceTexture = it.surfaceTexture
updateTransform()
}
val imageCaptureConfig = ImageCaptureConfig.Builder()
.apply {
setCaptureMode(ImageCapture.CaptureMode.MIN_LATENCY)
}.build()
imageCapture = ImageCapture(imageCaptureConfig)
val analyzerConfig = ImageAnalysisConfig.Builder().apply {
setImageReaderMode(
ImageAnalysis.ImageReaderMode.ACQUIRE_LATEST_IMAGE)
}.build()
analyzerUseCase = ImageAnalysis(analyzerConfig).apply {
setAnalyzer(executor, LuminosityAnalyzer())
}
CameraX.bindToLifecycle(this, preview, imageCapture)
But it's working well with setLensFacing(CameraX.LensFacing.BACK). Also if i remove ImageCapture object from bindToLifeCyle method, i can see a preview with the front facing camera.
CameraX.bindToLifecycle(this, preview, /*imageCapture*/)
Please advise.
You also need to set setLensFacing to front for imageCaptureConfig.
I was also facing the same issue , but your question solved my issue :p

App crashed with "Fail to find supported surface info - CameraId:null" CameraX

I am using new CameraX API (alpha 5).
On devices like: LGE, Samsung, Motorola, OPPO crash with message:
Fail to find supported surface info - CameraId:null
occur.
This crashes are rare, but unacceptable.
Error occur in method:
private fun initCamera(cameraMode: CameraMode) {
val lensFacing = if (cameraMode == CameraMode.DEFAULT) CameraX.LensFacing.BACK else CameraX.LensFacing.FRONT
val metrics = DisplayMetrics().also { textureView?.display?.getRealMetrics(it) }
val resolution = Size(metrics.widthPixels, metrics.heightPixels)
val previewConfig = PreviewConfig.Builder()
.setTargetResolution(resolution)
.setLensFacing(lensFacing)
.build()
preview?.let { CameraX.unbind(preview) }
preview = Preview(previewConfig)
preview?.setOnPreviewOutputUpdateListener { previewOutput ->
val parent = textureView?.parent as ViewGroup
parent.removeView(textureView)
parent.addView(textureView, 0)
textureView?.surfaceTexture = previewOutput.surfaceTexture
}
CameraX.bindToLifecycle(this, preview)
}
in line:
preview = Preview(previewConfig)
It is looks like that it cannot create preview for this devices because cannot get camera.
Is anyone know the possible workaround for this problem?
P.S:
I checked. This devices (device models in which crash occur) has both cameras (front, back).

Categories

Resources