Android提高十六篇之使用NDK把彩圖轉換灰階圖

來源:互聯網
上載者:User

 在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)  
            {  
             &

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.