2012-12-07 64 views
0

我在Android中使用位圖。當我轉換爲灰度時,我應該預期每個像素的值在0到255之間。但對於像素,我將其中的一些作爲0,其他作爲非常大的值。我不知道爲什麼。爲什麼我沒有得到範圍[0,255]的值?我如何得到它?位圖 - 獲得非常大的值

我的代碼是:

//Converting to grayscale 
         int width, height; 
         height = mybitmap.getHeight(); 
         width = mybitmap.getWidth();  

         Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
         Canvas c = new Canvas(bmpGrayscale); 
         Paint paint = new Paint(); 
         ColorMatrix cm = new ColorMatrix(); 
         cm.setSaturation(0); 
         ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); 
         paint.setColorFilter(f); 
         c.drawBitmap(mybitmap, 0, 0, paint);     
         //Done bmpgrayscale is the grayscale image formed 
         Toast toast23=Toast.makeText(getApplicationContext(),"Grayscale made", Toast.LENGTH_LONG); 
         toast23.show(); 
         for (int i=0;i<210;i++){ 
          for (int j=0;j<170;j++){ 

            Log.d("gray value is=", String.valueOf(bmpGrayscale.getPixel(i, j))); 

          } 
         } 

輸出的部分是:

12-07 23:14:28.022: D/gray value is=(958): 0 
12-07 23:14:28.022: D/gray value is=(958): 0 
12-07 23:14:28.022: D/gray value is=(958): 0 
12-07 23:14:28.022: D/gray value is=(958): 0 
12-07 23:14:28.022: D/gray value is=(958): 0 
12-07 23:14:28.022: D/gray value is=(958): 0 
12-07 23:14:28.022: D/gray value is=(958): 0 
12-07 23:14:28.022: D/gray value is=(958): 0 
12-07 23:14:28.022: D/gray value is=(958): 0 
12-07 23:14:28.022: D/gray value is=(958): 83886079 
12-07 23:14:28.022: D/gray value is=(958): 317846001 
12-07 23:14:28.022: D/gray value is=(958): 602992880 
12-07 23:14:28.022: D/gray value is=(958): 669772779 
12-07 23:14:28.022: D/gray value is=(958): 904719596 
12-07 23:14:28.022: D/gray value is=(958): 971894253 
12-07 23:14:28.022: D/gray value is=(958): 1223552493 
12-07 23:14:28.022: D/gray value is=(958): 1223552493 
12-07 23:14:28.022: D/gray value is=(958): 1223552493 
12-07 23:14:28.022: D/gray value is=(958): 1223552493 
12-07 23:14:28.052: D/gray value is=(958): 1223552493 
12-07 23:14:28.052: D/gray value is=(958): 1223552493 
12-07 23:14:28.052: D/gray value is=(958): 1223552493 
12-07 23:14:28.052: D/gray value is=(958): 1223552493 
12-07 23:14:28.052: D/gray value is=(958): 1223552493 
12-07 23:14:28.052: D/gray value is=(958): 1223552493 
12-07 23:14:28.052: D/gray value is=(958): 1223552493 
12-07 23:14:28.052: D/gray value is=(958): 1223552493 
12-07 23:14:28.052: D/gray value is=(958): 1223552493 
12-07 23:14:28.052: D/gray value is=(958): 1223552493 

請幫助。我正在使用它將我的視圖轉換爲位圖,然後將位圖轉換爲灰度。

編輯

//Converting to grayscale 
         int width, height; 
         height = mybitmap.getHeight(); 
         width = mybitmap.getWidth();  

         Bitmap bmpGrayscale = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888); 
         Canvas c = new Canvas(bmpGrayscale); 
         Paint paint = new Paint(); 
         ColorMatrix cm = new ColorMatrix(); 
         cm.setSaturation(0); 
         ColorMatrixColorFilter f = new ColorMatrixColorFilter(cm); 
         paint.setColorFilter(f); 
         c.drawBitmap(mybitmap, 0, 0, paint);     
         //Done bmpgrayscale is the grayscale image formed 
         Toast toast23=Toast.makeText(getApplicationContext(),"Grayscale made", Toast.LENGTH_LONG); 
         toast23.show(); 
         for (int i=0;i<210;i++){ 
          for (int j=0;j<170;j++){ 
            int a=bmpGrayscale.getPixel(i, j); 
            ByteBuffer b = ByteBuffer.allocate(4); 
            //b.order(ByteOrder.BIG_ENDIAN); // optional, the initial order of a byte buffer is always BIG_ENDIAN. 
            b.putInt(a ); 

            byte[] result = b.array();          
            Log.d("gray value is=", String.valueOf((int)result[0])); 

          } 
         } 
+1

我很困惑,爲什麼你的回報expecint 0-255。如果轉換爲十六進制,則較大的值更有意義:83886079 = 4FFFFFFF。 1223552493 = 48EDEDED。注意最後3組字節總是相等的? (灰度值總是RGB相等) – Grambot

+0

@TheCapn好的。但是我希望範圍[0,255]中的值用於進一步的操作。我該怎麼辦?如何將值轉換爲該範圍? –

+0

將int值轉換爲一系列字節,取最後8個字節並將它們轉換回int?你會得到一個0-255的範圍(0-FF十六進制與0-255十進制相同) – Grambot

回答

0

不知道爲什麼你得到負值,可能是溢出的事情嗎?這就是我所做的

int value = getLastBytes(1223552493); //Prints 237, the dec value of ED 

public int getLastBytes(long original) { 
String hexBytes = Long.toHexString(original); 
return Integer.parseInt(hexBytes.substring(hexBytes.length()-2),16); 
} 

你的情況,你也許可以把它像這樣:

Log.d("gray value is=", getLastBytes(bmpGrayscale.getPixel(i, j)));