2013-04-16 44 views
1

我是一名使用Android的新手,並且現在已經在爲這個問題工作了四天。我真的很感謝某人的幫助。Android Bitmap.getPixel總是返回負數

我在ImageView中有一個圖像,我想要獲取用戶觸摸的圖像部分的顏色。爲此我使用Bitmap.getPixel()函數。問題是,這個函數的返回值總是負的,正如文檔所說的那樣,是錯誤的。我沒有得到正確的顏色值,我真的嘗試過幾種方法(RGB,HSV ...)。請有人解釋我,爲什麼我的Bitmap.getPixel()函數始終返回負值?提前致謝。

這裏是我的.java代碼:

public class MainActivity extends Activity { 
    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     final ImageView imageView = (ImageView) findViewById(R.id.imageView1); 
     imageView.setOnTouchListener(new ImageView.OnTouchListener(){ 
      public boolean onTouch(View v, MotionEvent event) { 
       if(event.getAction() == MotionEvent.ACTION_DOWN) { 
        Drawable imgDrawable = ((ImageView)imageView).getDrawable(); 
        Bitmap mutableBitmap = Bitmap.createBitmap(imageView.getWidth(), imageView.getHeight(), Bitmap.Config.ARGB_8888); 
        Canvas canvas = new Canvas(mutableBitmap); 
        imgDrawable.draw(canvas); 
        int pixel = mutableBitmap.getPixel((int)event.getX(), (int)event.getY()); 
        Log.i("PIXEL COLOR", ""+pixel); 

        int alpha = Color.alpha(pixel); 
        int red = Color.red(pixel); 
        int blue = Color.blue(pixel); 
        int green = Color.green(pixel); 
        String color = String.format("#%02X%02X%02X%02X", alpha, red, green, blue); 
        Log.i("RGB", color); 

        float[] hsv = new float[3]; 
        Color.RGBToHSV(red, green, blue, hsv); 
        Log.i("HSV_H", "Hue=" + hsv[0]); 
        Log.i("HSV_H", "Saturation=" + hsv[1]); 
        Log.i("HSV_H", "Value=" + hsv[2]); 
       } 
       return true; 
      } 
     }); 
} 

}

這裏是我的.xml代碼:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" 
     android:scaleType="fitXY" 
     android:src="@drawable/lapices" /> 

</LinearLayout> 

回答

0

這可能是因爲它不是一個BitmapDrawable

在這裏檢查https://stackoverflow.com/a/16037415/906362我的回答是將Drawable轉換爲Bitmap的正確方法。

編輯:

試試這個:

Drawable imgDrawable = ((ImageView)imageView).getDrawable(); 
Bitmap mutableBitmap = Bitmap.createBitmap(imageView.getWidth(), imageView.getHeight(), Bitmap.Config.ARGB_8888); 
Canvas canvas = new Canvas(mutableBitmap); 
imgDrawable.draw(canvas); 
int pixel = mutableBitmap.getPixel((int)event.getX(), (int)event.getY()); 
+0

我看過這個例子。對不起,但我很困惑。它使用位圖和Drawable類,但不使用BitmapDrawable類。你的意思是我應該使用BitmapDrawable類而不是Bitmap類嗎?對於這些問題抱歉,但這有點令人困惑,而且我也很難理解Bitmap和Drawable之間的區別。 – patriciasc

+1

位圖只是一個有點像素信息的ByteArray。 Drawable是可以用來繪製成Canvas的東西。 BitmapDrawable是Drawable的一個擴展類,恰好在畫布上繪製一個位圖。 該示例代碼所做的並不在乎使用了哪種類型的Drawable,只需要將其繪製到具有位圖的畫布中即可。這樣,該位圖上的所有字節將獲得他們的顏色 – Budius

+0

感謝您的反應Budious,非常感謝。這次我使用畫布更改了代碼,就像我在例子中看到的那樣,但我仍然得到一個負值。我不知道我是否理解這個權利,但我猜測我並不完全。 – patriciasc

1

答案是不正確的: 「這可能是因爲它不是一個位圖繪製。」

正如在小評論中提到的那樣,這是因爲包含顏色的整數的大小。結果是消極或積極的。

所以:GetPixel返回正確的結果。

1

當您將圖片設置爲fitXY縮放類型時,可能會出現這種情況,您最終可能會得到錯誤的值。請移除縮放類型,然後重試。