当前位置:操作系统 > 安卓/Android >>

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

 在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/android-ndk-r4-windows/android-ndk-r4
   export NDK
PS:假设安装在d:android-ndk-r4-windowsandroid-ndk-r4。
4、运行cygwin,通过cd命令去到NDKsamples例子目录\,运行$NDK/ndk-build来编译该目录下的Android.mk

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

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

\

 

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

main.xml源码如下:

view plaincopy to clipboardprint?
<?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/btnJAVA" android:text="使用JAVA转换灰度图" />   
  <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btnNDK" android:text="使用NDK转换灰度图" />   
  <ImageView android:id="@+id/ImageView01" android:layout_width="fill_parent" android:layout_height="fill_parent" />   
  </LinearLayout> 
<?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/btnJAVA" android:text="使用JAVA转换灰度图" />
  <Button android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/btnNDK" android:text="使用NDK转换灰度图" />
  <ImageView android:id="@+id/ImageView01" android:layout_width="fill_parent" android:layout_height="fill_parent" />
  </LinearLayout>

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

view plaincopy to clipboardprint?
package com.testToGray;  
 
import android.app.Activity;  
import android.graphics.Bitmap;  
import android.graphics.Bitmap.Config;  
import android.graphics.drawable.BitmapDrawable;  
import android.os.Bundle;  
import android.view.View;  
import android.widget.Button;  
import android.widget.ImageView;  
 
public class testToGray extends Activity {  
    /** Called when the activity is first created. */ 
    Button btnJAVA,btnNDK;  
    ImageView imgView;  
    @Override 
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        this.setTitle("使用NDK转换灰度图---hellogv");  
        btnJAVA=(Button)this.findViewById(R.id.btnJAVA);  
        btnJAVA.setOnClickListener(new ClickEvent());  
          
        btnNDK=(Button)this.findViewById(R.id.btnNDK);  
        btnNDK.setOnClickListener(new ClickEvent());  
        imgView=(ImageView)this.findViewById(R.id.ImageView01);  
    }  
    class ClickEvent implements View.OnClickListener{  
 
        @Override 
        public void onClick(View v) {  
            if(v==btnJAVA)  
            {  
                long current=System.currentTimeMillis();  
                Bitmap img=ConvertGrayImg(R.drawable.cat);  
                long performance=System.currentTimeMillis()-current;  
                //显示灰度图  
                imgView.setImageBitmap(img);  
                testToGray.this.setTitle("w:"+String.valueOf(img.getWidth())+",h:"+String.valueOf(img.getHeight())  
                        +" JAVA耗时 "+String.valueOf(performance)+" 毫秒");  
            }  
            else if(v==btnNDK)  
            {  
             &

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,