OpenCV4Android开发之旅(一)----OpenCV2.4简介及 app通过Java接口调用OpenCV的示例
转自: http://blog.csdn.net/yanzi1225627/article/details/16917961
开发环境:windows+ADT Bundle+CDT+OpenCV-2.4.4-android-sdk
一、OpenCV2.4简介
二、Android APP通过Java接口调OpenCV
1、配置
2、新建项目并引入上面的jar包
3、一个完成的demo:将一个图片灰度化
- <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
- 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"
- tools:context=".MainActivity" >
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_alignParentTop="true"
- android:text="@string/wellcome_words" />
- <ImageView
- android:id="@+id/img_huaishi"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:background="@drawable/nanhuaijin"
- android:layout_centerInParent="true"/>
- <Button
- android:id="@+id/btn_gray_process"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_below="@id/img_huaishi"
- android:layout_centerHorizontal="true"
- android:text="灰度化"/>"
- </RelativeLayout>
- package com.example.grayprocess;
- 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;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.Config;
- import android.graphics.BitmapFactory;
- import android.os.Bundle;
- import android.os.Handler;
- import android.util.Log;
- import android.view.Menu;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.ImageView;
- public class MainActivity extends Activity {
- Button btnProcess;
- Bitmap srcBitmap;
- Bitmap grayBitmap;
- ImageView imgHuaishi;
- private static boolean flag = true;
- private static boolean isFirst = true;
- private static final String TAG = "MainActivity";
- //OpenCV库加载并初始化成功后的回调函数
- private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
- @Override
- public void onManagerConnected(int status) {
- // TODO Auto-generated method stub
- switch (status){
- case BaseLoaderCallback.SUCCESS:
- Log.i(TAG, "成功加载");
- break;
- default:
- super.onManagerConnected(status);
- Log.i(TAG, "加载失败");
- break;
- }
- }
- };
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initUI();
- btnProcess.setOnClickListener(new ProcessClickListener());
- }
- @Override
- public boolean onCreateOptionsMenu(Menu menu) {
- // Inflate the menu; this adds items to the action bar if it is present.
- getMenuInflater().inflate(R.menu.main, menu);
- return true;
- }
- public void initUI(){
- btnProcess = (Button)findViewById(R.id.btn_gray_process);
- imgHuaishi = (ImageView)findViewById(R.id.img_huaishi);
- Log.i(TAG, "initUI sucess...");
- }
- public void procSrc2Gray(){
- Mat rgbMat = new Mat();
- Mat grayMat = new Mat();
- srcBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.nanhuaijin);
- grayBitmap = Bitmap.createBitmap(srcBitmap.getWidth(), srcBitmap.getHeight(), 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...");
- }
- private class ProcessClickListener implements OnClickListener{
- @Override
- public void onClick(View v) {
- // TODO Auto-generated method stub
- if(isFirst)
- {
- procSrc2Gray();
- isFirst = false;
- }
- if(flag){
- imgHuaishi.setImageBitmap(grayBitmap);
- btnProcess.setText("查看原图");
- flag = false;
- }
- else{
- imgHuaishi.setImageBitmap(srcBitmap);
- btnProcess.setText("灰度化");
- flag = true;
- }
- }
- }
- @Override
- protected void onResume() {
- // TODO Auto-generated method stub
- super.onResume();
- //load OpenCV engine and init OpenCV library
- OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_4, getApplicationContext(), mLoaderCallback);
- Log.i(TAG, "onResume sucess load OpenCV...");
- // new Handler().postDelayed(new Runnable(){
- //
- // @Override
- // public void run() {
- // // TODO Auto-generated method stub
- // procSrc2Gray();
- // }
- //
- // }, 1000);
- }
- }
- new Handler().postDelayed(new Runnable(){
- @Override
- public void run() {
- // TODO Auto-generated method stub
- procSrc2Gray();
- }
- }, 1000);
三、OpenCV网站导航
1、http://opencv.org/ 这是OpenCV的官方网站
2、http://wiki.opencv.org.cn/index.php/ 这是国内的官网老版
OpenCV4Android开发之旅(一)----OpenCV2.4简介及 app通过Java接口调用OpenCV的示例,,5-wow.com
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。