Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to change the color of certain pixels in bitmap android

i've a bitmap that i want to change certain pixels. i've got the data from the bitmap into an array, but how would i set a pixels colour in that array?

thanks

int[] pixels = new int[myBitmap.getHeight()*myBitmap.getWidth()];
            myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

            for(int i =0; i<500;i++){
                //Log.e(TAG, "pixel"+i +pixels[i]);
like image 335
turtleboy Avatar asked May 06 '11 19:05

turtleboy


People also ask

How do I change the color of a bitmap image?

Click Bitmaps Bitmap color mask. Choose a color from the list of masked colors. Click the Edit color button . Use the controls in the Select color dialog box to edit the color.

How do I change the pixels of a BMP?

If you want to maintain the file size, enable the Maintain original size check box. When this check box is enabled, the height and width of the bitmap are automatically adjusted as you change the resolution. You can also resample a selected bitmap by clicking the Resample button on the property bar.

How do I change the pixel color in Java?

Create a Color object bypassing the new RGB values as parameters. Get the pixel value from the color object using the getRGB() method of the Color class. Set the new pixel value to the image by passing the x and y positions along with the new pixel value to the setRGB() method.

How to create bitmap in android?

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


1 Answers

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.

For example, to turn the first five rows of the bitmap blue:

import android.graphics.Color;

int[] pixels = new int[myBitmap.getHeight()*myBitmap.getWidth()];
myBitmap.getPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());
for (int i=0; i<myBitmap.getWidth()*5; i++)
    pixels[i] = Color.BLUE;
myBitmap.setPixels(pixels, 0, myBitmap.getWidth(), 0, 0, myBitmap.getWidth(), myBitmap.getHeight());

You can also set a pixel's color in a Bitmap object one at a time without having to set up a pixel buffer with the setPixel() method:

myBitmap.setPixel(x, y, Color.rgb(45, 127, 0));
like image 116
BHSPitMonkey Avatar answered Sep 25 '22 14:09

BHSPitMonkey