UVCAndroid,安卓UVC相机通用开发库(支持多预览和多摄像头) Posted 2023-04-02 kanseei
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了UVCAndroid,安卓UVC相机通用开发库(支持多预览和多摄像头)相关的知识,希望对你有一定的参考价值。
文章目录
简介
UVCandroid 是一款用于安卓UVC相机的通用开发库。
GitHub源码地址 :https://github.com/shiyinghan/UVCAndroid
主要功能
主要功能包括: (1) 支持USB Camera设备检测,画面实时预览; (2) 支持抓拍jpg格式图片,可设置图片压缩质量 ; (3) 支持录制mp4格式视频,可屏蔽音频,可设置视频和音频的录制参数 ; (4) 支持获取camera支持的分辨率,和分辨率切换; (5) 支持预览自动识别各种相机的分辨率; (6) 支持旋转摄像头90度、180度、270度 ; (7) 支持调整对比度、亮度、色调、饱和度、白平衡等等一些相机控制参数; (8) 支持多预览和多摄像头; (9) 支持Android5.0+;
如何使用
1. 添加依赖到本地工程
第一步 添加mavenCentral仓库到工程gradle文件 Step 1. Add the mavenCentral repository to your build file Add it in your root build.gradle at the end of repositories:
allprojects
repositories
...
mavenCentral ( )
第二步 添加依赖到app Module的gradle文件
dependencies
implementation 'com.herohan:UVCAndroid:1.0.4'
2. 获取权限
Request permissions
List < String > needPermissions = new ArrayList < > ( ) ;
needPermissions. add ( Manifest . permission. CAMERA) ;
needPermissions. add ( Manifest . permission. WRITE_EXTERNAL_STORAGE) ;
needPermissions. add ( Manifest . permission. RECORD_AUDIO) ;
XXPermissions . with ( this )
. permission ( needPermissions)
. request ( ( permissions, all) ->
if ( ! all)
return ;
) ;
< application
...
android: requestLegacyExternalStorage= " true"
>
3. 初始化UVC业务类,设置UVC摄像头状态回调,设置TextureView或者SurfaceView的Surface监听回调
Initialize CameraHelper,set UVC Camera state callback
private ICameraHelper mCameraHelper;
private AspectRatios urfaceView mCameraViewMain;
private ICameraHelper. StateCallback mStateListener;
mStateListener = new ICameraHelper. StateCallback ( )
@Override
public void onAttach ( UsbDevice device)
mCameraHelper. selectDevice ( device) ;
@Override
public void onDeviceOpen ( UsbDevice device, boolean isFirstOpen)
mCameraHelper. openCamera ( ) ;
@Override
public void onCameraOpen ( UsbDevice device)
mCameraHelper. startPreview ( ) ;
Size size = mCameraHelper. getPreviewSize ( ) ;
if ( size != null )
int width = size. width;
int height = size. height;
mCameraViewMain. setAspectRatio ( width, height) ;
mCameraHelper. addSurface ( mCameraViewMain. getHolder ( ) . getSurface ( ) , false ) ;
@Override
public void onCameraClose ( UsbDevice device)
if ( mCameraHelper != null )
mCameraHelper. removeSurface ( mCameraViewMain. getHolder ( ) . getSurface ( ) ) ;
@Override
public void onDeviceClose ( UsbDevice device)
@Override
public void onDetach ( UsbDevice device)
@Override
public void onCancel ( UsbDevice device)
;
mCameraViewMain. getHolder ( ) . addCallback ( new SurfaceHolder. Callback ( )
@Override
public void surfaceCreated ( @NonNull SurfaceHolder holder)
if ( mCameraHelper != null )
mCameraHelper. addSurface ( holder. getSurface ( ) , false ) ;
@Override
public void surfaceChanged ( @NonNull SurfaceHolder holder, int format, int width, int height)
@Override
public void surfaceDestroyed ( @NonNull SurfaceHolder holder)
if ( mCameraHelper != null )
mCameraHelper. removeSurface ( holder. getSurface ( ) ) ;
) ;
mCameraHelper = new CameraHelper ( ) ;
mCameraHelper. setStateCallback ( mStateListener) ;
4. 释放UVC业务类(包含取消UVC摄像头状态回调,停止Camera预览,关闭Camera等操作)
Release CameraHelper(including canceling UVC Camera state callback, stopping Camera preview, etc.)
mCameraHelper. release ( ) ;
5. 图片抓拍
Image Capture
mCameraHelper. setImageCaptureConfig (
mCameraHelper. getImageCaptureConfig ( ) . setCaptureMode ( ImageCapture . CAPTURE_MODE_MAXIMIZE_QUALITY) ) ;
File file = FileUtils . getCaptureFile ( this , Environment . DIRECTORY_DCIM, ".jpg" ) ;
ImageCapture. OutputFileOptions options =
new ImageCapture. OutputFileOptions. Builder ( file) . build ( ) ;
mCameraHelper. takePicture ( options, new ImageCapture. OnImageCaptureCallback ( )
@Override
public void onImageSaved ( @NonNull ImageCapture. OutputFileResults outputFileResults)
Toast . makeText ( TakePictureActivity . this ,
"save \\"" + UriHelper . getPath ( TakePictureActivity . this , outputFileResults. getSavedUri ( ) ) + "\\"" ,
Toast . LENGTH_SHORT) . show ( ) ;
@Override
public void onError ( int imageCaptureError, @NonNull String message, @Nullable Throwable cause)
Toast . makeText ( TakePictureActivity . this , message, Toast . LENGTH_SHORT) . show ( ) ;
) ;
6. 录制视频
Video Capture
mCameraHelper. setVideoCaptureConfig (
mCameraHelper. getVideoCaptureConfig ( )
. setAudioCaptureEnable ( true )
. setBitRate ( ( int ) ( 1024 * 1024 * 25 * 0.25 ) )
. setVideoFrameRate ( 25 )
. setIFrameInterval ( 1 ) ) ;
File file = FileUtils . getCaptureFile ( this , Environment . DIRECTORY_MOVIES, ".mp4" ) ;
VideoCapture. OutputFileOptions options =
new VideoCapture. OutputFileOptions. Builder ( file) . build ( ) ;
mCameraHelper. startRecording ( options, new VideoCapture. OnVideoCaptureCallback ( )
@Override
public void onStart ( )
@Override
public void onVideoSaved ( @NonNull VideoCapture. OutputFileResults outputFileResults)
Toast . makeText (
RecordVideoActivity . this ,
"save \\"" + UriHelper . getPath ( RecordVideoActivity . this , outputFileResults. getSavedUri ( ) ) + "\\"" ,
Toast . LENGTH_SHORT) . show ( ) ;
@Override
public void onError ( int videoCaptureError, @NonNull String message, @Nullable Throwable cause)
Toast . makeText ( RecordVideoActivity . this , message, Toast . LENGTH_LONG) . show ( ) ;
) ;
7. 改变摄像机预览参数(包括帧格式、宽度、高度、FPS)
Set camera preview parameters (including frame format, width, height, FPS)
mCameraHelper. stopPreview ( ) ;
mCameraHelper. setPreviewSize ( size) ;
mCameraHelper. startPreview ( ) ;
mCameraViewMain. setAspectRatio ( mPreviewWidth, mPreviewHeight) ;
8. 调整对比度、亮度、色调、饱和度、白平衡等等一些相机控制参数
Adjust contrast, brightness, hue, saturation, white balance, and other camera controls
UVCControl control = mCameraHelper. getUVCControl ( ) ;
private void setAllControlChangeListener ( UVCControl controls)
isbBrightness. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setBrightness ( seekParams. progress) ) ;
isbContrast. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setContrast ( seekParams. progress) ) ;
cbContrastAuto. setOnCheckedChangeListener ( ( buttonView, isChecked) ->
controls. setContrastAuto ( isChecked) ;
) ;
isbHue. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setHue ( seekParams. progress) ) ;
cbHueAuto. setOnCheckedChangeListener ( ( buttonView, isChecked) ->
controls. setHueAuto ( isChecked) ;
) ;
isbSaturation. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setSaturation ( seekParams. progress) ) ;
isbSharpness. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setSharpness ( seekParams. progress) ) ;
isbGamma. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setGamma ( seekParams. progress) ) ;
isbWhiteBalance. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setWhiteBalance ( seekParams. progress) ) ;
cbWhiteBalanceAuto. setOnCheckedChangeListener ( ( buttonView, isChecked) ->
controls. setWhiteBalanceAuto ( isChecked) ;
) ;
isbBacklightComp. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setBacklightComp ( seekParams. progress) ) ;
isbGain. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setGain ( seekParams. progress) ) ;
isbExposureTime. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setExposureTimeAbsolute ( seekParams. progress) ) ;
cbExposureTimeAuto. setOnCheckedChangeListener ( ( buttonView, isChecked) ->
controls. setExposureTimeAuto ( isChecked) ;
) ;
isbIris. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setIrisAbsolute ( seekParams. progress) ) ;
isbFocus. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setFocusAbsolute ( seekParams. progress) ) ;
cbFocusAuto. setOnCheckedChangeListener ( ( buttonView, isChecked) ->
controls. setFocusAuto ( isChecked) ;
) ;
isbZoom. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setZoomAbsolute ( seekParams. progress) ) ;
isbPan. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setPanAbsolute ( seekParams. progress) ) ;
isbTilt. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setTiltAbsolute ( seekParams. progress) ) ;
isbRoll. setOnSeekChangeListener (
( MyOnSeekChangeListener ) seekParams -> controls. setRollAbsolute ( seekParams. progress) ) ;
rgPowerLineFrequency. setOnCheckedChangeListener ( ( group, checkedId) ->
int value = 0 ;
if ( checkedId == R . id. rbPowerLineFrequencyDisable)
value = 0 ;
else if ( checkedId == R . id. rbPowerLineFrequency50Hz)
value = 1 ;
else if ( checkedId == R . id. rbPowerLineFrequency60Hz)
value = 2 ;
else if ( checkedId == R . id. rbPowerLineFrequencyAuto)
value = 3 ;
controls. setPowerlineFrequency ( value) ;
) ;
private void resetAllControlParams ( UVCControl control)
control. resetBrightness ( ) ;
control. resetContrast ( ) ;
control. resetContrastAuto ( ) ;
control. resetHue ( ) ;
control. resetHueAuto ( ) ;
control. resetSaturation ( ) ;
control. resetSharpness ( ) ;
control. resetGamma ( ) ;
control. resetWhiteBalance ( ) ;
control. resetWhiteBalanceAuto ( ) ;
control. resetBacklightComp ( ) ;
control. resetGain ( ) Ubuntu14.04安装libusb
libuvc是一个跨平台的USB视频设备库,建立在libusb之上。 它能够对导出标准USB视频类(UVC)接口的USB视频设备进行细化控制,使开发人员能够为以前不支持的设备编写驱动程序,或者以通用的方式访问UVC设备。
首先安装libusb。
先用sudo apt-get install安装了libusb-dev,安装完成但libuvc make的时候还是找不到libusb.h文件。所以用源代码安装。
1.环境
VMware下ubuntu14.04
2.获取源代码
下载 libusb-1.0.21.tar.bz2 http://sourceforge.net/projects/libusb/
3.解压源码
cd /下载
tar -xjvf libusb-1.0.21.tar.bz2
4.按照INSTALL文件给出的提示进行安装:./configure - make - make install
先configure:
cd libusb-1.0.21/
./configure
configure失败,configure: error: "udev support requested but libudev not installed"
安装依赖项libudev-dev:
cd
sudo apt-get install libudev-dev
安装成功后重新configure,成功,再make,make install,安装成功。
5.安装libuvc
此时安装libuvc成功,可以找到libusb.h文件了。
以上是关于UVCAndroid,安卓UVC相机通用开发库(支持多预览和多摄像头)的主要内容,如果未能解决你的问题,请参考以下文章
枚举相机属性集的 UVC 属性项
安卓系统手机怎么装uvc?
杂记4--森云相机UVC驱动编译过程一些问题解决记录
uvc是啥
高手进 VB 支持 UVC摄像头吗?
Ubuntu14.04安装libusb