Convert a Bitmap's transparent background to white - android

I take a screenshot of a view (which contains a black line drawn on a white background):
myView.setDrawingCacheEnabled(true);
Bitmap drawing = Bitmap.createBitmap(myView.getDrawingCache());
If I rotate this bitmap like so:
Matrix matrix = new Matrix();
matrix.postRotate(45);
drawing = Bitmap.createBitmap(drawing, 0, 0, drawing.getWidth(), drawing.getHeight(), matrix, true);
I will get an image with a transparent background.
What do I have to do in order to replace the transparent color with white and not leave any artifacts? Is this even possible?

Since my image consists of just black lines over a black background, I was able to work around this issue in a very inefficient manner: iterate over all the pixels in the image and replace their color with white if they are not black. It seems that there is a separation zone between Color.TRANSPARENT and Color.WHITE a few pixels wide, which will make life really hard for someone trying to achieve the same result for a more colorful image.

Related

Semi transparent bitmap overlay renders wrong colour

I have a stack of bitmaps that I need to render one above the other. I achieve this with a relative layout and several ImageViews on top of each other which all have a Bitmap assigned to it.
This works great, but when the top layers is semi-transparent, the colours of the lower bitmap are off.
All my bitmaps use Config.ARGB_8888.
Say the top layer is red with an alpha of 50% and the bottom layer is green with an alpha of 100%.
I can either set the colour of the bitmap to red, then the alpha of the ImageView to 0.5f and it will render the green colour below fine (darker green with some red mixed in).
If I set the bitmap pixels to a 50% red like this: bmp.eraseColor(0x7Fff0000); and leave the imageView alpha on 100%, the green below will be displayed as yellow, mixing red and green, rather than overlaying it.
Unfortunately I can not use the (working) fist version because the alpha on the Bitmap above is not going to be uniform.
Is there a blend mode setting to use true colours when using semi transparent pixels in a Bitmap?
EDIT: I have also tried to set several PorterDuffXfermodes to the ImageViews but none gives the right result.
Paint paint = new Paint();
paint.setXfermode(new PorterDuffXfermode(Mode.MULTIPLY)); //OVERLAY//ADD//SCREEN//DARKEN//LIGHTEN
imageView.setLayerType(View.LAYER_TYPE_SOFTWARE, paint);
Got it, needed to premultiply the alpha to get the desired result.

Fill a canvas with a transparent color in Android

Good Morning,
I have an ImageView that I initialized with #99aaaaaa color (it corresponds to 153,170, 170,170). after that I draw some lines with different colors. and Now I want to Fill my Canvas with the original color (#99aaaaaa).
The method myCanvas.drawColor(OriginalColor) fills the canvas with OriginalColor, but the lines still visible
myPaint.setColor(OriginalColor);
myPaint.setStyle(Paint.Style.FILL);
myCanvas.drawRect(0, 0, 170, 170, myPaint); // my ImageView is 170X170
Also let lines visible.
Any help please, Thank You
as the canvas original color is semi transparent, then you draw something on it and draw another layer of semi transparent stuff, then its pretty obvious youll see the level-down-layer through the top transparent layer isnt it? another words, if you place a half transparent glass on your knees, ull still see the knees through it
Set the transfer mode as follows, before calling drawRec():
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC));
Reset the painter afterwards, for further drawing:
paint.setXfermode(null);
paint.setColor(0xFFFFFFFF);

How to draw alpha on an image / setting pixels invisible - on fingertouch

I want to create a little scratch game. The problem is, that I can't figure out how to erase pixels from an image in android (like the eraser in gimp / photoshop).
The image is an .png with alpha channel.
AIUI, drawing operations on a canvas blend a transparent pixel with the prior value of the pixel. This is by default, and you can see it by setting a canvas to black and drawing a fully transparent shape onto it, and then drawing the underlying bitmap over an image of another canvas (result: a fully black canvas), or by setting a canvas to a partially transparent color and, drawing a shape of another partially transparent color, and then drawing this over an image (result: the original image is tinted by the first color, outside the shape; within the shape, it's tinted by both transparent colors). I don't know the blending method used by default, and looking through the docs just makes me wish I knew what book to buy so I can understand how to use what's available.
So I would set pixels to transparent by setting them 'directly', with Bitmap methods, rather than with canvas operations. Although if you need to punch a transparent shape into an overlay, you can draw the shape with a solid non-transparent color, and then manipulate the bitmap directly, mapping this color to the transparent color.
Bitmap docs. Prefer getPixels() and setPixels() to a method-call per pixel.
EDIT: ...er, did I misunderstand? You want to 'erase' pixels as in a paint program? Then just draw whatever the background color is. There's no erasure involved.

Android bitmap mask color, remove color

I am creating bitmap, next i am drawing second solid color bitmap on top of it.
And now i want to change first bitmap, so solid color that i drawed on it will be transparent.
Or simply, i want to remove all pixels of one color from bitmap.
I havie tried every colorfilter, and xfermode with no luck, is there any other possibility to remove color other that doing it pixel by pixel?
This works for removing a certain color from a bitmap. The main part is the use of AvoidXfermode. It should also work if trying to change one color to another color.
I should add that this answers the question title of removing a color from a bitmap. The specific question is probably better solved using PorterDuff Xfermode like the OP said.
// start with a Bitmap bmp
// make a mutable copy and a canvas from this mutable bitmap
Bitmap mb = bmp.copy(Bitmap.Config.ARGB_8888, true);
Canvas c = new Canvas(mb);
// get the int for the colour which needs to be removed
Paint p = new Paint();
p.setARGB(255, 255, 0, 0); // ARGB for the color, in this case red
int removeColor = p.getColor(); // store this color's int for later use
// Next, set the alpha of the paint to transparent so the color can be removed.
// This could also be non-transparent and be used to turn one color into another color
p.setAlpha(0);
// then, set the Xfermode of the pain to AvoidXfermode
// removeColor is the color that will be replaced with the pain't color
// 0 is the tolerance (in this case, only the color to be removed is targetted)
// Mode.TARGET means pixels with color the same as removeColor are drawn on
p.setXfermode(new AvoidXfermode(removeColor, 0, AvoidXfermode.Mode.TARGET));
// draw transparent on the "brown" pixels
c.drawPaint(p);
// mb should now have transparent pixels where they were red before
user487252's solution works like a charm up until API level 16 (Jelly Bean), after which AvoidXfermode does not seem to work at all.
In my particular use case, I have rendered a page of a PDF (via APV PDFView) into a pixel array int[] that I am going to pass into Bitmap.createBitmap( int[], int, int, Bitmap.Config ). This page contains line art drawn onto a white background, and I need to remove the background while preserving the anti-aliasing.
I couldn't find a Porter-Duff mode that did exactly what I wanted, so I ended up buckling and iterating through the pixels and transforming them one by one. The result was surprisingly simple and performant:
int [] pixels = ...;
for( int i = 0; i < pixels.length; i++ ) {
// Invert the red channel as an alpha bitmask for the desired color.
pixels[i] = ~( pixels[i] << 8 & 0xFF000000 ) & Color.BLACK;
}
Bitmap bitmap = Bitmap.createBitmap( pixels, width, height, Bitmap.Config.ARGB_8888 );
This is perfect for drawing line art, since any color can be used for the lines without losing the anti-aliasing. I'm using the red channel here, but you can use green by shifting 16 bits instead of 8, or blue by shifting 24.
Pixel by pixel is not a bad option. Just don't call setPixel inside your loop. Fill an array of argb ints with getPixels, modify it in place if you don't need to preserve the original, and then call setPixels at the end. You can do this row-by-row if memory is a concern, or you can just do the whole thing in one shot. You don't need to fill a whole bitmap for your overlay color since you'd just be doing a simple replace (if current pixel is color1, set to color2).

What does 'Paint' parameter do in android.graphics.Canvas.drawBitmap()?

I have a question about drawBitmap.
android.graphics.Canvas.drawBitmap(Bitmap bitmap, float left, float top, Paint paint)
What does that Paint paint? For example I have a picture.jpg and I make
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.picture);
paint = new Paint();
paint.setColor(Color.BLUE);
canvas.drawBitmap(bitmap, 0, 0, paint);
What can I do with that "paint" when I have a real picture not some canvas.drawCircle. Is there any way I can change pictures color or something like that?
Yes and another question. For example I draw circle in mspaint in 80x80 size and my background stays plain white. When I use that drawing in my program it shows circle + that white background. Is there any way that there will be displayed only circle without background. Maybe somebody can suggest some program in which I can make that happen or which code should I use in my program? (circle is just example, there can be anything)
Yes and excuse to use circle's background same as program's background is not appropriate, because my program's background isn't white or black or any other color, it is picture.
Paint objects can affect the rendering of the Bitmap. For example, they be used to mask the drawing of the Bitmap.
Save your circle as a PNG or GIF, and set the background as transparent (I do not know if MS Paint can do this).
i suggest gimp for image editing with transparency.
start a new image, delete the default layer, add a transparent layer, then paste your image over that. you can use the fuzzy select tool to trim any white space, then save as .png and you have a transparent image!

Categories

Resources