2013-01-12 18 views
0

這是我使用轉換雙色圖片爲灰度和像素比較它們像素的代碼。儘管我的應用程序保持關閉。這裏的代碼:轉換ColorBitmap到GrayscaleBitmap不斷崩潰

public boolean equals(Bitmap bitmap1, Bitmap bitmap2) { 
     Bitmap grayscaleBitmap1 = Bitmap.createBitmap(
       bitmap1.getWidth(), bitmap1.getHeight(), 
       Bitmap.Config.RGB_565); 

      Canvas c = new Canvas(grayscaleBitmap1); 
      Paint p = new Paint(); 
      ColorMatrix cm = new ColorMatrix(); 

      cm.setSaturation(0); 
      ColorMatrixColorFilter filter = new ColorMatrixColorFilter(cm); 
      p.setColorFilter(filter); 
      c.drawBitmap(bitmap1, 0, 0, p); 
      Bitmap grayscaleBitmap2 = Bitmap.createBitmap(
        bitmap2.getWidth(), bitmap2.getHeight(), 
        Bitmap.Config.RGB_565); 
      Canvas c1 = new Canvas(grayscaleBitmap2); 
      c1.drawBitmap(bitmap2, 0, 0, p);  
     ByteBuffer buffer1 = ByteBuffer.allocate(grayscaleBitmap1.getHeight() 
       * grayscaleBitmap1.getRowBytes()); 
     grayscaleBitmap1.copyPixelsToBuffer(buffer1); 

     ByteBuffer buffer2 = ByteBuffer.allocate(grayscaleBitmap2.getHeight() 
       * grayscaleBitmap2.getRowBytes()); 
     grayscaleBitmap2.copyPixelsToBuffer(buffer2); 

     return Arrays.equals(buffer1.array(), buffer2.array()); 
    } 

這是logcat

回答

1
01-12 10:12:48.947: E/AndroidRuntime(2052): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 
01-12 10:12:48.947: E/AndroidRuntime(2052):  at android.os.Handler.<init>(Handler.java:121) 
01-12 10:12:48.947: E/AndroidRuntime(2052):  at android.widget.Toast.<init>(Toast.java:68) 

這些行說,你已經在你的異步任務的地方寫吐司。 請把你的吐司到 runOnUiThread();如下所示:

runOnUiThread(new Runnable() {   
      @Override 
      public void run() { 
      Toast.makeText(yourActivity.this, "your Text Here!",1000).show();        
      } 
    }); 

你就完成了。

+0

那笨拙的我。謝謝。 –

+0

我已經寫了,你應該怎麼寫this.please檢查 –

+0

,我知道該怎麼做,只是貼一些代碼到的AsyncTask並沒有意識到它有一個烤麪包。 –