安卓OpenCV开发(三)竖屏检测
作者:互联网
##前言
对于OpenCV的竖屏检测,网络有很多,多到百度排名前几页,都是一大堆,但为什么要写这个文章,因为他们的文章,大部分都是有问题,或者是不可用的,以下为他们的实现方法:
(1)随便改改canvas绘制方向,并无其他操作
(2)使用WindowManager检测屏幕旋转方向,修改canvas绘制方式
对于以上两种做法,实际是欠佳的,改的是canvas,不是数据源,识别过程还得横屏才能识别,难道写这些文章的人都没发现?
##方法
首先,官方的Demo,跑的是横屏逻辑。而横屏和竖屏最大的区别就是在于角度和长宽比不一样。核心原理,在于宽高的转换,方向的改变。
怎样改?
首先,先对JavaCameraView代码进行修改,核心改动如下:
private class JavaCameraFrame implements CvCameraViewFrame {
@Override
public Mat gray() {
//返回Mat里的选定区域,这跟Yuv420sp格式紧密相关
//return mYuvFrameData.submat(0, mHeight, 0, mWidth);
//#Modified step3.1
Core.rotate(mYuvFrameData.submat(0, mHeight, 0, mWidth),
portrait_gray,Core.ROTATE_90_CLOCKWISE);
return portrait_gray;
}
@Override
public Mat rgba() {
if (mPreviewFormat == ImageFormat.NV21)
Imgproc.cvtColor(mYuvFrameData, mRgba, Imgproc.COLOR_YUV2RGBA_NV21, 4);
else if (mPreviewFormat == ImageFormat.YV12)
Imgproc.cvtColor(mYuvFrameData, mRgba, Imgproc.COLOR_YUV2RGB_I420, 4); // COLOR_YUV2RGBA_YV12 produces inverted colors
else
throw new IllegalArgumentException("Preview Format can be NV21 or YV12");
//#Modified step3.2
Core.rotate(mRgba, portrait_rgba,Core.ROTATE_90_CLOCKWISE);
return portrait_rgba;
}
public JavaCameraFrame(Mat Yuv420sp, int width, int height) {
super();
mWidth = width;
mHeight = height;
//#Modified
portrait_mHeight=mWidth;
portrait_mWidth=mHeight;
portrait_gray=new Mat(portrait_mHeight,portrait_mWidth,CvType.CV_8UC1);
portrait_rgba=new Mat(portrait_mHeight,portrait_mWidth,CvType.CV_8UC4);
mYuvFrameData = Yuv420sp;
mRgba = new Mat();
}
public void release() {
mRgba.release();
}
private Mat mYuvFrameData;
private Mat mRgba;
private int mWidth;
private int mHeight;
//#Modified
private int portrait_mHeight;
private int portrait_mWidth;
private Mat portrait_gray;
private Mat portrait_rgba;
}
其实原理,就是通过Core.rotate()方法进行旋转。
第二个,修改CameraBridgeViewBase的代码,核心代码如下:
/**
* This helper method can be called by subclasses to select camera preview size.
* It goes over the list of the supported preview sizes and selects the maximum one which
* fits both values set via setMaxFrameSize() and surface frame allocated for this view
*
* @param supportedSizes
* @param surfaceWidth
* @param surfaceHeight
* @return optimal frame size
*/
protected Size calculateCameraFrameSize(List<?> supportedSizes, ListItemAccessor accessor, int surfaceWidth, int surfaceHeight) {
//选择一个相机frame大小
int calcWidth = 0;
int calcHeight = 0;
//允许的最大width和height
//#Modified step4
//相机Frame的mMaxWidth应该与surface的surfaceHeight比
//相机Frame的mMaxHeight应该与surface的surfaceWidth比
//int maxAllowedWidth = (mMaxWidth != MAX_UNSPECIFIED && mMaxWidth < surfaceWidth)? mMaxWidth : surfaceWidth;
//int maxAllowedHeight = (mMaxHeight != MAX_UNSPECIFIED && mMaxHeight < surfaceHeight)? mMaxHeight : surfaceHeight;
int maxAllowedWidth = (mMaxWidth != MAX_UNSPECIFIED && mMaxWidth < surfaceHeight) ? mMaxWidth : surfaceHeight;
int maxAllowedHeight = (mMaxHeight != MAX_UNSPECIFIED && mMaxHeight < surfaceWidth) ? mMaxHeight : surfaceWidth;
for (Object size : supportedSizes) {
int width = accessor.getWidth(size);
int height = accessor.getHeight(size);
//在允许的范围内选择最大的size
//client是可通过设置小的mMaxWidth,mMaxHeight来选择低分辨率frame的
if (width <= maxAllowedWidth && height <= maxAllowedHeight) {
if (width >= calcWidth && height >= calcHeight) {
calcWidth = (int) width;
calcHeight = (int) height;
}
}
}
return new Size(calcWidth, calcHeight);
}
/**
* This method shall be called by the subclasses when they have valid
* object and want it to be delivered to external client (via callback) and
* then displayed on the screen.
*
* @param frame - the current frame to be delivered
*/
protected void deliverAndDrawFrame(CvCameraViewFrame frame) {
Mat modified;
if (mListener != null) {
//CvCameraViewListener2 mListener是client指定的
//这里调用客户重载的接口方法且接收返回值
//这里都是在数据处理线程里执行的
modified = mListener.onCameraFrame(frame);
} else {
//若client没指定CvCameraViewListener2 mListener即client不准备处理preview数据
//则modified设置为
//onPreviewFrame传回的数据转换成的rgba Mat
modified = frame.rgba();
}
//Log Mat的大小和Bitmap的大小
Log.d("FunnyAR", "mScale: " + mScale + " modified.rows: " + modified.rows()
+ " modified.cols: " + modified.cols() + " mCacheBitmap.getWidth(): " +
mCacheBitmap.getWidth() + " mCacheBitmap.getHeight() " +
mCacheBitmap.getHeight());
//标志modified转Bitmap是否成功
boolean bmpValid = true;
//若确实有modified则将其转为Bitmap
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;
}
}
//转换成功通过画布画到surface里
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);
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);
}
}
}
通过修改获取帧大小和帧分发的函数,即可。具体原理就是宽高互换。
而对于前置摄像头的方向,这里不再啰嗦了,原理一样。
修改文件源码:链接:https://pan.baidu.com/s/1OJs8IgV0TD6_Hq6nU1g9Xg
提取码:tr4i
复制这段内容后打开百度网盘手机App,操作更方便哦
that’s all-------------------------------------------------------------------------------------------
标签:canvas,int,getHeight,安卓,mCacheBitmap,竖屏,getWidth,OpenCV,portrait 来源: https://blog.csdn.net/motosheep/article/details/118611767