OpenCV Android解决相机旋转90度及全屏问题
程序员文章站
2022-04-18 11:58:03
一、图像全屏问题首先找到CameraBridgeViewBase文件的 protected void deliverAndDrawFrame(CvCameraViewFrame frame)方法,阅读源码发现该方法主要实现的就是相机预览图的绘制,核心代码如下:if (bmpValid && mCacheBitmap != null) { Canvas canvas = getHolder().lockCanvas(); if (canvas != null) {...
一、图像全屏问题
首先找到CameraBridgeViewBase文件的 protected void deliverAndDrawFrame(CvCameraViewFrame frame)方法,阅读源码发现该方法主要实现的就是相机预览图的绘制,核心代码如下:
if (bmpValid && mCacheBitmap != null) {
Canvas canvas = getHolder().lockCanvas();
if (canvas != null) {
canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
if (mScale != 0) {
canvas.drawBitmap(mCacheBitmap, new Rect(0, 0, mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),
new Rect((int) ((canvas.getWidth() - mScale * mCacheBitmap.getWidth()) / 2),
(int) ((canvas.getHeight() - mScale * mCacheBitmap.getHeight()) / 2),
(int) ((canvas.getWidth() - mScale * mCacheBitmap.getWidth()) / 2 + mScale * mCacheBitmap.getWidth()),
(int) ((canvas.getHeight() - mScale * mCacheBitmap.getHeight()) / 2 + mScale * mCacheBitmap.getHeight())), null);
} else {
canvas.drawBitmap(mCacheBitmap, new Rect(0, 0, mCacheBitmap.getWidth(), mCacheBitmap.getHeight()),
new Rect((canvas.getWidth() - mCacheBitmap.getWidth()) / 2,
(canvas.getHeight() - mCacheBitmap.getHeight()) / 2,
(canvas.getWidth() - mCacheBitmap.getWidth()) / 2 + mCacheBitmap.getWidth(),
(canvas.getHeight() - mCacheBitmap.getHeight()) / 2 + mCacheBitmap.getHeight()), null);
}
if (mFpsMeter != null) {
mFpsMeter.measure();
mFpsMeter.draw(canvas, 20, 30);
}
getHolder().unlockCanvasAndPost(canvas);
}
}
因此将绘制代码重新设计后可以解决图像旋转及全屏问题,解决如下:
if (bmpValid && mCacheBitmap != null) {
Canvas canvas = getHolder().lockCanvas();
if (canvas != null) {
canvas.drawColor(0, android.graphics.PorterDuff.Mode.CLEAR);
// 修改预览旋转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);
}
}
亲测这个方案可行,但是仅仅适用于竖屏并且是后置摄像头,如果改为前置摄像头、横屏模式则不行,还需要进一步修改,考虑到能不修改源码尽量不修改的原则,继续深挖代码。我们尝试在mScale上做文章,JavaCameraView类中有这么一段代码:
if ((getLayoutParams().width == LayoutParams.MATCH_PARENT) && (getLayoutParams().height == LayoutParams.MATCH_PARENT))
mScale = Math.min(((float)height)/mFrameHeight, ((float)width)/mFrameWidth);
else
mScale = 0;
这里决定了相机获取图像映射到画布上的缩放比例,很明显取最小值是要保证图像缩放,全部显示在画布上。
修改为裁剪图像,只显示其中间部分,如下:
if ((getLayoutParams().width == LayoutParams.MATCH_PARENT) && (getLayoutParams().height == LayoutParams.MATCH_PARENT)) {
if (isShowFullPreview()) {
mScale = Math.min(((float) height) / mFrameHeight, ((float) width) / mFrameWidth);
} else {
mScale = Math.max(((float) height) / mFrameHeight, ((float) width) / mFrameWidth);
}
} else {
mScale = 0;
}
还有其他解决方案,有兴趣的童鞋可以继续深挖。比如mFpsMeter对象的处理
二、图像旋转问题
旋转问题分为横屏、竖屏分开处理,
1、AndroidManifest.xml文件中指定Activity的屏幕方向:android:screenOrientation。
2、Activity继承CvCameraViewListener2接口,实现public Mat onCameraFrame(CvCameraViewFrame inputFrame)方法。如下:
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
Mat frame = inputFrame.rgba();
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
if (mOpenCvCameraView.isFrontCamare()) {
Core.rotate(frame, frame, Core.ROTATE_90_COUNTERCLOCKWISE);
Core.flip(frame, frame, 1);
} else {
Core.rotate(frame, frame, Core.ROTATE_90_CLOCKWISE);
}
}
else {
if (mOpenCvCameraView.isFrontCamare()) {
Core.flip(frame, frame, 1);
}
}
return frame;
}
本文地址:https://blog.csdn.net/liuhongshuo2012/article/details/107100870