用 android camera2 实现拍照录像视频和开启闪光灯

上传者: ddffjjyy | 上传时间: 2024-07-12 14:31:39 | 文件大小: 11.34MB | 文件类型: RAR
在Android平台上,开发一款能够拍照、录像以及控制闪光灯的应用是一项常见的需求。`camera2` API是Android系统提供的一种高级相机接口,它为开发者提供了更精细的控制权,以实现复杂的相机功能。在这个名为"TestCamera"的Android Studio工程中,我们将深入探讨如何使用`camera2` API来实现这些功能。 我们需要在AndroidManifest.xml文件中添加必要的权限,以允许应用访问相机和录制视频: ```xml ``` 接下来,我们创建一个`CameraActivity`,在这个活动中初始化相机,并设置预览界面。这通常涉及到设置SurfaceView或TextureView作为相机的预览展示区: ```java private CameraManager cameraManager; private SurfaceView surfaceView; private TextureView textureView; // 可选,根据需求选择 @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_camera); surfaceView = findViewById(R.id.surface_view); textureView = findViewById(R.id.texture_view); // 如果使用TextureView cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); try { String cameraId = cameraManager.getCameraIdList()[0]; // 获取第一个摄像头 cameraManager.openCamera(cameraId, new CameraDevice.StateCallback() {/*...*/}, null); } catch (CameraAccessException e) { e.printStackTrace(); } } ``` 在`StateCallback`中,我们需要实现打开、关闭相机的逻辑,以及设置预览会话和捕获器: ```java public class CameraStateCallback extends CameraDevice.StateCallback { @Override public void onOpened(@NonNull CameraDevice camera) { camera.createPreviewSession(setupPreviewSession(camera)); } private CameraCaptureSession.Callback setupPreviewSession(CameraDevice camera) {/*...*/} } ``` 对于拍照功能,我们需要创建一个`CaptureRequest.Builder`,设置适当的参数,然后提交请求到预览会话: ```java private void takePicture() { final CaptureRequest.Builder captureBuilder = camera.createCaptureRequest(CameraDevice.TEMPLATE_STILL_CAPTURE); captureBuilder.addTarget(imageReader.getSurface()); // imageReader用于保存图片 CameraCaptureSession.CaptureCallback captureCallback = new CameraCaptureSession.CaptureCallback() {/*...*/}; camera.createCaptureSession(Arrays.asList(captureBuilder.addTarget(surfaceView.getHolder().getSurface())), captureCallback, null); captureBuilder.set(CaptureRequest.CONTROL_AE_MODE, CaptureRequest.CONTROL_AE_MODE_ON_AUTO_FLASH); // 开启闪光灯 camera.capture(captureBuilder.build(), captureCallback, null); } ``` 录像功能则需要用到`MediaRecorder`,配置并启动它来记录视频: ```java private void startRecording() { MediaRecorder mediaRecorder = new MediaRecorder(); mediaRecorder.setVideoSource(MediaRecorder.VideoSource.SURFACE); mediaRecorder.setOutputFormat(MediaRecorder.OutputFormat.MPEG_4); mediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()); mediaRecorder.setVideoSize(width, height); mediaRecorder.setVideoEncoder(MediaRecorder.VideoEncoder.H264); mediaRecorder.setAudioEncoder(MediaRecorder.AudioEncoder.AAC); mediaRecorder.setPreviewDisplay(surfaceView.getHolder().getSurface()); try { mediaRecorder.prepare(); mediaRecorder.start(); } catch (IOException e) { e.printStackTrace(); } } private File getOutputMediaFile(int type) {/*...*/} // 创建保存视频的文件 ``` 控制闪光灯的状态可以通过调用`CameraCharacteristics`的`FLASH_MODE`来实现: ```java CameraManager cameraManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE); try { String cameraId = cameraManager.getCameraIdList()[0]; CameraCharacteristics characteristics = cameraManager.getCameraCharacteristics(cameraId); StreamConfigurationMap map = characteristics.get(CameraCharacteristics.SCALER_STREAM_CONFIGURATION_MAP); Integer flashMode = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE); if (flashMode != null && flashMode == 1) { // 检查相机是否支持闪光灯 if (isFlashOn) { cameraManager.setTorchMode(cameraId, false); // 关闭闪光灯 } else { cameraManager.setTorchMode(cameraId, true); // 打开闪光灯 } } } catch (CameraAccessException e) { e.printStackTrace(); } ``` 在实际应用中,还需要处理各种异常情况,比如权限问题、设备不支持等问题。此外,为了提供良好的用户体验,需要实现UI交互,如按钮点击事件,以触发拍照、录像和切换闪光灯操作。通过以上步骤,我们可以使用Android的`camera2` API创建一个功能完善的拍照、录像和控制闪光灯的应用。

文件下载

资源详情

[{"title":"( 555 个子文件 11.34MB ) 用 android camera2 实现拍照录像视频和开启闪光灯","children":[{"title":"07xDUCCeuPeq6CZtqQoCQYvxPpU= <span style='color:#111;'> 40.86KB </span>","children":null,"spread":false},{"title":"1DlCO2WqdIZnt8ffV3vsoYYPzZ8= <span style='color:#111;'> 10.00KB </span>","children":null,"spread":false},{"title":"2D9GBtIQXci0UuWWIthzeczi89A= <span style='color:#111;'> 34.35KB </span>","children":null,"spread":false},{"title":"34f2GJE8xjiteyNB4ZHY2ae5sk0= <span style='color:#111;'> 21.15KB </span>","children":null,"spread":false},{"title":"43EgLvqngGj1o0Y_Bd+NaEw1XXg= <span style='color:#111;'> 171.85KB </span>","children":null,"spread":false},{"title":"9YHus_wdwuaEd4yUDGWoo_lNWqY= <span style='color:#111;'> 15.11KB </span>","children":null,"spread":false},{"title":"a4Y7BXlThxbMDzFOoP9axwv9BRU= <span style='color:#111;'> 665.14KB </span>","children":null,"spread":false},{"title":"a86bLnuxjwweLFDAyX5LokbWgcs= <span style='color:#111;'> 225.67KB </span>","children":null,"spread":false},{"title":"androidResources <span style='color:#111;'> 41.48KB </span>","children":null,"spread":false},{"title":"resources-debug.ap_ <span style='color:#111;'> 1.26MB </span>","children":null,"spread":false},{"title":"app-debug.apk <span style='color:#111;'> 5.79MB </span>","children":null,"spread":false},{"title":"b8kIETrd8XWtuR1kcb5B_qM2Ymk= <span style='color:#111;'> 21.18KB </span>","children":null,"spread":false},{"title":"gradlew.bat <span style='color:#111;'> 2.70KB </span>","children":null,"spread":false},{"title":"executionHistory.bin <span style='color:#111;'> 1.86MB </span>","children":null,"spread":false},{"title":"jarAnalysis.bin <span style='color:#111;'> 1.66MB </span>","children":null,"spread":false},{"title":"classAnalysis.bin <span style='color:#111;'> 717.86KB </span>","children":null,"spread":false},{"title":"outputFiles.bin <span style='color:#111;'> 96.03KB </span>","children":null,"spread":false},{"title":"fileHashes.bin <span style='color:#111;'> 78.73KB </span>","children":null,"spread":false},{"title":"taskHistory.bin <span style='color:#111;'> 31.08KB </span>","children":null,"spread":false},{"title":"sha1-checksums.bin <span style='color:#111;'> 31.04KB </span>","children":null,"spread":false},{"title":"resourceHashesCache.bin <span style='color:#111;'> 19.49KB </span>","children":null,"spread":false},{"title":"graph.bin <span style='color:#111;'> 235B </span>","children":null,"spread":false},{"title":"graph.bin <span style='color:#111;'> 235B </span>","children":null,"spread":false},{"title":"graph.bin <span style='color:#111;'> 235B </span>","children":null,"spread":false},{"title":"graph.bin <span style='color:#111;'> 235B </span>","children":null,"spread":false},{"title":"graph.bin <span style='color:#111;'> 235B </span>","children":null,"spread":false},{"title":"graph.bin <span style='color:#111;'> 235B </span>","children":null,"spread":false},{"title":"graph.bin <span style='color:#111;'> 235B </span>","children":null,"spread":false},{"title":"graph.bin <span style='color:#111;'> 235B </span>","children":null,"spread":false},{"title":"last-build.bin <span style='color:#111;'> 1B </span>","children":null,"spread":false},{"title":"Camera2Controller.class <span style='color:#111;'> 22.55KB </span>","children":null,"spread":false},{"title":"PermissionsRequester.class <span style='color:#111;'> 4.44KB </span>","children":null,"spread":false},{"title":"RecordVideoFragment.class <span style='color:#111;'> 3.55KB </span>","children":null,"spread":false},{"title":"MainActivity.class <span style='color:#111;'> 3.15KB </span>","children":null,"spread":false},{"title":"TakePicFragment.class <span style='color:#111;'> 3.04KB </span>","children":null,"spread":false},{"title":"Camera2Controller$4.class <span style='color:#111;'> 2.93KB </span>","children":null,"spread":false},{"title":"Camera2Controller$5.class <span style='color:#111;'> 2.65KB </span>","children":null,"spread":false},{"title":"Camera2Activity.class <span style='color:#111;'> 2.63KB </span>","children":null,"spread":false},{"title":"Camera2Controller$3.class <span style='color:#111;'> 1.83KB </span>","children":null,"spread":false},{"title":"PermissionsRequester$2.class <span style='color:#111;'> 1.82KB </span>","children":null,"spread":false},{"title":"Camera2Controller$ImageSaver.class <span style='color:#111;'> 1.79KB </span>","children":null,"spread":false},{"title":"AutoFitTextureView.class <span style='color:#111;'> 1.64KB </span>","children":null,"spread":false},{"title":"RecordVideoFragment$1.class <span style='color:#111;'> 1.54KB </span>","children":null,"spread":false},{"title":"Camera2Controller$1.class <span style='color:#111;'> 1.54KB </span>","children":null,"spread":false},{"title":"Camera2Controller$2.class <span style='color:#111;'> 1.44KB </span>","children":null,"spread":false},{"title":"MainActivity$2.class <span style='color:#111;'> 1.35KB </span>","children":null,"spread":false},{"title":"MainActivity$1.class <span style='color:#111;'> 1.35KB </span>","children":null,"spread":false},{"title":"Camera2Controller$8.class <span style='color:#111;'> 1.28KB </span>","children":null,"spread":false},{"title":"PermissionsRequester$1.class <span style='color:#111;'> 1.24KB </span>","children":null,"spread":false},{"title":"Camera2Controller$6.class <span style='color:#111;'> 1.23KB </span>","children":null,"spread":false},{"title":"Camera2Controller$1$1.class <span style='color:#111;'> 1023B </span>","children":null,"spread":false},{"title":"Camera2Controller$CompareSizesByArea.class <span style='color:#111;'> 1000B </span>","children":null,"spread":false},{"title":"TakePicFragment$1.class <span style='color:#111;'> 985B </span>","children":null,"spread":false},{"title":"Camera2Controller$7.class <span style='color:#111;'> 978B </span>","children":null,"spread":false},{"title":"MainActivity$3.class <span style='color:#111;'> 949B </span>","children":null,"spread":false},{"title":"BuildConfig.class <span style='color:#111;'> 692B </span>","children":null,"spread":false},{"title":"Camera2Controller$ClassHolder.class <span style='color:#111;'> 689B </span>","children":null,"spread":false},{"title":"PermissionsRequester$IPermissionsResult.class <span style='color:#111;'> 290B </span>","children":null,"spread":false},{"title":"classes.dex <span style='color:#111;'> 4.05MB </span>","children":null,"spread":false},{"title":"classes.dex <span style='color:#111;'> 441.95KB </span>","children":null,"spread":false},{"title":"classes.dex <span style='color:#111;'> 36.07KB </span>","children":null,"spread":false},{"title":"Camera2Controller.dex <span style='color:#111;'> 17.58KB </span>","children":null,"spread":false},{"title":"PermissionsRequester.dex <span style='color:#111;'> 3.65KB </span>","children":null,"spread":false},{"title":"RecordVideoFragment.dex <span style='color:#111;'> 2.83KB </span>","children":null,"spread":false},{"title":"MainActivity.dex <span style='color:#111;'> 2.73KB </span>","children":null,"spread":false},{"title":"TakePicFragment.dex <span style='color:#111;'> 2.54KB </span>","children":null,"spread":false},{"title":"Camera2Controller$4.dex <span style='color:#111;'> 2.34KB </span>","children":null,"spread":false},{"title":"Camera2Controller$5.dex <span style='color:#111;'> 2.28KB </span>","children":null,"spread":false},{"title":"Camera2Activity.dex <span style='color:#111;'> 2.17KB </span>","children":null,"spread":false},{"title":"Camera2Controller$ImageSaver.dex <span style='color:#111;'> 1.96KB </span>","children":null,"spread":false},{"title":"PermissionsRequester$2.dex <span style='color:#111;'> 1.86KB </span>","children":null,"spread":false},{"title":"Camera2Controller$3.dex <span style='color:#111;'> 1.77KB </span>","children":null,"spread":false},{"title":"MainActivity$2.dex <span style='color:#111;'> 1.63KB </span>","children":null,"spread":false},{"title":"MainActivity$1.dex <span style='color:#111;'> 1.63KB </span>","children":null,"spread":false},{"title":"AutoFitTextureView.dex <span style='color:#111;'> 1.59KB </span>","children":null,"spread":false},{"title":"Camera2Controller$1.dex <span style='color:#111;'> 1.58KB </span>","children":null,"spread":false},{"title":"RecordVideoFragment$1.dex <span style='color:#111;'> 1.58KB </span>","children":null,"spread":false},{"title":"Camera2Controller$2.dex <span style='color:#111;'> 1.52KB </span>","children":null,"spread":false},{"title":"Camera2Controller$8.dex <span style='color:#111;'> 1.50KB </span>","children":null,"spread":false},{"title":"PermissionsRequester$1.dex <span style='color:#111;'> 1.42KB </span>","children":null,"spread":false},{"title":"Camera2Controller$CompareSizesByArea.dex <span style='color:#111;'> 1.38KB </span>","children":null,"spread":false},{"title":"Camera2Controller$6.dex <span style='color:#111;'> 1.35KB </span>","children":null,"spread":false},{"title":"Camera2Controller$7.dex <span style='color:#111;'> 1.33KB </span>","children":null,"spread":false},{"title":"Camera2Controller$1$1.dex <span style='color:#111;'> 1.25KB </span>","children":null,"spread":false},{"title":"TakePicFragment$1.dex <span style='color:#111;'> 1.17KB </span>","children":null,"spread":false},{"title":"MainActivity$3.dex <span style='color:#111;'> 1.12KB </span>","children":null,"spread":false},{"title":"Camera2Controller$ClassHolder.dex <span style='color:#111;'> 1.02KB </span>","children":null,"spread":false},{"title":"BuildConfig.dex <span style='color:#111;'> 964B </span>","children":null,"spread":false},{"title":"PermissionsRequester$IPermissionsResult.dex <span style='color:#111;'> 848B </span>","children":null,"spread":false},{"title":"dNOoOrpcycnVDTf1bebVUStkDaM= <span style='color:#111;'> 54.00KB </span>","children":null,"spread":false},{"title":"values_values.arsc.flat <span style='color:#111;'> 546.81KB </span>","children":null,"spread":false},{"title":"mipmap-xxxhdpi_ic_launcher_foreground.png.flat <span style='color:#111;'> 89.31KB </span>","children":null,"spread":false},{"title":"drawable_logo.jpg.flat <span style='color:#111;'> 77.66KB </span>","children":null,"spread":false},{"title":"mipmap-xxhdpi_ic_launcher_foreground.png.flat <span style='color:#111;'> 53.12KB </span>","children":null,"spread":false},{"title":"mipmap-xxxhdpi_ic_launcher_round.png.flat <span style='color:#111;'> 36.14KB </span>","children":null,"spread":false},{"title":"mipmap-xxxhdpi_ic_launcher.png.flat <span style='color:#111;'> 27.75KB </span>","children":null,"spread":false},{"title":"values-v21_values-v21.arsc.flat <span style='color:#111;'> 26.92KB </span>","children":null,"spread":false},{"title":"mipmap-xhdpi_ic_launcher_foreground.png.flat <span style='color:#111;'> 24.80KB </span>","children":null,"spread":false},{"title":"mipmap-xxhdpi_ic_launcher_round.png.flat <span style='color:#111;'> 22.33KB </span>","children":null,"spread":false},{"title":"mipmap-xxhdpi_ic_launcher.png.flat <span style='color:#111;'> 16.98KB </span>","children":null,"spread":false},{"title":"......","children":null,"spread":false},{"title":"<span style='color:steelblue;'>文件过多,未全部展示</span>","children":null,"spread":false}],"spread":true}]

评论信息

免责申明

【只为小站】的资源来自网友分享,仅供学习研究,请务必在下载后24小时内给予删除,不得用于其他任何用途,否则后果自负。基于互联网的特殊性,【只为小站】 无法对用户传输的作品、信息、内容的权属或合法性、合规性、真实性、科学性、完整权、有效性等进行实质审查;无论 【只为小站】 经营者是否已进行审查,用户均应自行承担因其传输的作品、信息、内容而可能或已经产生的侵权或权属纠纷等法律责任。
本站所有资源不代表本站的观点或立场,基于网友分享,根据中国法律《信息网络传播权保护条例》第二十二条之规定,若资源存在侵权或相关问题请联系本站客服人员,zhiweidada#qq.com,请把#换成@,本站将给予最大的支持与配合,做到及时反馈和处理。关于更多版权及免责申明参见 版权及免责申明