一: 最近尝试把Opencv配置在安卓上,具体的配置网上有很多。配置成功后直接用android opencv sdk中的sample作了测试,我用的是里面的tutorial1。
适当修改tutorial1使得demo能从前置摄像头提取视频帧。但是修改后发现从前置摄像头得到的图像的方向不对,若想旋转90则可以通过矩阵转置变换transpose(src, dst)来实现,若想水平或者垂直旋转可以通过opencv的flip函数来实现图像方向的翻转。flip函数的官方api介绍如下
C++: void ocl::flip(const oclMat& src, oclMat& dst, int flipCode)
Parameters:
src – source image.
dst – destination image.
flipCode – specifies how to flip the array: 0 means flipping around the x-axis, positive (e.g., 1) means flipping around y-axis, and negative (e.g., -1) means flipping around both axes.
The function flip flips the array in one of three different ways (row and column indices are 0-based). Supports all data types.
由于此函数属于Core包,所以还需要import org.opencv.core.Core;
最后在onCameraFrame函数中就可以对帧进行翻转了。代码如下
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
Mat srcImg = inputFrame.rgba();
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{ // 竖屏
// doSomrthing
Core.flip(srcImg, srcImg, 0);//flip aroud Y-axis
} else
{
// 横屏时dosomething
Core.flip(srcImg, srcImg, 1);//flip aroud Y-axis
}
return srcImg;
}
二:此外,若只想让Android屏幕横屏显示,可以在Manifest文件增加如下配置
android:screenOrientation="landscape" android:configChanges="keyboardHidden|orientation"
增加后的完整Manifest文件为:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="**.**.**.**">
<uses-sdk tools:overrideLibrary="org.opencv"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".Tutorial1Activity"
android:screenOrientation="landscape"
android:configChanges="keyboardHidden|orientation" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
<supports-screens android:resizeable="true"
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:anyDensity="true" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" android:required="false"/>
<uses-feature android:name="android.hardware.camera.autofocus" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front" android:required="false"/>
<uses-feature android:name="android.hardware.camera.front.autofocus" android:required="false"/>
</manifest>
三: 若在AndroidManifest中没有强制设置app运行的屏幕方向,并想在代码中对横屏和竖屏分别进行处理。可以通过下面的代码来控制
if (this.getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT)
{ // 竖屏
// doSomrthing
} else
{
// 横屏时dosomething
}
Configuration在android.content.res.Configuration包中。
四: 从摄像头拿到帧后把Mat转化为byte[]并对图像数据进行操作,操作后回显。可通过如下方式实现。
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {
Mat srcImg = inputFrame.rgba();
int t = srcImg.channels();
int width = srcImg.width();
int height = srcImg.height();
int rows = srcImg.rows();
int cols = srcImg.cols();
int imgSize = width*height*srcImg.channels();
byte buff[] = new byte[imgSize];
byte dstbuff[] = new byte[imgSize];
srcImg.get(0, 0, buff); //获取srcImg中所有的像素
//do whatever you want using buff
srcImg.put(0, 0, dstbuff);//将buff放回原mat,完成对mat的修改操作
return srcImg;
}
Reference:
http://blog.csdn.net/wunghao8/article/details/38868281
http://blog.csdn.net/watkinsong/article/details/9189649
http://blog.csdn.net/yangtrees/article/details/38279351
http://my.oschina.net/u/1246663/blog/197626