美文网首页
android opencv[7] 摄像头调用

android opencv[7] 摄像头调用

作者: proud2008 | 来源:发表于2020-08-21 10:32 被阅读0次

基于JavaCamera2View组件类

 javaCamera2View.setCameraIndex(0); //使用那个摄像头 后置 前置
 javaCamera2View.setCameraPermissionGranted(); // 摄像头权限问题
 javaCamera2View.enableFpsMeter(); // 显示帧率
 javaCamera2View.enableView(); // 开启
 javaCamera2View.disableView(); //关闭  与开启成对出现 注意周期
javaCamera2View.setCvCameraViewListener(new CvCameraViewListener2());  //回调监听
 private class CvCameraViewListener2__ implements CameraBridgeViewBase.CvCameraViewListener2 {


        @Override
        public void onCameraViewStarted(int width, int height) {

        }

        @Override
        public void onCameraViewStopped() {

        }

        @Override
        public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
            Mat src = inputFrame.rgba();  // 摄像头获取到的图像 是横向的 
            //可以调用任意方法改变图像 改变尺寸时要注意 CameraBridgeViewBase 中有图像的缓存不一致会报错

           return src;
        }
    }

···
//竖屏摄像头旋转问题
protected void deliverAndDrawFrame(CvCameraViewFrame frame) {
Mat modified;
if (mListener != null) {
modified = mListener.onCameraFrame(frame);
} else {
modified = frame.rgba();
}

boolean bmpValid = true;
if (modified != null) {
    try {
        Utils.matToBitmap(modified, mCacheBitmap);
    } catch(Exception e) {
        Log.e(TAG, "Mat type: " + modified);
        Log.e(TAG, "Bitmap type: " + mCacheBitmap.getWidth() + "*" + mCacheBitmap.getHeight());
        Log.e(TAG, "Utils.matToBitmap() throws an exception: " + e.getMessage());
        bmpValid = false;
    }
}

if (bmpValid && mCacheBitmap != null) {
    Canvas canvas = getHolder().lockCanvas();
    if (canvas != null) {
        canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
        if (BuildConfig.DEBUG)
            Log.d(TAG, "mStretch value: " + mScale);

       // ===修改这里解决预览旋转90度问题
        canvas.rotate(90,0,0);
        float scale = canvas.getWidth() / (float)mCacheBitmap.getHeight();
        float scale2 = canvas.getHeight() / (float)mCacheBitmap.getWidth();
        if(scale2 > scale){
            scale = scale2;
        }
        if (scale != 0) {
            canvas.scale(scale, scale,0,0);
        }
        canvas.drawBitmap(mCacheBitmap, 0, -mCacheBitmap.getHeight(), null);
        // ===结束这里解决预览旋转90度问题end

        if (mFpsMeter != null) {
            mFpsMeter.measure();
            mFpsMeter.draw(canvas, 20, 30);
        }
        getHolder().unlockCanvasAndPost(canvas);
    }
}

}
···

//改变尺寸 旋转 报错的问题 JavaCamera2View的父类 CameraBridgeViewBase类中 
 protected void deliverAndDrawFrame(CvCameraViewFrame frame) {
... 
//在方法内部 最后添加
 if (mCacheBitmap != null) { //
            mCacheBitmap.recycle();
            mCacheBitmap = Bitmap.createBitmap(modified.width(), modified.height(), Bitmap.Config.ARGB_8888);
        }
}
 Size szAfter = new Size(640, 480); // Scale back down to 640x480 (original dim.)
Imgproc.resize(result, result, szAfter); //改变尺寸
Core.rotate(result, result, Core.ROTATE_90_CLOCKWISE);  //横屏

相关文章

网友评论

      本文标题:android opencv[7] 摄像头调用

      本文链接:https://www.haomeiwen.com/subject/xowjlktx.html