如何在 Android 平台上编写一个实现了摄像头拍照和录像功能,并能切换前后摄像头的代码示例(Demo)?
时间: 2024-12-22 10:19:13 浏览: 9
在Android平台上编写一个简单的相机应用,可以使用`CameraX`库,它提供了一套现代、简单易用的API来进行设备访问。下面是一个简化的代码示例,展示了如何打开摄像头,切换前后摄像头以及拍照录像的基本流程:
```java
import android.content.Context;
import android.graphics.ImageReader;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraCaptureSession;
import android.hardware.camera2.CaptureRequest;
import android.hardware.camera2.TotalCaptureResult;
import android.media.MediaRecorder;
import android.os.Bundle;
import android.util.Size;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.lifecycle.LifecycleOwner;
import com.google.android.gms.vision.Frame;
public class CameraActivity extends AppCompatActivity implements ImageReader.OnImageAvailableListener {
private static final int REQUEST_CAMERA_PERMISSION = 1;
private CameraManager cameraManager;
private CameraXPreview cameraxPreview;
private MediaRecorder mediaRecorder;
private String cameraId;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA}, REQUEST_CAMERA_PERMISSION);
setupCamera();
}
private void setupCamera() {
try {
cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
cameraId = cameraManager.getCameraIdList()[0]; // 获取可用的第一个摄像头
cameraManager.openCamera(cameraId, new CameraDevice.StateCallback() {
@Override
public void onOpened(@NonNull CameraDevice camera) {
createCameraXPreview(camera);
}
// 更多回调...
@Override
public void onDisconnected(@NonNull CameraDevice camera) {
camera.close();
}
// 更多回调...
}, null);
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
private void createCameraXPreview(CameraDevice camera) {
// 创建CameraX preview
cameraxPreview = new CameraXPreview(this, camera, Size.of(640, 480)); // 使用默认大小,可以根据需要调整
setContentView(cameraxPreview);
// 初始化媒体录音
initMediaRecorder();
}
private void initMediaRecorder() {
mediaRecorder = new MediaRecorder();
mediaRecorder.setAudioSource(MediaRecorder.AudioSource.MIC);
mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4);
mediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA);
mediaRecorder.setVideoSize(640, 480); // 视频尺寸与preview一致
try {
mediaRecorder.prepare(); // 设置好录制参数后可以立即开始录制
} catch (IllegalStateException | IOException e) {
e.printStackTrace();
}
}
@Override
public void onImageAvailable(ImageReader reader) {
// 在这里处理接收到的帧,例如通过Vision API进行图像识别
Frame frame = reader.acquireNextFrame();
if (frame != null) {
// 进行图像分析...
frame.close();
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case REQUEST_CAMERA_PERMISSION:
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
setupCamera();
} else {
Toast.makeText(this, "权限请求未通过", Toast.LENGTH_SHORT).show();
finish();
}
break;
default:
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
// 更多方法如切换前后摄像头、拍照、停止录像等需在此添加相应的逻辑...
阅读全文