android之NDK C++图像处理

来源:互联网 发布:centos 桥接网络配置 编辑:程序博客网 时间:2024/06/05 15:22

先来简单地介绍一下Android的NDK使用步骤:

以NDK r4为例,或许以后新版的NDK的使用方法略有不同。
1、下载支持C++的android-ndk-r4-crystax,支持C++的话可玩性更强......
2、下载cygwin,选择ftp://mirrors.kernel.org这个镜像,搜索  Devel Install 安装 gcc 和 make 等工具;

0_12930845526d5j.gif 
2012-3-10 20:42 上传
下载附件 (5.02 KB)

在搜索框里分别搜索gcc和make,必须是 Devel Install 栏的。

3、Cygwin安装目录下,找到home/username的目录下的.bash_profile文件,打开文件在最后加上:
    NDK=/cygdrive/d:cygwin/android-ndk-r4-crystax 
   export NDK
PS:假设安装在D:/cygwin/android-ndk-r4-crystax。
4、运行cygwin,通过cd命令去到NDK/samples/例子目录/,运行$NDK/ndk-build来编译该目录下的Android.mk

以下是个人习惯.......
5、安装Eclipse的CDT,官方下载cdt安装包,解压缩后把plugins和feagures 复制覆盖到eclipse文件夹下即可
6、去到系统属性->环境变量->Path添加"D:/cygwin/bin"(假设cygwin安装在D:下)和"D:/cygwin/android-ndk-r4-crystax",重启计算机,然后就可以在Eclipse里面建立基于cygwin的C/C++工程了,先通过这一步来验证NDK的程序能够编译成功,然后再通过第4步来生成SO文件。

接下来看看本文程序运行的效果:

0_1293084341YZ03.gif


2012-3-10 20:42 上传
下载附件 (121.27 KB) 

从转换灰度图的耗时来说,NDK的确比JAVA所用的时间短不少。

main.xml源码如下:

  1. <?xml version="1.0" encoding="utf-8" ?> 
  2. - <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent">
  3.   <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btnJAVA" android:text="使用JAVA转换灰度图" /> 
  4.   <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btnNDK" android:text="使用NDK转换灰度图" /> 
  5.   <ImageView android:id="@+id/ImageView01" android:layout_width="fill_parent" android:layout_height="fill_parent" /> 
  6.   </LinearLayout>

主程序testToGray.java的源码如下:

  1. package com.testToGray;
  2. import android.app.Activity;
  3. import android.graphics.Bitmap;
  4. import android.graphics.Bitmap.Config;
  5. import android.graphics.drawable.BitmapDrawable;
  6. import android.os.Bundle;
  7. import android.view.View;
  8. import android.widget.Button;
  9. import android.widget.ImageView;
  10. public class testToGray extends Activity {
  11.     /** Called when the activity is first created. */
  12.         Button btnJAVA,btnNDK;
  13.         ImageView imgView;
  14.     @Override
  15.     public void onCreate(Bundle savedInstanceState) {
  16.         super.onCreate(savedInstanceState);
  17.         setContentView(R.layout.main);
  18.         this.setTitle("使用NDK转换灰度图---hellogv");
  19.         btnJAVA=(Button)this.findViewById(R.id.btnJAVA);
  20.         btnJAVA.setOnClickListener(new ClickEvent());
  21.         
  22.         btnNDK=(Button)this.findViewById(R.id.btnNDK);
  23.         btnNDK.setOnClickListener(new ClickEvent());
  24.         imgView=(ImageView)this.findViewById(R.id.ImageView01);
  25.     }
  26.     class ClickEvent implements View.OnClickListener{
  27.                 @Override
  28.                 public void onClick(View v) {
  29.                         if(v==btnJAVA)
  30.                         {
  31.                                 long current=System.currentTimeMillis();
  32.                                 Bitmap img=ConvertGrayImg(R.drawable.cat);
  33.                                 long performance=System.currentTimeMillis()-current;
  34.                                 //显示灰度图
  35.                                 imgView.setImageBitmap(img);
  36.                                 testToGray.this.setTitle("w:"+String.valueOf(img.getWidth())+",h:"+String.valueOf(img.getHeight())
  37.                                                 +" JAVA耗时 "+String.valueOf(performance)+" 毫秒");
  38.                         }
  39.                         else if(v==btnNDK)
  40.                         {
  41.                                 long current=System.currentTimeMillis();
  42.                                 
  43.                                 //先打开图像并读取像素
  44.                                 Bitmap img1=((BitmapDrawable) getResources().getDrawable(R.drawable.cat)).getBitmap();
  45.                                 int w=img1.getWidth(),h=img1.getHeight();
  46.                         int[] pix = new int[w * h];
  47.                         img1.getPixels(pix, 0, w, 0, 0, w, h);
  48.                         //通过ImgToGray.so把彩色像素转为灰度像素
  49.                         int[] resultInt=LibFuns.ImgToGray(pix, w, h);
  50.                         Bitmap resultImg=Bitmap.createBitmap(w, h, Config.RGB_565);
  51.                         resultImg.setPixels(resultInt, 0, w, 0, 0,w, h);
  52.                         long performance=System.currentTimeMillis()-current;
  53.                         //显示灰度图
  54.                                 imgView.setImageBitmap(resultImg);
  55.                                 testToGray.this.setTitle("w:"+String.valueOf(img1.getWidth())+",h:"+String.valueOf(img1.getHeight())
  56.                                                 +" NDK耗时 "+String.valueOf(performance)+" 毫秒");
  57.                         }
  58.                 }
  59.     }
  60.     
  61.     /**
  62.      * 把资源图片转为灰度图
  63.      * @param resID 资源ID
  64.      * @return
  65.      */
  66.     public Bitmap ConvertGrayImg(int resID)
  67.     {
  68.         Bitmap img1=((BitmapDrawable) getResources().getDrawable(resID)).getBitmap();
  69.         
  70.         int w=img1.getWidth(),h=img1.getHeight();
  71.         int[] pix = new int[w * h];
  72.         img1.getPixels(pix, 0, w, 0, 0, w, h);
  73.         
  74.         int alpha=0xFF<<24;
  75.         for (int i = 0; i < h; i++) {  
  76.             for (int j = 0; j < w; j++) {  
  77.                     // 获得像素的颜色  
  78.                 int color = pix[w * i + j];  
  79.                 int red = ((color & 0x00FF0000) >> 16);  
  80.                 int green = ((color & 0x0000FF00) >> 8);  
  81.                 int blue = color & 0x000000FF;  
  82.                 color = (red + green + blue)/3;  
  83.                 color = alpha | (color << 16) | (color << 8) | color;  
  84.                 pix[w * i + j] = color;
  85.             }
  86.         }
  87.         Bitmap result=Bitmap.createBitmap(w, h, Config.RGB_565);
  88.         result.setPixels(pix, 0, w, 0, 0,w, h);
  89.         return result;
  90.     }
  91. }

封装NDK函数的JAVA类LibFuns.java的源码如下:

  1. package com.testToGray;
  2. public class LibFuns {
  3.         static {
  4.         System.loadLibrary("ImgToGray");
  5.     }
  6.    /**
  7.     * @param width the current view width
  8.     * @param height the current view height
  9.     */
  10.     
  11.     public static native int[] ImgToGray(int[] buf, int w, int h);
  12. }
彩图转换为灰度图的ImgToGray.cpp源码:
  1. #include 
  2. #include 
  3. #include 
  4. extern "C" {
  5. JNIEXPORT jintArray JNICALL Java_com_testToGray_LibFuns_ImgToGray(
  6.                 JNIEnv* env, jobject obj, jintArray buf, int w, int h);
  7. }
  8. ;
  9. JNIEXPORT jintArray JNICALL Java_com_testToGray_LibFuns_ImgToGray(
  10.                 JNIEnv* env, jobject obj, jintArray buf, int w, int h) {
  11.         jint *cbuf;
  12.         cbuf = env->GetIntArrayElements(buf, false);
  13.         if (cbuf == NULL) {
  14.                 return 0; /* exception occurred */
  15.         }
  16.         int alpha = 0xFF << 24;
  17.         for (int i = 0; i < h; i++) {
  18.                 for (int j = 0; j < w; j++) {
  19.                         // 获得像素的颜色
  20.                         int color = cbuf[w * i + j];
  21.                         int red = ((color & 0x00FF0000) >> 16);
  22.                         int green = ((color & 0x0000FF00) >> 8);
  23.                         int blue = color & 0x000000FF;
  24.                         color = (red + green + blue) / 3;
  25.                         color = alpha | (color << 16) | (color << 8) | color;
  26.                         cbuf[w * i + j] = color;
  27.                 }
  28.         }
  29.         int size=w * h;
  30.         jintArray result = env->NewIntArray(size);
  31.         env->SetIntArrayRegion(result, 0, size, cbuf);
  32.         env->ReleaseIntArrayElements(buf, cbuf, 0);
  33.         return result;
  34. }
Android.mk的源码:
  1. LOCAL_PATH:= $(call my-dir)
  2. include $(CLEAR_VARS)
  3. LOCAL_MODULE    := ImgToGray
  4. LOCAL_SRC_FILES := ImgToGray.cpp
  5. include $(BUILD_SHARED_LIBRARY)
0 0
原创粉丝点击