Android-OpenCV调用CvSmooth实现高斯模糊


项目配置很乱,Makefile啥的不写了,直接代码

  1. MainActivity:  
  2.   
  3.   public void onCreate(Bundle savedInstanceState) {  
  4.   
  5.         super.onCreate(savedInstanceState);  
  6.   
  7.         //bitmapView=new BitmapView(this);   
  8.   
  9.         BitmapFactory.Options option = new BitmapFactory.Options();  
  10.   
  11.         option.inPreferredConfig = Config.ARGB_8888;  
  12.   
  13.         setContentView(R.layout.main);  
  14.   
  15.         bmp = (BitmapFactory.decodeResource(this.getResources(),  
  16.   
  17.                 R.drawable.bitmap1, option));  
  18.   
  19.         imageView=(ImageView)this.findViewById(R.id.imageView1);  
  20.   
  21.         imageView.setImageBitmap(bmp);  
  22.   
  23.         int width=bmp.getWidth();  
  24.   
  25.         int height=bmp.getHeight();  
  26.   
  27.         int[] src=new int[width*height];  
  28.   
  29.         int[] dst=new int[width*height];  
  30.   
  31.         Log.d("图片大小", width+"  "+height);  
  32.   
  33.         bmp.getPixels(src,0, width, 00, width, height);  
  34.   
  35.         Smooth(src,dst,width,height);  
  36.   
  37.         bmp.setPixels(dst,0, width,00, width, height);  
  38.   
  39.         imageView.setImageBitmap(bmp);  
  40.   
  41.     }  
  42.   
  43.     static{  
  44.   
  45.         System.loadLibrary("ImageProcess");  
  46.   
  47.     }  
  48.   
  49.     public native void Smooth(int[] src,int[] dst,int width,int height);  

 
  1. ImageProcess.c:  
  2.   
  3. #include <string.h>  
  4.   
  5. #include <jni.h>  
  6.   
  7. #include <opencv2/core/core.hpp>  
  8.   
  9. #include <opencv2/imgproc/imgproc.hpp>  
  10.   
  11. #include <opencv2/features2d/features2d.hpp>  
  12.   
  13. #include <cv.h>  
  14.   
  15. using namespace std;  
  16.   
  17. using namespace cv;  
  18.   
  19. extern "C" {  
  20.   
  21. JNIEXPORT void Java_com_imageprocess_MainActivity_Smooth(JNIEnv* env,  
  22.   
  23.         jobject thiz, jintArray javaIntArray0,jintArray dstArray,jint width, jint height) {  
  24.   
  25.     jint* src = env->GetIntArrayElements(javaIntArray0, 0);  
  26.   
  27.     jint* dst=env->GetIntArrayElements(dstArray,0);  
  28.   
  29.     Mat srcMat(Size(width,height),CV_8UC4,(unsigned char*)src);  
  30.   
  31.     Mat dstMat(Size(width,height),CV_8UC4,(unsigned char*)dst);  
  32.   
  33.     CvMat cvsrcMat=srcMat;  
  34.   
  35.     CvMat cvdstMat=dstMat;  
  36.   
  37.     cvSmooth(&cvsrcMat,&cvdstMat, CV_GAUSSIAN, 11, 0, 0, 0);  
  38.   
  39.     env->ReleaseIntArrayElements(javaIntArray0, src, 0);  
  40.   
  41.     env->ReleaseIntArrayElements(dstArray, dst, 0);  
  42.   
  43. }  
  44.   
  45. }  

高斯模糊效果

效果截图:

相关内容