概述
package com.example.opencvdemo;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements CvCameraViewListener2 {
/**
* 展示原图和对比图
*/
private ImageView iv1, iv2;
/**
* 原图和对比图
*/
private Bitmap bmp1, bmp2;
/**
* 拍摄原图、拍摄对比图、对比、自动对比
*/
private Button pz1, pz2, db, zddb;
/**
* 显示相似度(完全相同值为1)
*/
private TextView tv;
/**
* CV相机
*/
private CameraBridgeViewBase mCVCamera;
/**
* 加载OpenCV的回调
*/
private BaseLoaderCallback mLoaderCallback;
/**
* 拍照状态 0:不拍照 ,1:拍原图,2:拍对比图,3:拍对比图并自动对比
*/
private int isTakePhoto = 0;
/**
* 用于定时执行图片对比
*/
private Handler handler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// 初始化
init();
}
@SuppressLint("HandlerLeak")
private void init() {
// 初始化
iv1 = (ImageView) findViewById(R.id.iv1);
iv2 = (ImageView) findViewById(R.id.iv2);
pz1 = (Button) findViewById(R.id.pz1);
pz2 = (Button) findViewById(R.id.pz2);
db = (Button) findViewById(R.id.db);
zddb = (Button) findViewById(R.id.zddb);
tv = (TextView) findViewById(R.id.tv);
handler = new Handler() {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
// 自动拍照并对比
isTakePhoto = 3;
}
};
OnClickListener btnOnClick = new OnClickListener() {
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.pz1:
// 拍原图
isTakePhoto = 1;
break;
case R.id.pz2:
// 拍对比图
isTakePhoto = 2;
break;
case R.id.db:
hist();
break;
case R.id.zddb:
// 拍对比图并自动对比
isTakePhoto = 3;
break;
}
}
};
// 设置点击事件
pz1.setOnClickListener(btnOnClick);
pz2.setOnClickListener(btnOnClick);
db.setOnClickListener(btnOnClick);
zddb.setOnClickListener(btnOnClick);
// 初始化CV相机
mCVCamera = (CameraBridgeViewBase) findViewById(R.id.cv);
mCVCamera.setVisibility(CameraBridgeViewBase.VISIBLE);
// 设置相机监听
mCVCamera.setCvCameraViewListener(this);
// 连接到OpenCV的回调
mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
mCVCamera.enableView();
break;
default:
break;
}
}
};
}
private void hist() {
// 对比算法会耗时,导致页面卡顿,所以新开线程进行对比
new Thread(new Runnable() {
@Override
public void run() {
// 对比
final double target = HistUtils.comPareHist(bmp1, bmp2);
MainActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
// 将相似度显示在左上角
tv.setText("相似度:" target);
}
});
}
}).start();
}
@Override
public void onCameraViewStarted(int width, int height) {
}
@Override
public void onCameraViewStopped() {
}
@Override
public Mat onCameraFrame(CvCameraViewFrame inputFrame) {// 相机拍摄每一帧的图像,都在此处理
// 获取相机中的图像
final Mat rgba = inputFrame.rgba();
if (isTakePhoto != 0) {
// 记录拍照状态
final int who = isTakePhoto;
// 重置拍照状态
isTakePhoto = 0;
// 要把Mat对象转换成Bitmap对象,需要创建一个宽高相同的Bitmap对象昨晚参数
final Bitmap bmp = Bitmap.createBitmap(rgba.cols(), rgba.rows(),
Config.RGB_565);
// 记录要展示图片的ImageView
ImageView iv = null;
// Mat >>> Bitmap
Utils.matToBitmap(rgba, bmp);
if (who == 1) {
// 展示原图
iv = iv1;
bmp1 = bmp;
} else if (who == 2) {
// 展示对比图
iv = iv2;
bmp2 = bmp;
} else {
// 展示对比图
iv = iv2;
bmp2 = bmp;
// 对比
hist();
// 每隔0.5秒对比一次
handler.sendEmptyMessageDelayed(1, 500);
}
// 记录要展示图片的ImageView
final ImageView image = iv;
runOnUiThread(new Runnable() {
@Override
public void run() {
// 展示拍到的图片
image.setImageBitmap(bmp);
if (bmp1 != null) {
// 如果原图已经拍好了,那么可以进行自动对比,将自动对比按钮设置为可用
zddb.setEnabled(true);
if (bmp2 != null) {
// 如果原图和对比图都已经拍好了,那么可以进行对比,将对比按钮设置为可用
db.setEnabled(true);
}
}
}
});
}
// 将每一帧的图像展示在界面上
return inputFrame.rgba();
}
@Override
protected void onResume() {
// 界面加载完成的时候向OpenCV的连接回调发送连接成功的信号
if (OpenCVLoader.initDebug()) {
mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
}
super.onResume();
}
@Override
protected void onPause() {
super.onPause();
// 销毁OpenCV相机
if (mCVCamera != null)
mCVCamera.disableView();
}
}
资源下载此资源下载价格为2D币,请先登录
资源文件列表
OpenCVDemo/.classpath , 475
OpenCVDemo/.cproject , 3623
OpenCVDemo/.project , 2982
OpenCVDemo/.settings/org.eclipse.jdt.core.prefs , 177
OpenCVDemo/AndroidManifest.xml , 1418
OpenCVDemo/bin/AndroidManifest.xml , 1418
OpenCVDemo/bin/classes/com/example/opencvdemo/BuildConfig.class , 353
OpenCVDemo/bin/classes/com/example/opencvdemo/HistUtils.class , 1225
OpenCVDemo/bin/classes/com/example/opencvdemo/MainActivity$1.class , 778
OpenCVDemo/bin/classes/com/example/opencvdemo/MainActivity$2.class , 1007
OpenCVDemo/bin/classes/com/example/opencvdemo/MainActivity$3.class , 1023
OpenCVDemo/bin/classes/com/example/opencvdemo/MainActivity$4$1.class , 1204
OpenCVDemo/bin/classes/com/example/opencvdemo/MainActivity$4.class , 1227
OpenCVDemo/bin/classes/com/example/opencvdemo/MainActivity$5.class , 1496
OpenCVDemo/bin/classes/com/example/opencvdemo/MainActivity.class , 5603
OpenCVDemo/bin/classes/com/example/opencvdemo/R$attr.class , 437
OpenCVDemo/bin/classes/com/example/opencvdemo/R$dimen.class , 473
OpenCVDemo/bin/classes/com/example/opencvdemo/R$drawable.class , 446
OpenCVDemo/bin/classes/com/example/opencvdemo/R$id.class , 716
OpenCVDemo/bin/classes/com/example/opencvdemo/R$layout.class , 415
OpenCVDemo/bin/classes/com/example/opencvdemo/R$string.class , 484
OpenCVDemo/bin/classes/com/example/opencvdemo/R$style.class , 443
OpenCVDemo/bin/classes/com/example/opencvdemo/R$styleable.class , 636
OpenCVDemo/bin/classes/com/example/opencvdemo/R.class , 723
OpenCVDemo/bin/classes/org/opencv/R$attr.class , 401
OpenCVDemo/bin/classes/org/opencv/R$id.class , 414
OpenCVDemo/bin/classes/org/opencv/R$styleable.class , 592
OpenCVDemo/bin/classes/org/opencv/R.class , 393
OpenCVDemo/bin/R.txt , 890
OpenCVDemo/gen/com/example/opencvdemo/BuildConfig.java , 164
OpenCVDemo/gen/com/example/opencvdemo/R.java , 6116
OpenCVDemo/gen/org/opencv/R.java , 777
OpenCVDemo/ic_launcher-web.png , 51394
OpenCVDemo/jni/Android.mk , 519
OpenCVDemo/jni/OpenCVDemo.cpp , 18
OpenCVDemo/libs/android-support-v4.jar , 1294803
OpenCVDemo/proguard-project.txt , 781
OpenCVDemo/project.properties , 625
OpenCVDemo/res/drawable-hdpi/ic_launcher.png , 7658
OpenCVDemo/res/drawable-hdpi/red.png , 11176
OpenCVDemo/res/drawable-mdpi/ic_launcher.png , 3777
OpenCVDemo/res/drawable-xhdpi/ic_launcher.png , 12516
OpenCVDemo/res/drawable-xxhdpi/ic_launcher.png , 24777
OpenCVDemo/res/layout/activity_main.xml , 2746
OpenCVDemo/res/values/dimens.xml , 220
OpenCVDemo/res/values/strings.xml , 227
OpenCVDemo/res/values/styles.xml , 695
OpenCVDemo/src/com/example/opencvdemo/HistUtils.java , 1086
OpenCVDemo/src/com/example/opencvdemo/MainActivity.java , 5783
最后
以上就是体贴香水为你收集整理的opencv android 识别代码,android opencv 图像识别 源码下载的全部内容,希望文章能够帮你解决opencv android 识别代码,android opencv 图像识别 源码下载所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复