Follow my public account “ananzhuo” to learn more knowledge
This article is a continuation of the previous article about taking photos portal: Whoosh
The engineering code used is also the same as in the previous article
An overview of
Concepts and simple use suggestions skip to the previous article to see juejin.cn/post/696831…
Use (simply shoot a video and show it)
Open the camera and set related parameters
private fun openCamera(a) {
cameraProviderFuture = ProcessCameraProvider.getInstance(this)// Get the provider instance
cameraProviderFuture.addListener({
cameraProvider = cameraProviderFuture.get()
bindPreview()
}, ContextCompat.getMainExecutor(this))}@SuppressLint("RestrictedApi")
private fun bindPreview(a) {
val preview: Preview = Preview.Builder()
.build()
val cameraSelector: CameraSelector = CameraSelector.Builder()
.requireLensFacing(CameraSelector.LENS_FACING_BACK)
.build()
preview.setSurfaceProvider(previdew.surfaceProvider)
videoCapture = VideoCapture.Builder()
.setBitRate(1024 * 1024)
.setVideoFrameRate(36)
.setTargetRotation(previdew.display.rotation)
.build()
varcamera = cameraProvider? .bindToLifecycle(this,
cameraSelector,
videoCapture,
preview
)
}
Copy the code
Video shooting code
@SuppressLint("RestrictedApi"."MissingPermission")
fun takeVideo(a) {
val faceVideoFile = getExternalFilesDir("videoFile")? .also {if (it.exists()) {
it.mkdirs()
}
}
val destVideoFile = File(faceVideoFile, "${System.currentTimeMillis()}.mp4")
val outFileOption = VideoCapture.OutputFileOptions.Builder(destVideoFile).build()
videoCapture.startRecording(
outFileOption,
Executors.newSingleThreadExecutor(),
object : VideoCapture.OnVideoSavedCallback {
override fun onVideoSaved(outputFileResults: VideoCapture.OutputFileResults) {
lifecycleScope.launch(Dispatchers.Main) {
Toast.makeText(this@TakeVideoActivity."Shot successfully.", Toast.LENGTH_LONG).show()
startActivity(
Intent(
this@TakeVideoActivity,
ShowVideoActivity::class.java
).apply {
putExtra("path", destVideoFile.absolutePath)
})
}
}
override fun onError(videoCaptureError: Int, message: String, cause: Throwable?). {
lifecycleScope.launch(Dispatchers.Main) {
Toast.makeText(this@TakeVideoActivity."Filming failed", Toast.LENGTH_LONG).show()
}
}
})
}
Copy the code
The end
videoCapture.stopRecording()
Copy the code
Stops the camera when the activity is destroyed
@SuppressLint("RestrictedApi")
override fun onDestroy(a) {
super.onDestroy() cameraProvider? .shutdown()// Mandatory step call
}
Copy the code
The final result
Introduction of important APIS
When we build VideoCapture there are some important parameters that determine how we end up generating the video
SetBitRate Sets the video bit rate
.setBitRate(1024 * 1024)
Copy the code
The method argument is in bits, representing the size of the video we generated in one second of bits
My personal test results show that CameraX will try to follow the bitrate we set, and if the Settings are not reasonable CameraX will automatically adjust to ensure the quality of the resulting video
SetVideoFrameRate Sets the frame rate of the video
.setVideoFrameRate(100)
Copy the code
This is equivalent to setting the frame rate of the video, but in milliseconds. For example, in this example, we set 100ms, which means 10 video frames in 1s
SetTargetRotation Sets the rotation of the camera
.setTargetRotation(previdew.display.rotation)
Copy the code
There’s nothing to say about this API, you’re all familiar with it
SetAudioBitRate Sets the audio bit rate
.setAudioBitRate(1024)
Copy the code
Sets the bit rate of audio, similar to setBitRate
SetTargetResolution sets the resolution of the generated video
.setTargetResolution(Size(300.200))
Copy the code
Set the ratio of pixels to width for the resulting video. This is useful when we want to compress the video to a maximum size, where the compression benefit is much greater than controlling the frame rate.
Switch the camera method
val cameraSelector: CameraSelector = CamerasElector.builder ().requirelensfacing (CamerasElector.lens_facing_back) RequireLensFacing (Cameraselector.lens_facing_front) requireLensFacing(Cameraselector.lens_facing_front)Copy the code
One of the worst things about Android’s front-facing camera is that the images taken with the front-facing camera are left to right, and with CameraX we can’t customize the SurfaceView so we can’t manipulate the video orientation