Android提高十六篇之使用NDK把彩图转换灰度图

本文来自http://blog.csdn.net/hellogv/ ,引用必须注明出处!

       在Android上使用JAVA实现彩图转换为灰度图,跟J2ME上的实现类似,不过遇到频繁地转换或者是大图转换时,就必须使用NDK来提高速度了。本文主要通过JAVA和NDK这两种方式来分别实现彩图转换为灰度图,并给出速度的对比。

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

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

在搜索框里分别搜索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文件。

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

从转换灰度图的耗时来说,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.     } 

封装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); 

彩图转换为灰度图的ImgToGray.cpp源码:

  1. #include <jni.h> 
  2. #include <stdio.h> 
  3. #include <stdlib.h> 
  4. extern "C"
  5. JNIEXPORT jintArray JNICALL Java_com_testToGray_LibFuns_ImgToGray( 
  6.         JNIEnv* env, jobject obj, jintArray buf, int w, int h); 
  7. JNIEXPORT jintArray JNICALL Java_com_testToGray_LibFuns_ImgToGray( 
  8.         JNIEnv* env, jobject obj, jintArray buf, int w, int h) { 
  9.     jint *cbuf; 
  10.     cbuf = env->GetIntArrayElements(buf, false); 
  11.     if (cbuf == NULL) { 
  12.         return 0; /* exception occurred */ 
  13.     } 
  14.     int alpha = 0xFF << 24; 
  15.     for (int i = 0; i < h; i++) { 
  16.         for (int j = 0; j < w; j++) { 
  17.             // 获得像素的颜色 
  18.             int color = cbuf[w * i + j]; 
  19.             int red = ((color & 0x00FF0000) >> 16); 
  20.             int green = ((color & 0x0000FF00) >> 8); 
  21.             int blue = color & 0x000000FF; 
  22.             color = (red + green + blue) / 3; 
  23.             color = alpha | (color << 16) | (color << 8) | color; 
  24.             cbuf[w * i + j] = color; 
  25.         } 
  26.     } 
  27.     int size=w * h; 
  28.     jintArray result = env->NewIntArray(size); 
  29.     env->SetIntArrayRegion(result, 0, size, cbuf); 
  30.     env->ReleaseIntArrayElements(buf, cbuf, 0); 
  31.     return result; 

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
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值