Universal UVCCamera library,supporting recording, pushing, etc
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

193 lines
6.5 KiB

# AndroidUSBCamera开源项目
7 years ago
### AndroidUSBCamera基于[saki4510t/UVCCamera](https://github.com/saki4510t/UVCCamera)开发,该项目对USB Camera(UVC设备)的使用和视频数据采集进行了高度封装,能够帮助开发者通过几个简单的API实现USB Camera设备的检测、连接、预览和音视频数据采集,最重要的是手机无需root,只需支持otg功能即可驱动。主要功能包括:  
7 years ago
(1)支持USB Camera设备检测,画面实时预览;
(2)支持本地录制mp4格式视频,支持实时获取音视频数据流;
(3)支持jpg格式图片抓拍;
(4)支持多种分辨率切换;
(5)支持屏蔽声音;
7 years ago
> AndroidUSBCamera is developed based on the saki4510t/UVCCamera, the project of USB Camera (UVC equipment) and the use of video data acquisition are highly packaged, and it can help developers using USB Camera devices to connect, preview and video data collection by a few simple API. The main functions include:
7 years ago
  (1)supports detecting USB Camera equipment, and previewing;
(2)supports recording MP4 format video, and acquiring real-time audio and video data;
(3)supports capturing JPG format image;
(4)supports switching resolution;
(5)support shielding sound;
7 years ago
## 如何使用AndroidUSBCamera项目
![效果图](http://img.blog.csdn.net/20171025213631816?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvQW5kckV4cGVydA==/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
7 years ago
### 1.添加依赖到本地工程
To get a Git project into your build:
7 years ago
第一步 添加JitPack仓库到工程gradle
Step 1. Add the JitPack repository to your build file
Add it in your root build.gradle at the end of repositories:
7 years ago
```
allprojects {
repositories {
...
maven { url 'http://raw.github.com/saki4510t/libcommon/master/repository/' }
maven { url 'https://jitpack.io' }
}
}
7 years ago
```
7 years ago
第二步 添加依赖到app Module的gradle
7 years ago
Step 2. Add the dependency
```
dependencies {
compile 'com.github.jiangdongguo:AndroidUSBCamera:v1.2.1'
}
7 years ago
```
7 years ago
### 2.初始化引擎,注册USB设备事件监听器
7 years ago
Init AndroidUSBCamera engine,register the USB device event listener
```
USBCameraManager mUSBManager = USBCameraManager.getInstance();
// mTextureView为UVCCameraTextureView实例,继承于TextureView
// 用于渲染图像,需要在xml文件中定义
CameraViewInterface mUVCCameraView = (CameraViewInterface) mTextureView;
// 初始化引擎,注册事件监听器
mUSBManager.init(this, mUVCCameraView, new USBCameraManager.OnMyDevConnectListener() {
// 插入USB设备
@Override
public void onAttachDev(UsbDevice device) {
if(mUSBManager == null || mUSBManager.getUsbDeviceCount() == 0){
showShortMsg("未检测到USB摄像头设备");
return;
}
// 请求打开摄像头
if(! isRequest){
isRequest = true;
if(mUSBManager != null){
mUSBManager.requestPermission(0);
}
}
}
// 拔出USB设备
@Override
public void onDettachDev(UsbDevice device) {
if(isRequest){
// 关闭摄像头
isRequest = false;
mUSBManager.closeCamera();
showShortMsg(device.getDeviceName()+"已拨出");
}
}
// 连接USB设备成功
@Override
public void onConnectDev(UsbDevice device,boolean isConnected) {
if(! isConnected) {
showShortMsg("连接失败,请检查分辨率参数是否正确");
}
}
// 与USB设备断开连接
@Override
public void onDisConnectDev(UsbDevice device) {
}
};
7 years ago
```
7 years ago
### 3. 注册USB设备广播事件监听器,开始Camera预览
7 years ago
Register the USB device broadcast event listener and start the Camera Preview
7 years ago
```
// 注册USB事件广播监听器
if(mUSBManager != null){
mUSBManager.registerUSB();
}
// 恢复Camera预览
if(mUVCCameraView != null){
mUVCCameraView.onResume();
}
7 years ago
```
7 years ago
### 4. 注销USB设备广播事件监听器,停止Camera预览
7 years ago
Unregister the USB device broadcast event listener and stop the Camera Preview
7 years ago
```
// 注销USB事件广播监听器
if(mUSBManager != null){
mUSBManager.unregisterUSB();
}
// 暂停Camera预览
if(mUVCCameraView != null){
mUVCCameraView.onPause();
}
7 years ago
```
### 5. 图片抓拍
7 years ago
Picture capturing
7 years ago
```
if(mUSBManager == null || ! mUSBManager.isCameraOpened()){
showShortMsg("抓拍异常,摄像头未开启");
return;
}
mUSBManager.capturePicture(picPath);
7 years ago
```
### 6. 本地录制(可实时获取音视频数据流)
7 years ago
recoring mp4,and get media real-stream
7 years ago
```
if(mUSBManager == null || ! mUSBManager.isCameraOpened()){
showShortMsg("录制异常,摄像头未开启");
return;
}
// 开始录制
if( !mUSBManager.isRecording()){
mUSBManager.startRecording(videoPath, new AbstractUVCCameraHandler.OnEncodeResultListener() {
@Override
public void onEncodeResult(byte[] data, int offset, int length, long timestamp, int type) {
7 years ago
             // type=0为音频流,type=1为视频流
});
}
// 停止录制
mUSBManager.stopRecording();
7 years ago
```
### 7. 切换分辨率
update Resulotion  
```
mUSBManager.updateResolution(this, mUVCCameraView, 320, 240, new USBCameraManager.OnPreviewListener() {
@Override
public void onPreviewResult(boolean isSuccess) {
if(! isSuccess) {
showShortMsg("预览失败,不支持该分辨率");
}else {
showShortMsg("以切换到分辨率为320x240");
}
}
});
```
### 8. 释放引擎资源
7 years ago
release resource
7 years ago
```
// 释放资源
if(mUSBManager != null){
mUSBManager.release();
}
7 years ago
```
7 years ago
### 9. 添加权限
 add permissions  
```
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RECORD_AUDIO" />
```
7 years ago
### USBCameraManager API (Other)
```
(1) void requestPermission(int index):请求授予开启USB摄像头权限;
(2) int getUsbDeviceCount():返回查询到的可用USB Camera数目;
(3) boolean isRecording():判断是否正在录制视频;
(4) boolean isCameraOpened():判断USB摄像头是否正常打开;
(5) void release():释放资源
(6) USBMonitor getUSBMonitor():返回USBMonitor实例;
7 years ago
```