I would like to change the exposure correction value of the camera using CameraControl of AndroidX.

Asked 2 years ago, Updated 2 years ago, 70 views

We are currently creating a program to launch the camera on Android devices using the following programs:

package com.example.mycameraxapp;

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.Manifest
import android.content.pm.PackageManager
import android.net.Uri
import android.util.Log
import android.widget.Toast
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import java.util.current.Executors
import androidx.camera.core.*
import androidx.camera.lifecycle.ProcessCameraProvider
import kotlinx.android.synthetic.main.activity_main.*
import java.io.File
import java.nio.ByteBuffer
import java.text.SimpleDateFormat
import java.util.*
import java.util.current.ExecutorService
import androidx.camera.core.CameraControl

typealias LumaListener=(luma:Double) - > Unit

classMainActivity:AppCompatActivity(){
    private var imageCapture: ImageCapture?= null

    private lateinit var outputDirectory:File
    private lateinit varcameraExecutor:ExecutorService

    override fun onCreate (savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        // Request camera permissions
        if(allPermissionsGranted()) {
            startCamera()
        } else{
            ActivityCompat.requestPermissions(
                this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS)
        }

        // Setup the listener for take photo button
        camera_capture_button.setOnClickListener {takePhoto()}

        outputDirectory=getOutputDirectory()

        cameraExecutor=Executors.newSingleThreadExecutor()
    }

    private funtakePhoto(){}

    private fun startCamera(){
        valcameraProviderFuture=ProcessCameraProvider.getInstance(this)
        cameraProviderFuture.addListener(Runable{
            // Used to bind the life cycle of camera to the life cycle owner
            valcameraProvider: ProcessCameraProvider=cameraProviderFuture.get()
            // Preview
            val preview=Preview.Builder()
                .build()
                .also{
                    it.setSurfaceProvider (viewFinder.surfaceProvider)
                }
            // Select back camera as a default
            valcameraSelector=CameraSelector.DEFAULT_BACK_CAMERA
            try{
                // Unbind uses before rebinding
                cameraProvider.unbindAll()
                // Bind use cases to camera
                cameraProvider.bindToLifecycle(
                    this, cameraSelector, preview)
            } catch(exc:Exception){
                Log.e(TAG, "Use case binding failed", exc)
            }
            
            // exposure correction value setting
            varcameraexpose=Camera.CameraControl()
                cameraexpose.setExposeCompensationIndex(30)
            
        }, ContextCompat.getMainExecutor(this))
    }

    private fun allPermissionsGranted() = REQUIRED_PERMISSIONS.all {
        ContextCompat.checkSelfPermission(
            baseContext, it) == PackageManager.PERMISSION_GRANTED
    }

    private fungetOutputDirectory():File{
        valmediaDir=externalMediaDirs.firstOrNull()?let{
            File(it,resources.getString(R.string.app_name))).apply {mkdirs()}
        return if (mediaDir!=null&mediaDir.exists())
            mediaDir else filesDir
    }

    override fun onDestroy() {
        super.onDestroy()
        cameraExecutor.shutdown()
    }

    US>companion object {
        private constval TAG = "CameraXBasic"
        private constval FILENAME_FORMAT="yyyy-MM-dd-HH-mm-ss-SSS"
        private constant REQUEST_CODE_PERMISSIONS = 10
        private value REQUIRED_PERMISSIONS=arrayOf(Manifest.permission.CAMERA)
    }

    override fun onRequestPermissionsResult(
        requestCode:Int,permissions:Array<String>,grantResults:
        IntArray) {
        super.onRequestPermissionsResult(requestCode,permissions,grantResults)
        if(requestCode==REQUEST_CODE_PERMISSIONS) {//Verify that the request code is correct
            if(allPermissionsGranted()) {//Launch Camera if you have access to the Camera
                startCamera()
            } Else {// If you don't have permission, present toast to notify you that you don't have permission
                Toast.makeText(this,
                    "Permissions not granted by the user.",
                    Toast.LENGTH_SHORT).show()
                finish()
            }
        }
    }
}

Now that I can start the camera, I'm trying to change the exposure correction value on the preview screen, so I'm making it based on the official documentation, but I don't know how to implement the official content.

https://developer.android.com/reference/kotlin/androidx/camera/core/CameraControl?hl=ja#setExposureCompensationIndex(kotlin.Int)

I am trying to change the value using the setExposeCompensationIndex on this site.
As mentioned above, we have imported the "CameraControl" interface with setExposeCompensationIndex as a method.

import androidx.camera.core.CameraControl

"Also, I wrote the following to use the method, but the CameraControl part is in the red with ""Rename Reference"" and it doesn't work."

//Set exposure correction value
varcameraexpose=Camera.CameraControl()
    cameraexpose.setExposeCompensationIndex(30)

Looking at the official documentation, I don't know how to implement it in detail.

java android kotlin

2022-09-30 14:14

1 Answers

When working with camera, you must work with the instance associated with the life cycle.

val camera=cameraProvider.bindToLifecycle(this,cameraSelector,preview)

If you want to configure exposure compensation, make sure it is supported first.

val exposureState=camera.cameraInfo.exposureState
if(exposeState.isExposeCompensationSupported){...}

If the terminal camera supports it, set the exposure compensation value.
For example, the exposure compensation is 30, but you must specify a range.
This may vary from terminal to terminal.

 if(exposeState.isExposeCompensationSupported){
    value range=exposeState.exposeCompensationRange

    if(range.contains(30)){
        camera.cameraControl.setExposeCompensationIndex(30)
    }
}

If the camerax version is 1.0 series, exposure correction can cause errors for the experimental API.


2022-09-30 14:14

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.