0

这是我用来将两张彩色图片转换为灰度并逐像素比较它们的代码。我的应用程序一直强制关闭。这是代码:

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

4

1 回答 1

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)

这些行表明您在异步任务中的某处写了 Toast。请将您的 Toast 放入 runOnUiThread(); 如下所示:

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

你就完成了。

于 2013-01-12T04:49:25.073 回答