Android USBCamera,UVCCamera开发通用库,我试用了普通的USB摄像头没问题

转自 https://blog.csdn.net/AndrExpert/article/details/78324181

Android直播开发之旅(10):AndroidUSBCamera,UVCCamera开发通用库

    AndroidUSBCamera基于[saki4510t/UVCCamera](https://github.com/saki4510t/UVCCamera)开发,该项目对USB Camera(UVC设备)的使用和视频数据采集进行了高度封装,能够帮助开发者通过几个简单的API实现USB Camera设备的检测、连接、预览和音视频数据采集,最重要的是手机无需root,只需支持otg功能即可驱动。主要功能包括:  

(1)支持USB Camera设备检测,画面实时预览;  
(2)支持本地录制mp4格式视频,支持实时获取音视频数据流;  
(3)支持jpg格式图片抓拍;  
(4)支持获取camera支持的分辨率,和分辨率切换;  
(5)支持屏蔽声音,重启Camera; 
(6)支持相机自动对焦;  

(7)支持调整对比度和亮度

(8) 支持480P、720P、1080P and higher


如何使用AndroidUSBCamera项目 


1. 添加依赖到本地工程
  
第一步 添加JitPack仓库到工程gradle  
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:

 


    
  1. allprojects {
  2. repositories {
  3. maven { url ‘http://raw.github.com/saki4510t/libcommon/master/repository/’ }
  4. maven { url ‘https://jitpack.io’ }
  5. }
  6. }


第二步 添加依赖到app Module的gradle   
Step 2. Add the dependency  

 

 

 


    
  1. dependencies {
  2. compile 'com.github.jiangdongguo:AndroidUSBCamera:1.3.8'
  3. }

 

 

 

 2. 初始化引擎,注册USB设备事件监听器  
  Init AndroidUSBCamera engine,register the USB device event listener  
  


    
  1. private USBCameraManager.OnMyDevConnectListener listener = new USBCameraManager.OnMyDevConnectListener() {
  2. // 插入USB设备
  3. @Override
  4. public void onAttachDev(UsbDevice device) {
  5. if(mUSBManager == null || mUSBManager.getUsbDeviceCount() == 0){
  6. showShortMsg("未检测到USB摄像头设备");
  7. return;
  8. }
  9. // 请求打开摄像头
  10. if(! isRequest){
  11. isRequest = true;
  12. if(mUSBManager != null){
  13. mUSBManager.requestPermission(0);
  14. }
  15. }
  16. }
  17. // 拔出USB设备
  18. @Override
  19. public void onDettachDev(UsbDevice device) {
  20. if(isRequest){
  21. // 关闭摄像头
  22. isRequest = false;
  23. mUSBManager.closeCamera();
  24. showShortMsg(device.getDeviceName()+"已拨出");
  25. }
  26. }
  27. // 连接USB设备成功
  28. @Override
  29. public void onConnectDev(UsbDevice device,boolean isConnected) {
  30. if(! isConnected) {
  31. showShortMsg("连接失败,请检查分辨率参数是否正确");
  32. isPreview = false;
  33. }else{
  34. isPreview = true;
  35. }
  36. }
  37. // 与USB设备断开连接
  38. @Override
  39. public void onDisConnectDev(UsbDevice device) {
  40. showShortMsg("连接失败");
  41. }
  42. };
  43. mUVCCameraView = (CameraViewInterface) mTextureView;
  44. mUVCCameraView.setCallback(new CameraViewInterface.Callback() {
  45. @Override
  46. public void onSurfaceCreated(CameraViewInterface view, Surface surface) {
  47. if(!isPreview && mUSBManager.isCameraOpened()) {
  48. mUSBManager.startPreview(mUVCCameraView, new AbstractUVCCameraHandler.OnPreViewResultListener() {
  49. @Override
  50. public void onPreviewResult(boolean result) {
  51. }
  52. });
  53. isPreview = true;
  54. }
  55. }
  56. @Override
  57. public void onSurfaceChanged(CameraViewInterface view, Surface surface, int width, int height) {
  58. }
  59. @Override
  60. public void onSurfaceDestroy(CameraViewInterface view, Surface surface) {
  61. if(isPreview && mUSBManager.isCameraOpened()) {
  62. mUSBManager.stopPreview();
  63. isPreview = false;
  64. }
  65. }
  66. });
  67. // 初始化引擎
  68. mUSBManager = USBCameraManager.getInstance();
  69. mUSBManager.initUSBMonitor(this,listener);
  70. mUSBManager.createUVCCamera(mUVCCameraView);


3. 注册USB设备广播事件监听器,开始Camera预览  
  Register the USB device broadcast event listener and start the Camera Preview

 

 

 


    
  1. // 注册USB事件广播监听器
  2. if(mUSBManager != null){
  3. mUSBManager.registerUSB();
  4. }
  5. // 恢复Camera预览
  6. if(mUVCCameraView != null){
  7. mUVCCameraView.onResume();
  8. }

 


4. 注销USB设备广播事件监听器,停止Camera预览  
  Unregister the USB device broadcast event listener and stop the Camera Preview

 

 

 

 


    
  1. // 注销USB事件广播监听器
  2. if(mUSBManager != null){
  3. mUSBManager.unregisterUSB();
  4. }
  5. // 暂停Camera预览
  6. if(mUVCCameraView != null){
  7. mUVCCameraView.onPause();
  8. }

 


 5. 图片抓拍
  Picture capturing

 

 

 

 


    
  1. if(mUSBManager == null || ! mUSBManager.isCameraOpened()){
  2. showShortMsg( "抓拍异常,摄像头未开启");
  3. return;
  4. }
  5. mUSBManager.capturePicture(picPath, new AbstractUVCCameraHandler.OnCaptureListener() {
  6. @Override
  7. public void onCaptureResult(String path) {
  8. showShortMsg( "保存路径:"+path);
  9. }
  10. });

 


6. 本地录制(可实时获取音视频数据流)
   recoring mp4,and get media real-stream  
   

 

 

 


    
  1. if(mUSBManager == null || ! mUSBManager.isCameraOpened()){
  2. showShortMsg( "录制异常,摄像头未开启");
  3. return;
  4. }
  5. if(! mUSBManager.isRecording()){
  6. String videoPath = USBCameraManager.ROOT_PATH+System.currentTimeMillis();
  7. FileUtils.createfile(FileUtils.ROOT_PATH+ "test666.h264");
  8. RecordParams params = new RecordParams();
  9. params.setRecordPath(videoPath);
  10. params.setRecordDuration( 0); // 设置为0,不分割保存
  11. params.setVoiceClose( false); // 不屏蔽声音
  12. mUSBManager.startRecording(params, new AbstractUVCCameraHandler.OnEncodeResultListener() {
  13. @Override
  14. public void onEncodeResult(byte[] data, int offset, int length, long timestamp, int type) {
  15. // type = 0,aac格式音频流
  16. // type = 1,h264格式视频流
  17. if(type == 1){
  18. FileUtils.putFileStream(data,offset,length);
  19. }
  20. }
  21. @Override
  22. public void onRecordResult(String videoPath) {
  23. showShortMsg(videoPath);
  24. }
  25. });
  26. // 停止录制
  27. mUSBManager.stopRecording();


7. 切换分辨率
  update Resulotion  
    

 


    
  1. mUSBManager.updateResolution( this, mUVCCameraView, 320, 240, new USBCameraManager.OnPreviewListener() {
  2. @Override
  3. public void onPreviewResult(boolean isSuccess) {
  4. if(! isSuccess) {
  5. showShortMsg( "预览失败,不支持该分辨率");
  6. } else {
  7. showShortMsg( "以切换到分辨率为320x240");
  8. }
  9. }
  10. });
  11. // 获取Camera支持得分辨率
  12. List<Size> list = mUSBManager.getSupportedPreviewSizes();
  13. // Camera自动对焦
  14. mUSBManager.startCameraFoucs();


8. 释放引擎资源
  release resource  

 


    
  1. // 释放资源
  2. if(mUSBManager != null){
  3. mUSBManager.release();
  4. }

9. 添加权限
  add permissions  
   


    
  1. <uses-permission android:name= "android.permission.WRITE_EXTERNAL_STORAGE" />
  2. <uses-permission android:name= "android.permission.RECORD_AUDIO" />


USBCameraManager  API (Other)

 


    
  1. ( 1) void requestPermission(int index):请求授予开启USB摄像头权限;
  2. (2) int getUsbDeviceCount ():返回查询到的可用USB Camera数目;
  3. (3) boolean isRecording ():判断是否正在录制视频;
  4. (4) boolean isCameraOpened ():判断USB摄像头是否正常打开;
  5. (5) void release ():释放资源
  6. (6) USBMonitor getUSBMonitor ():返回USBMonitor实例;
  7. (7) mUSBManager. setModelValue (USBCameraManager.MODE_CONTRAST,contrast++); 调整对比度
  8. ( 8) mUSBManager.setModelValue(USBCameraManager.MODE_BRIGHTNESS,brightness++);调整亮度

 

 

 

 

 

注:在使用Android Studio移植UVCCamera时,很多朋友可能会遇到”open(“/dev/bus/usb/001/002”, O_RDWR, 0),报错,Permission denied”问题,这是由于Android Studio使用的ndk版本所致,建议使用ndk-r14即可。解决方法:local.properties–>指定ndk.dir版本。(注:这里使用的是离线方式)

 

最新版为2.1.0,使用方法请移步Github项目

 

 

GitHub源码地址:https://github.com/jiangdongguo/AndroidUSBCamera(如果对您有用,欢迎star&fork以表支持~谢谢^_^!)

上面是原博主的下载地址,有可能下载后无法运行,我调试好后上传了一份新的
https://download.csdn.net/download/qq_31939617/10642906

 

  • 5
    点赞
  • 69
    评论
  • 20
    收藏
  • 一键三连
    一键三连
  • 扫一扫,分享海报

相关推荐
©️2020 CSDN 皮肤主题: 技术黑板 设计师:CSDN官方博客 返回首页