Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Create a new bitmap and draw new pixels into it

Tags:

android

bitmap

I'm trying to make an app that will take two pictures you specify via editText, compare the colors of each pixel on both images and create a new picture (bitmap) (that you can save to the sd card) containing the differences between the two original pictures.

I'm having a problem with creating this new bitmap. How can I achieve my goal? I don't really know how to do this, do I create the new bitmap first and then write into it, or do I get the differences first and then draw a bitmap from that? The pictures will be approx. 300x300 px.

like image 761
Marvos Avatar asked Apr 16 '12 19:04

Marvos


People also ask

How do I change the pixels of a BMP?

To set the colors of the pixels in your pixels array, get values from the static methods of Android's Color class and assign them into your array. When you're done, use setPixels to copy the pixels back to the bitmap.

How do I create a bitmap in canvas?

To create a Bitmap from a resource, use the BitmapFactory method decodeResource(): Bitmap = BitmapFactory. decodeResource(getResources(), R. drawable.

How do I create a bitmap in Kotlin?

To create a bitmap from a resource, you use the BitmapFactory method decodeResource(): Bitmap bitmap = BitmapFactory. decodeResource(getResources(), R. drawable.


1 Answers

this code is just out of my head and untested but it should get you on the right track.

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();
like image 138
Renard Avatar answered Oct 14 '22 06:10

Renard