Trouble making parts of a bitmap transparent - android

I'm developing an android app where i need to capture text and save it as a transparent image. Capturing the text has been done but making a transparent png file is where i'm stuck as i'm not familiar with image pixel manipulation at all. Here's what I have so far... i first create a blank bitmap and fill it with a white background, then i set the paint's transparency to 0 (full transparency) and then draw the source bitmap into the destination bitmap using the XOR modes.. but when i run the app all i see is a blank white image. i'll be glad if someone points out what i'm doing wrong and how to fix it. Thanks in advance.
b = Bitmap.createBitmap(tw, th,Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(b);
Rect dest = new Rect(0,0,b.getWidth(),b.getHeight());
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.WHITE);
canvas.drawRect(0, 0, b.getWidth(), b.getHeight(), paint);
paint.setAlpha(0);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.XOR));
canvas.drawBitmap(bmp,null,dest,paint);

Have you looked at : How to change a bitmap's opacity?
Seems like
paint.setAlpha(0);
won't do anything as you need to set the alpha channel to something greater than 0...

Use:
Color.argb(0,0,0,0)
The first parameter is the alpha. Set it to 0 for complete transparency.

Related

Android change bitmap transparency on canvas

I want to be able to slowly make a bitmap image more and more transparent on my canvas.
Currently I am drawing the .png file (stored in drawables) like this:
//I setup the Bitmap in my constructor.
heartSymb = BitmapFactory.decodeResource(getResources(),R.drawable.heartsymbol);
//This is in on draw.
canvas.drawBitmap(heartSymb,0,0,null);
How would I be able to slowly change the bitmap's transparency until it becomes fully transparent?
You can use a Paint object to modify the alpha of the bitmap to be drawn:
Paint alphaPaint = new Paint();
alphaPaint.setAlpha(alpha);
canvas.drawBitmap(heartSymb, 0, 0, alphaPaint);
Then you just have to modify alpha value and perform update periodically, maybe by using a Handler.

how to change the color of an image to black and white in android

In my application i want the image which is captured through the camera to appear as a pure black and white image , as i want the captured image image to be printed later..
I tried many codes to convert the into a black and white image but still the image comes as a gray scale image the pixels other than the black ones should become white.
The code that i am using is given as below :
public static Bitmap blackNwhite(Bitmap bitmap)
{
Bitmap bmpMonochrome = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bmpMonochrome);
ColorMatrix ma = new ColorMatrix();
ma.setSaturation(0);
Paint paint = new Paint();
paint.setColorFilter(new ColorMatrixColorFilter(ma));
canvas.drawBitmap(bitmap, 0, 0, paint);
return bmpMonochrome;
}
The image that i get as the output is
which is not how i wanted as it is slightly greyish is color..
I want the image to be displayed as follows :
how can i achieve this????? Please help !!!
You can go for following link:
What is the fastest way to convert an image to pure black and white in C#?
Using exact ColorMatrix, best results can be obtained.

Android - Apply a bitmap texture to API drawing routines

I have a bitmap that spans the whole screen that will function as texture for a Path object that I need to draw to my canvas. I then have a background image that this textured path needs to be drawn on top of.
I tried using the PorterDuff modes, but nothing seemed to work correctly. I was having a hard time figuring out exactly how the PorterDuff modes act, because none of them seem to act the way I always thought they were supposed to function.
I've figured out a way to texture the path with this test code:
Paint paint = new Paint();
//draw the texture
canvas.drawBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.texture),0,0,paint);
//construct the Path with an inverse even-odd fill
Path p = new Path();
p.setFillType(Path.FillType.INVERSE_EVEN_ODD);
p.addCircle(this.getHeight()/2, this.getWidth()/2, 200, Path.Direction.CCW);
//use CLEAR to remove inverted fill, thus showing only the originally filled section
paint.setXfermode(new PorterDuffXfermode(android.graphics.PorterDuff.Mode.CLEAR));
//draw path
canvas.drawPath(p, paint);
But I can't figure out how to then place that on top of a background image. Am I just using the PorterDuff modes wrong?
Maybe my questions could lead you to your solution:
The paint used in your drawPath call:
What is the paint style?
What is the paint stroke width?
What is the paint stroke/fill color?
If you are not using a stoke/fill color, but a texture instead, where is the call to the paint's setShader (using a BitmapShader)?

transform a sqaure shape drawable in to circular shape drawable withaout affecting the pixel quality

I have an application which takes picture from camera .And normally this pictures have either rectangle shapes or square shapes.But due to make these pictures circular in shape i limited the shape to only square.So now my problem is to make this square picture into a circular shape having radius of half of width or height.And the clipped part should be removed from the picture.
Its same like making a circle from the square which touches midpoint of all of the faces of square.
I don't have any experience with canvas or other in built drawing functions.
For example i have square at first now i want to make image circular such that parts pointed by arrow should get clipped from image.
Note:as i have to work with camera images .quality is crucial factor.So please suggest the possible ways that will not affect the pixel quality and other important factors.
You can do it by masking the image with your desired shape, see this post for detailed info.
example code:
Resources resources = context.getResources();
Bitmap original = BitmapFactory.decodeResource(resources,R.drawable.original);
Bitmap mask = BitmapFactory.decodeResource(resources,R.drawable.mask);
Bitmap result = Bitmap.createBitmap(mask.getWidth(), mask.getHeight(), Config.ARGB_8888);
Canvas c = new Canvas(result);
Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
c.drawBitmap(original, 0, 0, null);
c.drawBitmap(mask, 0, 0, paint);
paint.setXfermode(null);
imageView.setImageBitmap(result);

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