Android 创建一个新位图并在其中绘制新像素

Android 创建一个新位图并在其中绘制新像素,android,bitmap,Android,Bitmap,我正在尝试制作一个应用程序,它将拍摄您通过editText指定的两张图片,比较两张图片上每个像素的颜色,并创建一张新图片(位图)(可以保存到sd卡),其中包含两张原始图片之间的差异 我在创建新位图时遇到问题。我怎样才能实现我的目标?我真的不知道怎么做,我是先创建新的位图,然后写入其中,还是先得到差异,然后再从中绘制位图?图片大约为300x300像素。我会先创建位图并计算每个像素之间的差异,但欢迎您先计算差异,然后使用bitmap.copyPixels,但我认为第一种方法更容易理解。以下是一个例子

我正在尝试制作一个应用程序,它将拍摄您通过editText指定的两张图片,比较两张图片上每个像素的颜色,并创建一张新图片(位图)(可以保存到sd卡),其中包含两张原始图片之间的差异


我在创建新位图时遇到问题。我怎样才能实现我的目标?我真的不知道怎么做,我是先创建新的位图,然后写入其中,还是先得到差异,然后再从中绘制位图?图片大约为300x300像素。

我会先创建位图并计算每个像素之间的差异,但欢迎您先计算差异,然后使用bitmap.copyPixels,但我认为第一种方法更容易理解。以下是一个例子:

// Load the two bitmaps
Bitmap input1 = BitmapFactory.decodeFile(/*first input filename*/);
Bitmap input2 = BitmapFactory.decodeFile(/*second input filename*/);
// Create a new bitmap. Note you'll need to handle the case when the two input
// bitmaps are not the same size. For this example I'm assuming both are the 
// same size
Bitmap differenceBitmap = Bitmap.createBitmap(input1.getWidth(), 
    input1.getHeight(), Bitmap.Config.ARGB_8888);
// Iterate through each pixel in the difference bitmap
for(int x = 0; x < /*bitmap width*/; x++)
{
    for(int y = 0; y < /*bitmap height*/; y++)
    {
        int color1 = input1.getPixel(x, y);
        int color2 = input2.getPixel(x, y);
        int difference = // Compute the difference between pixels here
        // Set the color of the pixel in the difference bitmap
        differenceBitmap.setPixel(x, y, difference);
    }
}
//加载两个位图
Bitmap input1=BitmapFactory.decodeFile(/*第一个输入文件名*/);
Bitmap input2=BitmapFactory.decodeFile(/*第二个输入文件名*/);
//创建一个新位图。注意,当两个输入
//位图的大小不同。对于这个例子,我假设两者都是相同的
//同样大小
位图差异位图=位图.createBitmap(input1.getWidth(),
input1.getHeight(),Bitmap.Config.ARGB_8888);
//迭代差分位图中的每个像素
对于(int x=0;x
这段代码是我脑子里想不出来的,没有经过测试,但它应该会让你走上正轨

final int w1 = b1.getWidth();
final int w2 = b2.getWidth();
final int h1 = b1.getHeight();
final int h2 = b2.getHeight();
final int w = Math.max(w1, w2);
final int h = Math.max(h2, h2);

Bitmap compare = Bitmap.createBitmap(w, h, Config.ARGB_8888);

int color1, color2, a, r, g, b;

for (int x = 0; x < w; x++) {
    for (int y = 0; y < h; y++) {
        if (x < w1 && y < h1) {
            color1 = b1.getPixel(x, y);
        } else {
            color1 = Color.BLACK;
        }
        if (x < w2 && y < h2) {
            color2 = b2.getPixel(x, y);
        } else {
            color2 = Color.BLACK;
        }
        a = Math.abs(Color.alpha(color1) - Color.alpha(color2));
        r = Math.abs(Color.red(color1) - Color.red(color2));
        g = Math.abs(Color.green(color1) - Color.green(color2));
        b = Math.abs(Color.blue(color1) - Color.blue(color1));

        compare.setPixel(x, y, Color.argb(a, r, g, b));
    }
}
b1.recycle();
b2.recycle();
final int w1=b1.getWidth();
final int w2=b2.getWidth();
最终int h1=b1.getHeight();
final int h2=b2.getHeight();
最终整数w=数学最大值(w1,w2);
最终整数h=数学最大值(h2,h2);
位图比较=Bitmap.createBitmap(w,h,Config.ARGB_8888);
int color1,color2,a,r,g,b;
对于(int x=0;x