2012-10-19 58 views
2

變灰圖像我是Android新手ndk.I已開始通過圖像處理示例012xxruckus和IBM博客學習。我正在嘗試使圖像灰白。 這裏是我使用無法使用Android ndk

所顯示

<?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" 

    > 
    <ImageView 
    android:id="@+id/gimageView1" 
    android:layout_width="400px" 
    android:src="@drawable/wallace" 
    android:layout_height="266px" 
    /> 

    <Button 
    android:id="@+id/gbutton" 
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Go Gray" 
    /> 

    <ImageView 
    android:id="@+id/gimageView2" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    /> 
</LinearLayout> 

佈局和Java代碼的XML文件的代碼

package com.example; 

import android.app.Activity; 
import android.graphics.Bitmap; 
import android.graphics.BitmapFactory; 
import android.os.Bundle; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.Button; 
import android.widget.ImageView; 

public class GrayClass extends Activity { 
    private ImageView imageView; 
     private Bitmap bitmap; 
     private Button button; 
     private Bitmap original; 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.gray); 
     original = BitmapFactory.decodeResource(getResources(), R.drawable.wallace); 
     bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.wallace); 
     button = (Button) findViewById(R.id.gbutton); 
     imageView = (ImageView) findViewById(R.id.gimageView2); 
     button.setOnClickListener(new OnClickListener() { 

     public void onClick(View v) { 
      ((ImageView)findViewById(R.id.gimageView1)).setVisibility(View.GONE); 
      button.setVisibility(View.GONE); 
       GoGray(); 

     } 


    }); 

    } 

    private void GoGray() { 
     Bitmap oBitmap = original.copy(Bitmap.Config.ARGB_8888, true); 
     Bitmap gBitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true); 

     goGrayWithNative(oBitmap,gBitmap); 
     imageView.setImageBitmap(gBitmap); 

    } 

    public native void goGrayWithNative(Bitmap bmp1, Bitmap bmp2); 
} 

這裏是我寫的.c文件代碼爲灰色邏輯

/* 
convertToGray 
Pixel operation 
*/ 
JNIEXPORT void JNICALL Java_com_example_GrayClass_goGrayWithNative(JNIEnv 
* env, jobject obj, jobject bitmapcolor,jobject bitmapgray) 
{ 
    AndroidBitmapInfo infocolor; 
    void*    pixelscolor; 
    AndroidBitmapInfo infogray; 
    void*    pixelsgray; 
    int    ret; 
    int    y; 
    int    x; 

    if ((ret = AndroidBitmap_getInfo(env, bitmapcolor, &infocolor)) < 0) { 
     LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret); 
     return; 
    } 


    if ((ret = AndroidBitmap_getInfo(env, bitmapgray, &infogray)) < 0) { 
     LOGE("AndroidBitmap_getInfo() failed ! error=%d", ret); 
     return; 
    } 



    if (infocolor.format != ANDROID_BITMAP_FORMAT_RGBA_8888) { 
     LOGE("Bitmap format is not RGBA_8888 !"); 
     return; 
    } 


LOGE("Bitmap format is not RGBA_8888 !====%d==", infocolor.format) ; 




    if ((ret = AndroidBitmap_lockPixels(env, bitmapcolor, &pixelscolor)) < 0) { 
     LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret); 
    } 

    if ((ret = AndroidBitmap_lockPixels(env, bitmapgray, &pixelsgray)) < 0) { 
     LOGE("AndroidBitmap_lockPixels() failed ! error=%d", ret); 
    } 


    // modify pixels with image processing algorithm 
    LOGI("unlocking pixels height = %d",infocolor.height); 
    for(y=0;y<infocolor.height;y++) { 
    LOGI("unlocking pixels height = %d",infocolor.width); 
     argb * line = (argb *) pixelscolor; 
     uint8_t * grayline = (uint8_t *) pixelsgray; 

     for(x=0;x<infocolor.width;x++) { 

      grayline[x] = 0.3 * line[x].red + 0.59 * line[x].green + 0.11*line[x].blue; 
     } 
     pixelscolor = (char *)pixelscolor + infocolor.stride; 
     pixelsgray = (char *) pixelsgray + infogray.stride; 
    } 

    LOGI("unlocking pixels"); 
    AndroidBitmap_unlockPixels(env, bitmapcolor); 
    AndroidBitmap_unlockPixels(env, bitmapgray); 


} 

該代碼運行良好,但輸出我是gett ING是不同的看到圖片enter image description here

點擊GoGray按鈕後,它顯示了這樣的形象

enter image description here

誰能告訴我哪裏有錯誤是什麼?

回答

4

據我所知,Android位圖只能處理每像素32位的圖像,所以必須通過重複紅色,綠色的灰度值來存儲灰度結果,就好像它在彩色圖像的位置一樣和藍色通道,將alpha通道設置爲完全不透明。

順便說一句,如果你分析你的屏幕截圖,你會意識到灰度版本的寬度只有1/4的彩色圖像,這往往表明這是問題。

在C使用此代碼++部分應該做的工作:

// modify pixels with image processing algorithm 
LOGI("unlocking pixels height = %d",infocolor.height); 
for(y=0;y<infocolor.height;y++) { 
LOGI("unlocking pixels height = %d",infocolor.width); 
    argb * line = (argb *) pixelscolor; 
    argb * grayline = (argb *) pixelsgray; 

    for(x=0;x<infocolor.width;x++) { 

     uint8_t v = 0.3 * line[x].red + 0.59 * line[x].green + 0.11*line[x].blue; 
     grayline[x].red = v; 
     grayline[x].green = v; 
     grayline[x].blue = v; 
     grayline[x].alpha = line[x].alpha 
    } 
    pixelscolor = (char *)pixelscolor + infocolor.stride; 
    pixelsgray = (char *) pixelsgray + infogray.stride; 
} 

希望這有助於!

+0

感謝您的寶貴回覆。因爲我不是一個C/C++的傢伙,我不明白我如何通過重複紅色,綠色和藍色通道中的灰度值來將灰度結果存儲爲彩色圖像,將alpha通道設置爲完全不透明? – anshul

+0

編輯我的答案推送一些代碼。我比C++粉絲更像C癮,所以這可能需要稍微調整,但我想我們離它不遠。讓我張貼! – mbrenon

+0

好的,謝謝我會試試這個,讓你知道進度。 – anshul

0

我無法幫助您使用NDK,但我確定您可以使用普通Android SDK創建灰色圖像。