先贴上采用的开源库链接:https://github.com/saki4510t/UVCCamera
https://github.com/saki4510t/OpenCVwithUVC -《给USBCamera添加了OpenCV》
业余时间捣鼓了下Android 板子连接多个usb摄像头的方案,一开始使用系统的CameraV1的api,但是取到的摄像头数量一直不对(api: Camera.getNumberOfCameras()),然后又去网上查了方案(传送门:https://blog.youkuaiyun.com/xiangzhihong8/article/details/82877901)发现Android P之后原生就支持多摄像头,心里美滋滋,这么快就大结局了,但是果然天不遂人愿,于是改用CameraV2的api,但还是识别不到完整的摄像头列表。没查到具体原因,但是猜测是跟Android板子有关,虽然在软件上已经支持多摄像头,但还需要底层可能还是限制了最大连接数量。然后去应用市场下了一个usb摄像头app,发现是可以正常识别出所有的摄像头,遂反编译之,发现是使用了UVCCamera。这个开源库貌似已经很久没有维护,并且之前使用的经验来看也有不少bug(主要是兼容性方面),但是手头的硬件只有一个型号,并不需要做太多设备兼容性的适配,因此还是可以拿来一用。下面就分享一下UVCCamera的接入过程。
无论使用哪种Camera的api,Camera的封装都可以大致分为两个流程:数据采集、渲染。于是我们就可以定义出这两块功能的接口:
数据采集
public interface ICamera {
List<String> getCameras();
interface OnPhotoTake {
void onPhotoTake(Bitmap reader,String path);
}
boolean open(String id);
void close();
void takePicture(OnPhotoTake onPhotoTake,String id);
}
渲染
public interface CameraViewInterface extends IAspectRatioView {
interface Callback {
void onSurfaceCreated(CameraViewInterface view, Surface surface);
void onSurfaceChanged(CameraViewInterface view, Surface surface, int width, int height);
void onSurfaceDestroy(CameraViewInterface view, Surface surface);
}
void onPause();
void onResume();
void setCallback(Callback callback);
SurfaceTexture getSurfaceTexture();
Surface getSurface