Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting colors in a bitmap android

Tags:

android

I have a simple image in my project for android 2.3.3 that i want to know the color of the first pixel and replace every pixel with that color to the background color.... I tried everything like iterating the pixels and setting the color of the pixels and I get nothing...For example,with that picture:

https://www.dropbox.com/s/e9dcq72c9u5o3op/img2.jpg

I want to get a result like that: https://www.dropbox.com/s/r9zox7zueekq2xe/img3.jpg

I'm a very new developer for Android, and because of that sorry for my NAIF question!

like image 889
Ricardo Marques de Sousa Avatar asked Jun 11 '26 05:06

Ricardo Marques de Sousa


1 Answers

Simply use ColorFilter to achieve this. It will replace one colour with another. Working example:

    Paint pnt = new Paint();
    Bitmap myBit = BitmapFactory.decodeFile(pathName);
    Canvas myCanvas = new Canvas(myBit );

    int myColor = myBit.getPixel(0, 0);

    // Set the colour to replace.
    ColorFilter filter = new LightingColorFilter(myColor, Color.WHITE );
    pnt.setColorFilter(filter);

    // Draw onto new bitmap. result Bitmap is newBit
    myCanvas.drawBitmap(myBit,0,0, pnt);
like image 164
IAmGroot Avatar answered Jun 17 '26 13:06

IAmGroot



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!