2011-09-21 10:22:35
原创作品,允许转载,转载时请务必以超链接形式标明文章
原始出处 、作者信息和本声明。否则将追究法律责任。
http://underthehood.blog.51cto.com/2531780/670169
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <Button android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:id="@+id/btnNDK"
- android:text="使用C++ OpenCV进行处理" />
- <Button android:layout_height="wrap_content"
- android:layout_width="fill_parent"
- android:id="@+id/btnRestore"
- android:text="还原" />
- <ImageView android:id="@+id/ImageView01"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent" />
- </LinearLayout>
- package com.testopencv.haveimgfun;
- public class LibImgFun {
- static {
- System.loadLibrary("ImgFun");
- }
- /**
- * @param width the current view width
- * @param height the current view height
- */
- public static native int[] ImgFun(int[] buf, int w, int h);
- }
- #include <jni.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <opencv2/opencv.hpp>
- using namespace cv;
- extern "C" {
- JNIEXPORT jintArray JNICALL Java_com_testopencv_haveimgfun_LibImgFun_ImgFun(
- JNIEnv* env, jobject obj, jintArray buf, int w, int h);
- JNIEXPORT jintArray JNICALL Java_com_testopencv_haveimgfun_LibImgFun_ImgFun(
- JNIEnv* env, jobject obj, jintArray buf, int w, int h){
- jint *cbuf;
- cbuf = env->GetIntArrayElements(buf, false);
- if(cbuf == NULL)
- {
- return 0;
- }
- Mat myimg(h, w, CV_8UC4, (unsigned char*)cbuf);
- for(int j=0;j<myimg.rows/2;j++)
- {
- myimg.row(j).setTo(Scalar(0,0,0,0));
- }
- int size=w * h;
- jintArray result = env->NewIntArray(size);
- env->SetIntArrayRegion(result, 0, size, cbuf);
- env->ReleaseIntArrayElements(buf, cbuf, 0);
- return result;
- }
- }
- LOCAL_PATH := $(call my-dir)
- include $(CLEAR_VARS)
- include ../includeOpenCV.mk
- ifeq ("$(wildcard $(OPENCV_MK_PATH))","")
- #try to load OpenCV.mk from default install location
- include $(TOOLCHAIN_PREBUILT_ROOT)/user/share/OpenCV/OpenCV.mk
- else
- include $(OPENCV_MK_PATH)
- endif
- LOCAL_MODULE := ImgFun
- LOCAL_SRC_FILES := ImgFun.cpp
- include $(BUILD_SHARED_LIBRARY)
- APP_STL:=gnustl_static
- APP_CPPFLAGS:=-frtti -fexceptions
- APP_ABI:=armeabi armeabi-v7a
- package com.testopencv.haveimgfun;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.Bitmap.Config;
- import android.graphics.drawable.BitmapDrawable;
- import android.os.Bundle;
- import android.widget.Button;
- import android.view.View;
- import android.widget.ImageView;
- public class HaveImgFun extends Activity {
- /** Called when the activity is first created. */
- ImageView imgView;
- Button btnNDK, btnRestore;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- this.setTitle("使用NDK转换灰度图");
- btnRestore=(Button)this.findViewById(R.id.btnRestore);
- btnRestore.setOnClickListener(new ClickEvent());
- btnNDK=(Button)this.findViewById(R.id.btnNDK);
- btnNDK.setOnClickListener(new ClickEvent());
- imgView=(ImageView)this.findViewById(R.id.ImageView01);
- Bitmap img=((BitmapDrawable) getResources().getDrawable(R.drawable.lena)).getBitmap();
- imgView.setImageBitmap(img);
- }
- class ClickEvent implements View.OnClickListener{
- public void onClick(View v){
- if(v == btnNDK)
- {
- long current=System.currentTimeMillis();
- Bitmap img1=((BitmapDrawable) getResources().getDrawable(R.drawable.lena)).getBitmap();
- int w=img1.getWidth(),h=img1.getHeight();
- int[] pix = new int[w * h];
- img1.getPixels(pix, 0, w, 0, 0, w, h);
- int[] resultInt=LibImgFun.ImgFun(pix, w, h);
- Bitmap resultImg=Bitmap.createBitmap(w, h, Config.RGB_565);
- resultImg.setPixels(resultInt, 0, w, 0, 0,w, h);
- long performance=System.currentTimeMillis()-current;
- imgView.setImageBitmap(resultImg);
- HaveImgFun.this.setTitle("w:"+String.valueOf(img1.getWidth())+",h:"+String.valueOf(img1.getHeight())
- +" NDK耗时 "+String.valueOf(performance)+" 毫秒");
- }
- else if(v == btnRestore)
- {
- Bitmap img2=((BitmapDrawable) getResources().getDrawable(R.drawable.lena)).getBitmap();
- imgView.setImageBitmap(img2);
- HaveImgFun.this.setTitle("使用OpenCV进行图像处理");
- }
- }
- }
- }