android調用內置攝像頭的方法相對比較簡單,參考實例也比較多,但是針對調用外置攝像頭的方法介紹比較少,本篇對現有的 資料進行彙總,並給出相關案例如下:
https://blog.csdn.net/fengshiguang2012/article/details/79569280
https://blog.csdn.net/andrexpert/article/details/78324181
https://blog.csdn.net/CrazyMo_/article/details/58139934
開源項目:
https://github.com/saki4510t/UVCCamera
https://github.com/rhllasag/FLIR-App
https://github.com/jiangdongguo/AndroidUSBCamera
https://github.com/search?q=mUVCCameraView+opencv&type=Code
重點總結:
查找資料過程中,可以發現目前調用外置usb攝像頭的方法基本上都是基於UVCCamera這個開源項目,原理是android設備具備otg功能,通過檢測android設備是否有usb設備連接,進一步採集usb攝像頭所採集到的圖像數據。 這裏只對圖像的顯示方法進行總結,圖像顯示主要有兩種方法,一種是:
final SurfaceTexture st = mUVCCameraView.getSurfaceTexture();
mCameraHandler.startPreview(new Surface(st));
另外一種是:
mCameraHandler.setPreviewCallback(mIFrameCallback);
//*************** IFrameCallback ***************
private final IFrameCallback mIFrameCallback = new IFrameCallback() {
@Override
public void onFrame(final ByteBuffer frame) {
frame.clear();
if(!isActive() || isInCapturing){
return;
}
if(bitmap == null){
Toast.makeText(MainActivity.this, "錯誤:Bitmap爲空", Toast.LENGTH_SHORT).show();
return;
}
synchronized (bitmap) {
srcBitmap.copyPixelsFromBuffer(frame);
WarnText = "";
if(bitmap.getWidth() != srcBitmap.getWidth() || bitmap.getHeight() != srcBitmap.getHeight()){
bitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.RGB_565);
}
Mat src = new Mat ();
Utils.bitmapToMat(srcBitmap,src);
calculate(srcBitmap.getNativeObjAddr(), mLeft, mTop, mWidth, mHeight);
Utils.matToBitmap(srcBitmap,bitmap);
}
mImageView.post(mUpdateImageTask);
}
};
第二種方法我將 ByteBuffer 類型的數據轉化爲opencv的Mat類型,但是,這種方法採集到的圖像相比於第一種圖像質量要差一些,具體原因不是很清楚,如果有研究明白的還希望告知一下,謝謝。