当前位置:网站首页>UVC camera encapsulation class
UVC camera encapsulation class
2022-04-23 01:30:00 【Jianren Yinkou】
The following code is about UVC camera Encapsulation class for processing , Specifically dependent on aar Please refer to https://github.com/saki4510t/UVCCamera.
Borrow this class , In the access UVC The camera device can be used conveniently , Just in the corresponding Activity in , Initialize this class first , Bind one SurfaceView, Easy to display camera video data , And then onStart Call middle note onStart Method 、onStop Call middle note onStop Method 、 stay onDestroy Call middle note release that will do .
public class UVCCameraManager {
private static final String TAG = UVCCameraManager.class.getSimpleName();
private final USBMonitor mUSBMonitor;
private UVCCamera mUVCCamera;
private SurfaceView mUVCCameraView;
private Surface mPreviewSurface;
private final Object mSync = new Object();
private boolean isActive, isPreview;
private final HandlerThreadHandler mWorkerHandler;
private final Handler mUIHandler = new Handler(Looper.getMainLooper());
private long mWorkerThreadID;
public UVCCameraManager(@NonNull Context context, @NonNull SurfaceView surfaceView) {
mUSBMonitor = new USBMonitor(context, mConnectListener);
mWorkerHandler = HandlerThreadHandler.createHandler(TAG);
mWorkerThreadID = mWorkerHandler.getLooper().getThread().getId();
mUVCCameraView = surfaceView;
mUVCCameraView.getHolder().addCallback(mSurfaceViewCallback);
}
public void onStart() {
L.d(TAG, "onStart:");
synchronized (mSync) {
mUSBMonitor.register();
}
}
public void onStop() {
L.d(TAG, "onStop:");
synchronized (mSync) {
mUSBMonitor.unregister();
}
}
public void release() {
synchronized (mSync) {
isActive = isPreview = false;
if (mUVCCamera != null) {
mUVCCamera.destroy();
mUVCCamera = null;
}
mUSBMonitor.destroy();
}
try {
mWorkerHandler.getLooper().quit();
} catch (Exception e) {
e.printStackTrace();
}
}
private synchronized void queueEvent(final Runnable task, final long delayMillis) {
if (task == null) return;
try {
mWorkerHandler.removeCallbacks(task);
if (delayMillis > 0) {
mWorkerHandler.postDelayed(task, delayMillis);
} else if (mWorkerThreadID == Thread.currentThread().getId()) {
task.run();
} else {
mWorkerHandler.post(task);
}
} catch (final Exception e) {
// ignore
}
}
private final USBMonitor.OnDeviceConnectListener mConnectListener = new USBMonitor.OnDeviceConnectListener() {
@Override
public void onAttach(UsbDevice usbDevice) {
L.d(TAG, "onAttach");
if (usbDevice != null) {
mUSBMonitor.requestPermission(usbDevice);
} else {
L.d(TAG, "usbDevice is null");
}
}
@Override
public void onDettach(UsbDevice usbDevice) {
L.d(TAG, "onDettach");
}
@Override
public void onConnect(UsbDevice usbDevice, USBMonitor.UsbControlBlock ctrlBlock, boolean createNew) {
L.d(TAG, "onConnect:");
synchronized (mSync) {
if (mUVCCamera != null) {
mUVCCamera.destroy();
}
isActive = isPreview = false;
}
queueEvent(() -> {
synchronized (mSync) {
final UVCCamera camera = new UVCCamera();
camera.open(ctrlBlock);
L.d(TAG, "supportedSize:" + camera.getSupportedSize());
try {
camera.setPreviewSize(UVCCamera.DEFAULT_PREVIEW_WIDTH, UVCCamera.DEFAULT_PREVIEW_HEIGHT, UVCCamera.FRAME_FORMAT_MJPEG);
} catch (IllegalArgumentException e) {
try {
// fallback to YUV mode
camera.setPreviewSize(UVCCamera.DEFAULT_PREVIEW_WIDTH, UVCCamera.DEFAULT_PREVIEW_HEIGHT, UVCCamera.DEFAULT_PREVIEW_MODE);
} catch (IllegalArgumentException ex) {
camera.destroy();
return;
}
}
mPreviewSurface = mUVCCameraView.getHolder().getSurface();
if (mPreviewSurface != null) {
isActive = true;
camera.setPreviewDisplay(mPreviewSurface);
// For the first time, the screen will be black here ???
//isPreview = camera.startPreview()>=0;
//L.d(TAG,"startPreview:"+isPreview);
}
synchronized (mSync) {
mUVCCamera = camera;
}
}
}, 0);
mUIHandler.post(() -> {
mUVCCameraView.setVisibility(View.VISIBLE);
mUVCCameraView.bringToFront();
});
}
@Override
public void onDisconnect(UsbDevice usbDevice, USBMonitor.UsbControlBlock usbControlBlock) {
L.d(TAG, "onDisconnect:");
// XXX you should check whether the comming device equal to camera device that currently using
queueEvent(() -> {
synchronized (mSync) {
if (mUVCCamera != null) {
mUVCCamera.close();
if (mPreviewSurface != null) {
mPreviewSurface.release();
mPreviewSurface = null;
}
isActive = isPreview = false;
}
}
}, 0);
mUIHandler.post(() -> mUVCCameraView.setVisibility(View.GONE));
}
@Override
public void onCancel(UsbDevice usbDevice) {
L.d(TAG, "onCancel:");
}
};
private final SurfaceHolder.Callback mSurfaceViewCallback = new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(final SurfaceHolder holder) {
L.d(TAG, "surfaceCreated:");
}
@Override
public void surfaceChanged(final SurfaceHolder holder, final int format, final int width, final int height) {
if ((width == 0) || (height == 0)) return;
L.d(TAG, "surfaceChanged:");
mPreviewSurface = holder.getSurface();
synchronized (mSync) {
if (isActive && !isPreview && (mUVCCamera != null)) {
mUVCCamera.setPreviewDisplay(mPreviewSurface);
mUVCCamera.startPreview();
isPreview = true;
L.d(TAG, "startPreview:" + isPreview);
} else {
L.d(TAG, "isActive:" + isActive + ",isPreview:" + isPreview + ",mUVCCamera:" + mUVCCamera);
}
}
}
@Override
public void surfaceDestroyed(final SurfaceHolder holder) {
L.d(TAG, "surfaceDestroyed:");
synchronized (mSync) {
if (mUVCCamera != null) {
mUVCCamera.stopPreview();
}
isPreview = false;
}
mPreviewSurface = null;
}
};
}
版权声明
本文为[Jianren Yinkou]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204220942099459.html
边栏推荐
- Gbase 8s存儲結構簡介及空間管理
- Gbase 8s query processing and optimization
- “自虐神器”一夜爆火:用手柄控制自己的脸,代码自取,后果自负
- Configuration of imx6ull bare metal development analysis and configuration process of elcdif lighting RGB LCD
- (product resources) mingdeyang ad8488 module high performance digital X-ray FMC interface 128 analog channel high-speed ADC chip
- NR polar code 七- SCL(succesive cancellation list decoding)
- Live broadcast software | IPTV live broadcast software | TV live broadcast | tvplayer IPTV easyplayer | youwo live broadcast | customized development of super live broadcast software
- World reading day: 18 it books with Douban score above 9.0 are worth collecting
- How to introduce SPI into a project
- 世界读书日:18本豆瓣评分9.0以上的IT书值得收藏
猜你喜欢
“自虐神器”一夜爆火:用手柄控制自己的脸,代码自取,后果自负
10月24号是什么?中国的高级程序员为什么没有五六十岁的,而国外人、、、,玉念聿辉带你过1024程序员节日
200. Number of islands
DFS奇偶性剪枝
App中使用微信公众号的模版消息来进行消息推送
计蒜客:方程的解数
Counting garlic customers: Sudoku (DFS)
What kind of project is suitable for automated testing?
In the second half of the smart watch, opportunities and challenges coexist
JD side: comment un thread enfant obtient - il la valeur de threadlocal du thread parent? Je suis couvert...
随机推荐
Live broadcast software | IPTV live broadcast software | TV live broadcast | tvplayer IPTV easyplayer | youwo live broadcast | customized development of super live broadcast software
DFS奇偶性剪枝
Rotation slice
Unity结合iTextSharp生成PDF 准备dll
无关联进程间通信 —— 命名管道的创建和使用
Introduction to gbase 8s checkpoint
Is it difficult for girls to learn software testing?
找数字(DFS)
Linked list dynamic header insertion
gin--hello
Introduction to gbase8s SQL Engine framework
计蒜客家谱(dfs求直系后代数)
Planning garlic guest (outing) (DFS and BFS solution of dyeing block problem)
全排列(DFS和next_permutation解法)
Google developer tool preserve log
Gbase 8s 并发控制之粒度锁介绍
GBASE 8s 并发控制之封锁操作
Gbase 8s query processing and optimization
Find number (DFS)
Gbase 8t index