从官方网站下载 OpenCV 4.1.0 For Android SDK
1.导入\OpenCV-android-sdk\sdk\java,java资源库,自定义名字,修改资源库gradle配置文件
apply plugin: 'com.android.library'
android {
compileSdkVersion 28
buildToolsVersion "28.0.2"
defaultConfig {
//applicationId "org.opencv"
minSdkVersion 24
targetSdkVersion 28
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.txt'
}
}
}
2.新建jniLibs文件件,引入\OpenCV-android-sdk\sdk\native\libs和\OpenCV-android-sdk\sdk\native\staticlibs
引入资源后的jniLibs
3.修改app的gradle配置文件
android:id="@+id/mtjcv"
android:layout_width="match_parent"
android:layout_height="match_parent" />
public class MainActivity extends AppCompatActivity {
// Used to load the 'native-lib' library on application startup.
static {
System.loadLibrary("native-lib");
}
private JavaCameraView javaCamera2View;
Mat mRgba;
Mat mRgbaF;
Mat mRgbaT;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Example of a call to a native method
TextView tv = (TextView) findViewById(R.id.sample_text);
tv.setText(stringFromJNI());
javaCamera2View = findViewById(R.id.mtjcv);
javaCamera2View.enableView();
javaCamera2View.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);//设置前置摄像头
javaCamera2View.setCvCameraViewListener(new CameraBridgeViewBase.CvCameraViewListener2() {
@Override
public void onCameraViewStarted(int width, int height) {
mRgba = new Mat(height, width, CvType.CV_8UC4);
mRgbaF = new Mat(height, width, CvType.CV_8UC4);
mRgbaT = new Mat(width, width, CvType.CV_8UC4);
}
@Override
public void onCameraViewStopped() {
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
mRgba = inputFrame.rgba();
Imgproc.resize(mRgba, mRgbaF, mRgbaF.size(), 0,0, 0);
Core.flip(mRgbaF, mRgba, -1);//旋转图像 正面显示
return mRgba;
}
});
}
@Override
protected void onResume() {
super.onResume();
if (!OpenCVLoader.initDebug()) {
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION, this, mLoaderCallback);
} else {
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
}
//openCV4Android 需要加载用到
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS: {
//jcv.enableView();
// mOpenCvCameraView.setOnTouchListener(ColorBlobDetectionActivity.this);
}
break;
default: {
super.onManagerConnected(status);
}
break;
}
}
};
/**
* A native method that is implemented by the 'native-lib' native library,
* which is packaged with this application.
*/
public native String stringFromJNI();
}
添加摄像头权限,运行成功即可说明集成成功。
1.图像旋转;
Imgproc.resize(mRgba, mRgbaF, mRgbaF.size(), 0,0, 0);
Core.flip(mRgbaF, mRgba, -1);
2.打开前摄像头
javaCamera2View.setCameraIndex(CameraBridgeViewBase.CAMERA_ID_FRONT);
3.全屏设置
CameraBridgeViewBase。java类,deliverAndDrawFrame方法修改。修改后的方法如下
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.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);
// 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);
}
}
}