亚洲免费在线-亚洲免费在线播放-亚洲免费在线观看-亚洲免费在线观看视频-亚洲免费在线看-亚洲免费在线视频

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

系統 1813 0
http://blog.csdn.net/hellogv/archive/2010/12/23/6094127.aspx
?????? 在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文件。

接下來看看本文程序運行的效果:

Android提高十六篇之使用NDK把彩圖轉換灰度圖
從轉換灰度圖的耗時來說,NDK的確比JAVA所用的時間短不少。

main.xml源碼如下:
    
<?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的源碼如下:
    
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)  
            {  
                long current=System.currentTimeMillis();  
                  
                //先打開圖像并讀取像素  
                Bitmap img1=((BitmapDrawable) getResources().getDrawable(R.drawable.cat)).getBitmap();  
                int w=img1.getWidth(),h=img1.getHeight();  
                int[] pix = new int[w * h];  
                img1.getPixels(pix, 0, w, 0, 0, w, h);  
                //通過ImgToGray.so把彩色像素轉為灰度像素  
                int[] resultInt=LibFuns.ImgToGray(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);  
                testToGray.this.setTitle("w:"+String.valueOf(img1.getWidth())+",h:"+String.valueOf(img1.getHeight())  
                        +" NDK耗時 "+String.valueOf(performance)+" 毫秒");  
            }  
        }  
    }  
      
    /** 
     * 把資源圖片轉為灰度圖 
     * @param resID 資源ID 
     * @return 
     */  
    public Bitmap ConvertGrayImg(int resID)  
    {  
        Bitmap img1=((BitmapDrawable) getResources().getDrawable(resID)).getBitmap();  
          
        int w=img1.getWidth(),h=img1.getHeight();  
        int[] pix = new int[w * h];  
        img1.getPixels(pix, 0, w, 0, 0, w, h);  
          
        int alpha=0xFF<<24;  
        for (int i = 0; i < h; i++) {    
            for (int j = 0; j < w; j++) {    
                // 獲得像素的顏色    
                int color = pix[w * i + j];    
                int red = ((color & 0x00FF0000) >> 16);    
                int green = ((color & 0x0000FF00) >> 8);    
                int blue = color & 0x000000FF;    
                color = (red + green + blue)/3;    
                color = alpha | (color << 16) | (color << 8) | color;    
                pix[w * i + j] = color;  
            }  
        }  
        Bitmap result=Bitmap.createBitmap(w, h, Config.RGB_565);  
        result.setPixels(pix, 0, w, 0, 0,w, h);  
        return result;  
    }  
}  

  

封裝NDK函數的JAVA類LibFuns.java的源碼如下:
    
package com.testToGray;  
public class LibFuns {  
    static {  
        System.loadLibrary("ImgToGray");  
    }  
   /** 
    * @param width the current view width 
    * @param height the current view height 
    */  
      
    public static native int[] ImgToGray(int[] buf, int w, int h);  
}  

  

彩圖轉換為灰度圖的ImgToGray.cpp源碼:
    
#include <jni.h>  
#include <stdio.h>  
#include <stdlib.h>  
extern "C" {  
JNIEXPORT jintArray JNICALL Java_com_testToGray_LibFuns_ImgToGray(  
        JNIEnv* env, jobject obj, jintArray buf, int w, int h);  
}  
;  
JNIEXPORT jintArray JNICALL Java_com_testToGray_LibFuns_ImgToGray(  
        JNIEnv* env, jobject obj, jintArray buf, int w, int h) {  
    jint *cbuf;  
    cbuf = env->GetIntArrayElements(buf, false);  
    if (cbuf == NULL) {  
        return 0; /* exception occurred */  
    }  
    int alpha = 0xFF << 24;  
    for (int i = 0; i < h; i++) {  
        for (int j = 0; j < w; j++) {  
            // 獲得像素的顏色  
            int color = cbuf[w * i + j];  
            int red = ((color & 0x00FF0000) >> 16);  
            int green = ((color & 0x0000FF00) >> 8);  
            int blue = color & 0x000000FF;  
            color = (red + green + blue) / 3;  
            color = alpha | (color << 16) | (color << 8) | color;  
            cbuf[w * i + j] = color;  
        }  
    }  
    int size=w * h;  
    jintArray result = env->NewIntArray(size);  
    env->SetIntArrayRegion(result, 0, size, cbuf);  
    env->ReleaseIntArrayElements(buf, cbuf, 0);  
    return result;  
}  

  

Android.mk的源碼:
    
LOCAL_PATH:= $(call my-dir)  
include $(CLEAR_VARS)  
LOCAL_MODULE    := ImgToGray  
LOCAL_SRC_FILES := ImgToGray.cpp  
include $(BUILD_SHARED_LIBRARY)  

  

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


更多文章、技術交流、商務合作、聯系博主

微信掃碼或搜索:z360901061

微信掃一掃加我為好友

QQ號聯系: 360901061

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描下面二維碼支持博主2元、5元、10元、20元等您想捐的金額吧,狠狠點擊下面給點支持吧,站長非常感激您!手機微信長按不能支付解決辦法:請將微信支付二維碼保存到相冊,切換到微信,然后點擊微信右上角掃一掃功能,選擇支付二維碼完成支付。

【本文對您有幫助就好】

您的支持是博主寫作最大的動力,如果您喜歡我的文章,感覺我的文章對您有幫助,請用微信掃描上面二維碼支持博主2元、5元、10元、自定義金額等您想捐的金額吧,站長會非常 感謝您的哦!!!

發表我的評論
最新評論 總共0條評論
主站蜘蛛池模板: 久久精品免视看国产成人2021 | 免费网站看v片在线成人国产系列 | 国产精品亚洲综合久久 | 欧美一级毛片久久精品 | se色综合视频 | 国产精品视频偷伦精品视频 | 欧美在线一区二区三区精品 | 国产69精品久久久久99不卡 | 深夜免费看 | 亚洲欧美精品中字久久99 | 国内高清久久久久久久久 | 色爱区综合激情五月综合激情 | 91成人午夜精品福利院在线观看 | 四虎永久在线精品国产 | 性做久久久久久免费观看 | 色综合久久久 | 在线不卡日本 | 国产综合亚洲欧美日韩一区二区 | 奇米影视第四色首页 | 99国产福利 | 婷婷成人综合 | 一级黄色毛片免费看 | 婷婷综合社区 | 亚洲精品人成网线在线 | 全黄冷激性性视频 | japanese乱子另类 | 国产日产欧美精品一区二区三区 | 国产一级淫 | 亚洲香蕉久久一区二区 | 最新香蕉97超级碰碰碰碰碰久 | 青青青视频精品中文字幕 | 日韩经典欧美一区二区三区 | 一本久久a久久精品vr综合 | 成年人一级毛片 | 九九在线偷拍视频在线播放 | 欧美日韩视频在线播放 | 一本伊大人香蕉高清在线观看 | 91亚洲欧美 | 亚洲欧美小视频 | 97综合久久| 久久99精品久久久久久黑人 |