Video Capture + CameraController

353 views
Skip to first unread message

aaron acosta

unread,
Jun 26, 2023, 12:12:10 AM6/26/23
to Android CameraX Discussion Group
Hi, I followed Donovan's tutorial using the CameraController pathway because it already has zoom, focus, WYSIWYG..etc, I am now trying to integrate VideoCapture (I believe it's still experimental?) .I only found examples for video on "Migrating camera1 to camerax' guide, I am getting errors about OnVideoSavedCallback being unresolved and others..  I would like to know If my code is outdated? I can't get it to work. Thanks!


class MainActivity : AppCompatActivity() {
    private lateinit var viewBinding: ActivityMainBinding

    //If using CameraController
    private lateinit var cameraController: LifecycleCameraController





    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        viewBinding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(viewBinding.root)

        if(!hasPermissions(baseContext)) {
            //Request camera-related permissions
            activityResultLauncher.launch(REQUIRED_PERMISSIONS)
        }else{
            startCamera()
        }
        // Set up the listeners for take photo and video capture buttons
        viewBinding.imageCaptureButton.setOnClickListener { takePhoto()}
        viewBinding.videoCaptureButton.setOnClickListener { startStopVideo()}


    }

    private fun startCamera(){
        val previewView: PreviewView = viewBinding.viewFinder

        cameraController = LifecycleCameraController(baseContext)

        cameraController.setEnabledUseCases(VIDEO_CAPTURE)
        cameraController.bindToLifecycle(this)
        cameraController.cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA


        previewView.controller = cameraController

    }



     //Define a VideoSaveCallback class for handling success and error states.
    class VideoSaveCallback : OnVideoSavedCallback {
        override fun onVideoSaved(outputFileResults: OutputFileResults) {
            val msg = "Video capture succeeded: ${outputFileResults.savedUri}"
            Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
            Log.d(TAG, msg)
        }

        override fun onError(videoCaptureError: Int, message: String,
                             cause: Throwable?) {
            Log.d(TAG, "error saving video: $message", cause)
        }
    }

    private fun startStopVideo() {
        if (cameraController.isRecording()) {
            // Stop the current recording session.
            cameraController.stopRecording()
            return
        }

        // Define the File options for saving the video.
        val name = SimpleDateFormat(FILENAME_FORMAT, Locale.US)
            .format(System.currentTimeMillis())

        val outputFileOptions = OutputFileOptions
            .Builder(File(this.filesDir, name))
            .build()

        // Call startRecording on the CameraController.
        cameraController.startRecording(
            outputFileOptions,AudioConfig.AUDIO_DISABLED,
            ContextCompat.getMainExecutor(this),
            VideoSaveCallback()
        )
    }



    private fun takePhoto(){
        // Create time stamped name and MediaStore entry.
        val name = SimpleDateFormat(FILENAME_FORMAT, Locale.US)
            .format(System.currentTimeMillis())
        val contentValues = ContentValues().apply {
            put(MediaStore.MediaColumns.DISPLAY_NAME, name)
            put(MediaStore.MediaColumns.MIME_TYPE, "image/jpeg")
            if(Build.VERSION.SDK_INT > Build.VERSION_CODES.P){
                put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/BAM2-Image")
            }
        }

        //Create output options object which contains file + metadata
        val outputOptions = ImageCapture.OutputFileOptions
            .Builder(contentResolver,
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                contentValues)
            .build()

        //Set up image capture listener, which is triggered after photo has
        //been taken

        cameraController.takePicture(
            outputOptions,
            ContextCompat.getMainExecutor(this),
            object : ImageCapture.OnImageSavedCallback{
                override fun onError(exc: ImageCaptureException) {
                    Log.e(TAG, "Photo capture failed: ${exc.message}", exc)
                }

                override fun onImageSaved(output: ImageCapture.OutputFileResults) {
                    val msg = "Photo capture succeeded: ${output.savedUri}"
                    Toast.makeText(baseContext, msg, Toast.LENGTH_SHORT).show()
                    Log.d(TAG,msg)

                }
            }
        )
    }

Xi Zhang (张熹)

unread,
Jun 26, 2023, 1:49:28 PM6/26/23
to aaron acosta, Android CameraX Discussion Group
Hi Aaron:

Which version of CameraX are you using? In the latest 1.3.0-beta01, CameraController supports video capture. You can check out CameraX test app for code sample: https://github.com/androidx/androidx/blob/c3618a178cb09b43ed803bb98f2b2e5903e86e80/camera/integration-tests/viewtestapp/src/main/java/androidx/camera/integration/view/CameraControllerFragment.java#L283

Thanks,

Xi

--
You received this message because you are subscribed to the Google Groups "Android CameraX Discussion Group" group.
To unsubscribe from this group and stop receiving emails from it, send an email to camerax-develop...@android.com.
To view this discussion on the web visit https://groups.google.com/a/android.com/d/msgid/camerax-developers/932da855-26fc-4cb3-af3e-d342baa36a36n%40android.com.

aaron acosta

unread,
Jun 26, 2023, 10:32:21 PM6/26/23
to Android CameraX Discussion Group, xi...@google.com, Android CameraX Discussion Group, aaron acosta
Thanks, works like a charm! 

How can we switch between aspect ratios with CameraController? 16/9 or 4/3

Thanks!

Xi Zhang (张熹)

unread,
Jun 27, 2023, 11:09:56 AM6/27/23
to aaron acosta, Android CameraX Discussion Group
For the camera controller, the result is WYSIWYG. If you set PreviewView's aspect ratio to 16/9 or 4/3, I believe the output will be adjusted accordingly. It supports arbitrary aspect ratio too. Please let me know if that's not the case.
Reply all
Reply to author
Forward
0 new messages