看看效果图,如果运行时提示需要安装xxxx.mamager,那么就去现在Opencvforandroid,解压以后安装相应的manager安装包就好了
这就是运行的效果,关于jni的调用就不说了,就看下他改变不同效果图的代码
package com.example.opencvdemo1;
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.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.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import com.example.openjni.ImageProc;
public class MainActivity extends Activity implements OnClickListener{
private Button mBtn_type1,mBtn_type2,mBtn_type3,mBtn_type4,mBtn_type5,mBtn_type6,mBtn_type7,mBtn_src;
private ImageView mIv_src;
private Bitmap mDes_Bitmap;
private boolean isGray = false;
private BaseLoaderCallback loaderCallback = new BaseLoaderCallback(this) {
public void onManagerConnected(int status) {
switch (status) {
case LoaderCallbackInterface.SUCCESS:
System.loadLibrary("image_proc");
break;
case LoaderCallbackInterface.INIT_FAILED:
break;
case LoaderCallbackInterface.INSTALL_CANCELED:
break;
case LoaderCallbackInterface.MARKET_ERROR:
break;
case LoaderCallbackInterface.INCOMPATIBLE_MANAGER_VERSION:
break;
default:
super.onManagerConnected(status);
break;
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
}
public void init(){
mBtn_type1 = (Button)findViewById(R.id.btn_type1);
mBtn_type2 = (Button)findViewById(R.id.btn_type2);
mBtn_type3 = (Button)findViewById(R.id.btn_type3);
mBtn_type4 = (Button)findViewById(R.id.btn_type4);
mBtn_type5 = (Button)findViewById(R.id.btn_type5);
mBtn_type6 = (Button)findViewById(R.id.btn_type6);
mBtn_type7 = (Button)findViewById(R.id.btn_type7);
mBtn_src = (Button)findViewById(R.id.btn_src);
mIv_src = (ImageView)findViewById(R.id.iv_img);
mDes_Bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.a);
mIv_src.setImageBitmap(mDes_Bitmap);
mBtn_type1.setOnClickListener(this);
mBtn_type2.setOnClickListener(this);
mBtn_type3.setOnClickListener(this);
mBtn_type4.setOnClickListener(this);
mBtn_src.setOnClickListener(this);
mBtn_type5.setOnClickListener(this);
mBtn_type6.setOnClickListener(this);
mBtn_type7.setOnClickListener(this);
}
public Bitmap procSrc2GrayJni(){
int w = mDes_Bitmap.getWidth();
int h = mDes_Bitmap.getHeight();
int [] pixels = new int [w*h];
mDes_Bitmap.getPixels(pixels, 0, w, 0, 0, w, h);
int[] resultInt = ImageProc.grayProc(pixels, w, h);
Bitmap bitmap = Bitmap.createBitmap(w, h, Config.ARGB_8888);
bitmap.setPixels(resultInt, 0, w, 0, 0, w, h);
return bitmap;
}
public Bitmap procSrc2Gray(int type){
Mat rgbMat = new Mat();
Mat grayMat = new Mat();
Bitmap grayBitmap = Bitmap.createBitmap(mDes_Bitmap.getWidth(), mDes_Bitmap.getHeight(), Config.RGB_565);
Utils.bitmapToMat(mDes_Bitmap, rgbMat);//convert original bitmap to Mat, R G B.
Imgproc.cvtColor(rgbMat, grayMat,type);//rgbMat to gray grayMat
Utils.matToBitmap(grayMat, grayBitmap); //convert mat to bitmap
Log.i("TAG", "procSrc2Gray sucess...");
return grayBitmap;
}
@Override
protected void onResume() {
super.onResume();
OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_9,this, loaderCallback);
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btn_type1:
mIv_src.setImageBitmap(procSrc2Gray(Imgproc.COLOR_RGB2GRAY));
break;
case R.id.btn_type2:
mIv_src.setImageBitmap(procSrc2Gray(Imgproc.COLOR_RGB2HLS));
break;
case R.id.btn_type3:
mIv_src.setImageBitmap(procSrc2Gray(Imgproc.COLOR_RGB2HSV));
break;
case R.id.btn_type4:
Log.e("TAG", "procSrc2Gray sucess...");
mIv_src.setImageBitmap(procSrc2Gray(Imgproc.COLOR_RGB2Lab));
break;
case R.id.btn_type5:
mIv_src.setImageBitmap(procSrc2Gray(Imgproc.COLOR_RGB2Luv));
break;
case R.id.btn_type6:
mIv_src.setImageBitmap(procSrc2Gray(Imgproc.COLOR_RGB2XYZ));
break;
case R.id.btn_type7:
mIv_src.setImageBitmap(procSrc2Gray(Imgproc.COLOR_RGB2YUV));
break;
case R.id.btn_src:
mIv_src.setImageBitmap(mDes_Bitmap);
break;
default:
break;
}
}
}
Android openCv
Imgproc :该类是用来对图片进行处理的一个类,你可以让图片改变成你想要的样子,变成灰色的,或是素描,或是底片类型,等等,都可以。
他是通过jni调用来让我们可以在android里边能用openCv的功能。
private static native void Canny_0(long image_nativeObj, long edges_nativeObj, double threshold1, double threshold2, int apertureSize, boolean L2gradient);
这个方法是用来找到图片边缘的。然后可以让你转换为一个类似描边的图像。里边的参数可以看到,第一个参数是要转换的图片的矩阵,第二个参数是转换以后的矩阵,第三个是阀值,第四个越是,你可以自己改变他俩个的大小看看效果,越大边缘月少。第五个参数其实也是调整显示的时候边缘化的程度了好像,我试验了一下以后
这些参数的大小在填的时候需要自己调整好,最后一个参数是用来判断是否要更准确一点。
private static native void Canny_1(long image_nativeObj, long edges_nativeObj, double threshold1, double threshold2);
这个跟上边的类似。
private static native void GaussianBlur_0(long src_nativeObj, long dst_nativeObj, double ksize_width, double ksize_height, double sigmaX, double sigmaY, int borderType);
高斯滤波器来对图片进行模糊化。第一个参数是图片原型矩阵,第二个是转换后输出矩阵,第三个为高斯滤波器模板的宽,第四个为高斯滤波器模板的高,第五第六sigmaX和sigmaY分别为高斯滤波在横线和竖向的滤波系数,borderType为边缘点插值类型。对于这个方法的使用参考http://blog.csdn.net/vblittleboy/article/details/9187447
private static native void GaussianBlur_1(long src_nativeObj, long dst_nativeObj, double ksize_width, double ksize_height, double sigmaX, double sigmaY);
private static native void GaussianBlur_2(long src_nativeObj, long dst_nativeObj, double ksize_width, double ksize_height, double sigmaX);
同上边
private static native void HoughCircles_0(long image_nativeObj, long circles_nativeObj, int method, double dp, double minDist, double param1, double param2, int minRadius, int maxRadius);
private static native void HoughCircles_1(long image_nativeObj, long circles_nativeObj, int method, double dp, double minDist);
/**
*高斯模糊处理
* @return
*/
public Bitmap gaosiBlur(){
Mat rgbMat = new Mat();
Mat grayMat = new Mat();
Bitmap gaosiBitmap = Bitmap.createBitmap(mDes_Bitmap.getWidth(), mDes_Bitmap.getHeight(), Config.ARGB_8888);
Utils.bitmapToMat(mDes_Bitmap, rgbMat);//convert original bitmap to Mat, R G B.
Size size = new Size(); //进行高斯模糊处理
size.height = 17;
size.width = 17;
Imgproc.GaussianBlur(rgbMat, grayMat, size, 2, 2, Imgproc.BORDER_DEFAULT);
Utils.matToBitmap(grayMat, gaosiBitmap); //convert mat to bitmap
Log.i("TAG", "procSrc2Gray sucess...");
return gaosiBitmap;
}
public Bitmap getwarpPerspective(){
Mat srcMat = new Mat();
Mat dstMat = new Mat();
Mat perspectiveTransform = Imgproc.getPerspectiveTransform(srcMat, dstMat);
Size size = new Size(); //进行高斯模糊处理
size.height = mDes_Bitmap.getHeight();
size.width = mDes_Bitmap.getWidth();
Bitmap dstBitmap = Bitmap.createBitmap(mDes_Bitmap.getWidth(), mDes_Bitmap.getHeight(), Config.RGB_565);
Utils.bitmapToMat(mDes_Bitmap, srcMat);
Core.perspectiveTransform(srcMat, dstMat, perspectiveTransform);
Imgproc.warpPerspective(srcMat, dstMat, perspectiveTransform, size);
Utils.matToBitmap(dstMat, dstBitmap);
return dstBitmap;
}
/**
* 进行边缘检测
* @return
*/
public Bitmap getCanny(){
Mat rgbMat = new Mat();
Mat grayMat = new Mat();
Bitmap grayBitmap = Bitmap.createBitmap(mDes_Bitmap.getWidth(), mDes_Bitmap.getHeight(), Config.ARGB_8888);
Utils.bitmapToMat(mDes_Bitmap, rgbMat);//convert original bitmap to Mat, R G B.
Imgproc.Canny(rgbMat, grayMat,0,150); //对图片进行边缘检测
Utils.matToBitmap(grayMat, grayBitmap); //convert mat to bitmap
Log.i("TAG", "procSrc2Gray sucess...");
return grayBitmap;
}
Demo: http://download.csdn.net/detail/u012808234/9374169