Android studio 中调用摄像头录像

要在 Android Studio 中调用摄像头录像,需要使用 Android 的 Camera API 或 CameraX API(推荐)。以下是使用 CameraX API 实现摄像头录像的基本步骤:

使用 CameraX API 实现摄像头录像步骤:

  1. 添加依赖: 在 build.gradle 文件中添加 CameraX 的依赖:

    gradle
    dependencies { def camerax_version = "1.1.0-alpha10" implementation "androidx.camera:camera-core:$camerax_version" implementation "androidx.camera:camera-camera2:$camerax_version" implementation "androidx.camera:camera-lifecycle:$camerax_version" }
  2. 在 XML 布局中添加预览视图: 在你的布局文件中添加 PreviewView,用于显示摄像头预览:

    xml
    <androidx.camera.view.PreviewView android:id="@+id/previewView" android:layout_width="match_parent" android:layout_height="match_parent" />
  3. 设置 CameraX 用例: 在 Activity 或 Fragment 中配置和启动 CameraX 用例,包括预览和录像:

    java
    import androidx.camera.core.CameraSelector; import androidx.camera.core.Preview; import androidx.camera.core.VideoCapture; import androidx.camera.lifecycle.ProcessCameraProvider; import androidx.camera.view.PreviewView; import androidx.core.content.ContextCompat; import java.io.File; import java.text.SimpleDateFormat; import java.util.Locale; public class CameraActivity extends AppCompatActivity { private PreviewView previewView; private VideoCapture videoCapture; private File outputDirectory; private String currentVideoPath; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera); previewView = findViewById(R.id.previewView); outputDirectory = getOutputDirectory(); startCamera(); } private void startCamera() { ListenableFuture<ProcessCameraProvider> cameraProviderFuture = ProcessCameraProvider.getInstance(this); cameraProviderFuture.addListener(() -> { try { ProcessCameraProvider cameraProvider = cameraProviderFuture.get(); Preview preview = new Preview.Builder().build(); preview.setSurfaceProvider(previewView.getSurfaceProvider()); videoCapture = new VideoCapture.Builder() .setTargetRotation(previewView.getDisplay().getRotation()) .build(); CameraSelector cameraSelector = CameraSelector.DEFAULT_BACK_CAMERA; cameraProvider.unbindAll(); cameraProvider.bindToLifecycle(this, cameraSelector, preview, videoCapture); } catch (Exception e) { e.printStackTrace(); } }, ContextCompat.getMainExecutor(this)); } private File getOutputDirectory() { File mediaDir = new File(getExternalFilesDir(null), "Videos"); if (!mediaDir.exists()) { mediaDir.mkdirs(); } return mediaDir; } private File createFile() { String timestamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(System.currentTimeMillis()); String prepend = "VIDEO_" + timestamp + "_"; File videoFile = new File(outputDirectory, prepend + ".mp4"); currentVideoPath = videoFile.getAbsolutePath(); return videoFile; } private void startRecording() { File videoFile = createFile(); videoCapture.startRecording(videoFile, ContextCompat.getMainExecutor(this), new VideoCapture.OnVideoSavedCallback() { @Override public void onVideoSaved(@NonNull OutputFileResults outputFileResults) { String msg = "Video saved: " + currentVideoPath; Toast.makeText(CameraActivity.this, msg, Toast.LENGTH_SHORT).show(); } @Override public void onError(int videoCaptureError, @NonNull String message, @Nullable Throwable cause) { String msg = "Video capture failed: " + message; Toast.makeText(CameraActivity.this, msg, Toast.LENGTH_SHORT).show(); } }); } private void stopRecording() { videoCapture.stopRecording(); } // Add methods to handle recording start/stop based on user interaction }
  4. 录像控制

    • 使用 startRecording() 方法开始录像,并通过 stopRecording() 方法停止录像。
    • 可以根据需要添加用户交互控制,例如按钮点击开始和停止录像。

通过上述步骤,你可以在 Android 应用中使用 CameraX API 实现摄像头录像功能,并根据需求进行定制和优化。

关键字:

Android, CameraX API, 摄像头录像, PreviewView, VideoCapture, 录像功能