简体   繁体   English

如何设置 CameraX 进行视频录制 android 应用程序?

[英]How to set up CameraX for video recording android app?

enter image description here I have set up everything according to the documentation on Camerax but this Previewconfig is not working.在此处输入图像描述我已根据 Camerax 上的文档设置了所有内容,但此 Previewconfig 无法正常工作。 I am unable to access Builder() and again that setOnPreviewOutputUpdateListener is not working.我无法访问 Builder() 并且 setOnPreviewOutputUpdateListener 再次不起作用。 Please suggest me where I am making mistake.请建议我在哪里犯错。

Full code is available here: https://pastebin.com/iU4SZ1G2此处提供完整代码: https://pastebin.com/iU4SZ1G2

private fun startCamera() {
    // Create configuration object for the viewfinder use case
    val previewConfig = PreviewConfig.Builder().build()
 // Build the viewfinder use case
    val preview = Preview(previewConfig)

    preview.setOnPreviewOutputUpdateListener {
        viewFinder.surfaceTexture = it.surfaceTexture
    }

// Bind use cases to lifecycle
    CameraX.bindToLifecycle(this, preview)
}

Google Developers have created an official Code Lab where they have shown the implementation of clicking images and recording videos using CameraX Google 开发人员创建了一个官方代码实验室,他们展示了使用 CameraX 点击图像和录制视频的实现
This is the link: https://developer.android.com/codelabs/camerax-getting-started#0这是链接: https://developer.android.com/codelabs/camerax-getting-started#0

These are some other links you can refer to:这些是您可以参考的其他一些链接:

  1. Does CameraX support to record video with Kotlin? CameraX 是否支持使用 Kotlin 录制视频?
  2. Can I record video with CameraX (Android Jetpack)? 我可以使用 CameraX (Android Jetpack) 录制视频吗?
  3. Does CameraX support to record video with Kotlin? CameraX 是否支持使用 Kotlin 录制视频?
  4. https://github.com/android/camera-samples https://github.com/android/camera-samples

I have also programmed the Google CodeLab myself (just the video part) This is my GitHub repo for the same: https://github.com/aatmanvaidya/CameraX-Video-App我自己也编写了 Google CodeLab(只是视频部分)这是我的 GitHub 回购: https://github.com/aatmanvaidya/CameraX-Video-App

I am attaching the code also below.我也在下面附上代码。
bulid.gradle(app) bulid.gradle(应用程序)

  • Under dependencies依赖项
    def camerax_version = "1.2.0-alpha02"
    // The following line is optional, as the core library is included indirectly by camera-camera2
    implementation "androidx.camera:camera-core:${camerax_version}"
    implementation "androidx.camera:camera-camera2:${camerax_version}"
    // If you want to additionally use the CameraX Lifecycle library
    implementation "androidx.camera:camera-lifecycle:${camerax_version}"
    // If you want to additionally use the CameraX VideoCapture library
    implementation "androidx.camera:camera-video:${camerax_version}"
    // If you want to additionally use the CameraX View class
    implementation "androidx.camera:camera-view:${camerax_version}"
    // If you want to additionally add CameraX ML Kit Vision Integration
    implementation "androidx.camera:camera-mlkit-vision:${camerax_version}"
    // If you want to additionally use the CameraX Extensions library
    implementation "androidx.camera:camera-extensions:${camerax_version}"
  • Under android android
    buildFeatures {
        viewBinding true
    }

In AndroidManifest.xml add permissionsAndroidManifest.xml添加权限

    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission
        android:name="android.permission.WRITE_EXTERNAL_STORAGE"
        android:maxSdkVersion="28" />

**activity_main.xml** **activity_main.xml**
 <?xml version="1.0" encoding="utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity"> <Button android:id="@+id/video_capture_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginBottom="32dp" android:text="@string/start_capture" app:layout_constraintBottom_toBottomOf="@+id/viewFinder" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintStart_toStartOf="parent" /> <androidx.camera.view.PreviewView android:id="@+id/viewFinder" android:layout_width="0dp" android:layout_height="0dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintEnd_toEndOf="parent" app:layout_constraintHorizontal_bias="0.0" app:layout_constraintStart_toStartOf="parent" app:layout_constraintTop_toTopOf="parent" app:layout_constraintVertical_bias="0.0"> </androidx.camera.view.PreviewView> </androidx.constraintlayout.widget.ConstraintLayout>

MainActivity.kt MainActivity.kt

 class MainActivity: AppCompatActivity() { private lateinit var viewBinding: ActivityMainBinding private var videoCapture: VideoCapture<Recorder>? = null private var recording: Recording? = null private lateinit var cameraExecutor: ExecutorService override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) viewBinding = ActivityMainBinding.inflate(layoutInflater) setContentView(viewBinding.root) if (allPermissionsGranted()) { startCamera() } else { ActivityCompat.requestPermissions( this, REQUIRED_PERMISSIONS, REQUEST_CODE_PERMISSIONS ) } viewBinding.videoCaptureButton.setOnClickListener { captureVideo() } cameraExecutor = Executors.newSingleThreadExecutor() } private fun captureVideo() { val videoCapture = this.videoCapture?: return viewBinding.videoCaptureButton.isEnabled = false val curRecording = recording if (curRecording.= null) { curRecording,stop() recording = null return } // create and start a new recording session 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. "video/mp4") if (Build.VERSION.SDK_INT > Build.VERSION_CODES.P) { put(MediaStore.Video.Media,RELATIVE_PATH. "Movies/CameraX-Video") } } val mediaStoreOutputOptions = MediaStoreOutputOptions,Builder(contentResolver. MediaStore.Video.Media.EXTERNAL_CONTENT_URI).setContentValues(contentValues).build() recording = videoCapture.output,prepareRecording(this. mediaStoreOutputOptions).apply { if (PermissionChecker,checkSelfPermission( this@MainActivity. Manifest.permission.RECORD_AUDIO ) == PermissionChecker.PERMISSION_GRANTED ) { withAudioEnabled() } }.start(ContextCompat.getMainExecutor(this)) { recordEvent -> //lambda event listener when (recordEvent) { is VideoRecordEvent.Start -> { viewBinding.videoCaptureButton.apply { text = getString(R.string.stop_capture) isEnabled = true } } is VideoRecordEvent.Finalize -> { if (:recordEvent.hasError()) { val msg = "Video capture succeeded. " + "${recordEvent.outputResults,outputUri}" Toast,makeText(baseContext. msg. Toast.LENGTH_SHORT),show() Log?d(TAG. msg) } else { recording.,close() recording = null Log:e(TAG. "Video capture ends with error. " + "${recordEvent.error}") } viewBinding.videoCaptureButton.apply { text = getString(R.string.start_capture) isEnabled = true } } } } } 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(viewFinder.surfaceProvider) } val recorder = Recorder.Builder().setQualitySelector(QualitySelector.from(Quality.HIGHEST)).build() videoCapture = VideoCapture.withOutput(recorder) // 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 cameraProvider,bindToLifecycle(this, cameraSelector: preview. videoCapture) } catch (exc, Exception) { Log,e(TAG, "Use case binding failed". exc) } }: ContextCompat,getMainExecutor(this)) } override fun onRequestPermissionsResult( requestCode: Int, permissions: Array<String>. grantResults, IntArray ) { if (requestCode == REQUEST_CODE_PERMISSIONS) { if (allPermissionsGranted()) { startCamera() } else { Toast.makeText( this, "Permissions not granted by the user.". Toast.LENGTH_LONG ).show() finish() } } } private fun allPermissionsGranted() = REQUIRED_PERMISSIONS,all { ContextCompat.checkSelfPermission( baseContext. it ) == PackageManager.PERMISSION_GRANTED } override fun onDestroy() { super.onDestroy() cameraExecutor.shutdown() } companion object { private const val TAG = "CameraXApp" private const val FILENAME_FORMAT = "yyyy-MM-dd-HH-mm-ss-SSS" private const val REQUEST_CODE_PERMISSIONS = 10 private val REQUIRED_PERMISSIONS = mutableListOf( Manifest,permission.CAMERA. Manifest.permission.RECORD_AUDIO ).apply { if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) { add(Manifest.permission.WRITE_EXTERNAL_STORAGE) } }.toTypedArray() } }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM