我的天!
折腾了好久终于搭建成功了第一个项目。
项目环境:
Windows 7 家庭普通版 64位
Android studio 1.5.1
OpenCV-2.4.9-android-sdk
基于Android studio上OpenCv开发环境的配置请参考
http://www.cnblogs.com/whieenz/p/5247166.html
项目内容:
很简单将一张图片灰度化。
项目源码:
xml文件源码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="com.whieenz.myapplication.MainActivity"
tools:showIn="@layout/activity_main"
android:orientation="vertical"> <ImageView
android:layout_width="match_parent"
android:layout_height="450dp"
android:id="@+id/image"
android:src="@drawable/genie" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="灰度化"
android:id="@+id/button" />
</LinearLayout>
java文件代码:
package com.whieenz.myapplication;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.imgproc.Imgproc;
public class MainActivity extends Activity { private ImageView imageView;
private Button button;
private Bitmap srcBitmap;
private Bitmap grayBitmap;
private static boolean flag = true;
private static boolean isFirst = true;
private static final String TAG = "gao_chun"; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_main); imageView = (ImageView)findViewById(R.id.image);
button = (Button)findViewById(R.id.button);
button.setOnClickListener(new MyOnClickListener()) ; } public class MyOnClickListener implements View.OnClickListener{
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(isFirst){
procSrc2Gray();
isFirst = false;
}
if(flag){
imageView.setImageBitmap(grayBitmap);
button.setText("查看原图");
flag = false;
}else{
imageView.setImageBitmap(srcBitmap);
button.setText("灰度化");
flag = true;
}
}
} @Override
protected void onResume() {
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9,getApplicationContext(),mLoaderCallback);
} private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
super.onManagerConnected(status);
switch (status){
case BaseLoaderCallback.SUCCESS:
Log.i(TAG, "成功加载");
break;
default:
super.onManagerConnected(status);
Log.i(TAG, "加载失败");
break;
} }
}; public void procSrc2Gray(){
Mat rgbMat = new Mat();
Mat grayMat = new Mat();
srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.genie);
grayBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), Bitmap.Config.RGB_565);
Utils.bitmapToMat(srcBitmap, rgbMat);//convert original bitmap to Mat, R G B.
Imgproc.cvtColor(rgbMat, grayMat, Imgproc.COLOR_RGB2GRAY);//rgbMat to gray grayMat
Utils.matToBitmap(grayMat, grayBitmap); //convert mat to bitmap
Log.i(TAG, "procSrc2Gray sucess...");
}
}
具体流程:
- 新建Android工程
- 配置opencv开发环境
- 编辑xml文件
主要是一个ImageView 和Button 控件 - 编辑java文件
- 在onResume()里添加opencvloader.initsync();
- 自定义灰度化函数pcocSrc2Gray()
- 添加Button点击事件调用灰度化函数
- 安装opencv manager到模拟器
注意事项:
- 一定要安装opencv manager到模拟器
- 提前配置好环境
项目展示: