- 下载 OpenCV-3.0.0-android-sdk-1.zip
- 打开 intellj,新建立一个 android 工程后选择工程属性,导入模块(Import module from external model" with Eclipse option),选择文件夹 OpenCV-3.0.0-android-sdk\sdk\java
- 设置工程依赖
package com.example.testopencv;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.view.View;
import android.widget.Button;
import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.android.Utils;
import org.opencv.core.Mat;
import org.opencv.core.Point;
import org.opencv.core.Scalar;
import android.os.Bundle;
import android.widget.ImageView;
import android.app.Activity;
import android.graphics.Bitmap;
import org.opencv.imgproc.Imgproc;
import java.io.FileNotFoundException;
import java.io.InputStream;
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
private static final int SELECT_PHOTO = 100;
private Button selectbt;
private Button processbt;
private Bitmap image;
private ImageView iv;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
selectbt = (Button) findViewById(R.id.button);
processbt = (Button) findViewById(R.id.start);
iv = (ImageView) findViewById(R.id.imageView);
//选择图片
selectbt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);
}
});
processbt.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_0_0, MyActivity.this, mLoaderCallback);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch(requestCode) {
case SELECT_PHOTO:
if(resultCode == RESULT_OK){
Uri selectedImage = imageReturnedIntent.getData();
InputStream imageStream = null;
try {
imageStream = getContentResolver().openInputStream(selectedImage);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
image = BitmapFactory.decodeStream(imageStream);
}
}
}
//process button
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
@Override
public void onManagerConnected(int status) {
if (status == LoaderCallbackInterface.SUCCESS ) {
mainProcess();
} else {
super.onManagerConnected(status);
}
}
};
// 增加文字在图像上
private void mainProcess() {
Mat m = new Mat();
Utils.bitmapToMat(this.image, m);
Imgproc.putText(m, "=====OPENCV PUTTEXT TEST=====", new Point(20, 40), 3, 1, new Scalar(0, 255, 0, 255), 2);
Bitmap bm = Bitmap.createBitmap(m.cols(), m.rows(), Bitmap.Config.ARGB_8888);
Utils.matToBitmap(m, bm);
iv.setImageBitmap(bm);
}
@Override
protected void onResume() {
super.onResume();
}
}
增加权限
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.testopencv"
android:versionCode="1"
android:versionName="1.0">
<uses-sdk android:minSdkVersion="17"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application android:label="@string/app_name" android:icon="@drawable/ic_launcher">
<activity android:name="MyActivity"
android:label="@string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
</manifest>
ref
http://answers.opencv.org/question/16993/display-image/
APK download
http://pan.baidu.com/s/1ntN9KBv