Android: drawRect without antialiasing - android

I draw a rectangle on a bitmap and its borders are antialiased or have some effects as you can see:
original image:
How to switch off that effect to get solid lines like this:
I use this code:
Bitmap mBuffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
gfx = new Canvas(mBuffer);
Paint paintNorm = new Paint();
paintNorm.setAntiAlias(false);
paintNorm.setStrokeWidth(1);
paintNorm.setStyle(Paint.Style.STROKE);
paintNorm.clearShadowLayer();
paintNorm.setDither(false);
paintNorm.setFilterBitmap(false);
paintNorm.setColor(0xFFA8A8A8);
gfx.drawRect(2,3,50,50, paintNorm);

My impression is that your bitmap is correctly drawn without anti-aliasing, but the blurry effect may come from the magnification when displaying the bitmap. Your screenshot is 245 pixels wide, but your code seems to indicate that the rectangle should be about 50 pixels or so.

Related

Android: Canvas drawBitmap?

I have a 200x200px bitmap. I want to draw the top left 50x50px corner of my bitmap, on my canvas at coordinates 100,100 with a width and height of 50px, by using:
drawBitmap(Bitmap bitmap, Rect src, Rect dst, Paint paint)
This is what I've tried:
drawBitmap(myBitmap, new Rect(0,0,50,50), new Rect(100,100,150,150) , null);
What am I doing wrong?
From developer.android.com:
Parameters
bitmap The bitmap to be drawn
src May be null. The subset of the bitmap to be drawn
dst The rectangle that the bitmap will be scaled/translated to fit into
paint May be null. The paint used to draw the bitmap
What is missing in my code?
Thanks!
You need to change your rectangles. This is because, as described in the documentation, the first rectangle is the subset of the bitmap you want to draw, the second is the scaling/translating so basically the size of the destination draw (50x50)
So it should look like this:
drawBitmap(myBitmap, new Rect(100,100,150,150), new Rect(0,0,50,50) , null);

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.

How to draw Bitmap from ARGB_8888 Bitmap

I am drawing paths on canvas and creating Transparent bitmap while saving and creating cropping bitmap from transparent bitmap.
See Images :
In this I mage I am drawing path on canvas and I am creating transparent bitmap and according to startX,lowestY and highestX,highestY
Bitmap cropBitmap =Bitmap.createBitmap(sourceBitmap,startX,lowestY,highestX,highestY);
When I am cropping Bitmap I want Only "Test" drawing crop bitmap.But it's giving empty bitmap. Like this
Inside red box I want cropped bitmap from transparent bitmap whatever I draw on canvas.
You cannot simply create a transparent bitmap, and assume that whatever is within the bounds of the bitmap has become a part of it meaning that the pixels are identical. There are two problems with your assumption, number 1, a transparent bitmap won't help because transparency serves as a see through bitmap which only will have alpha value, number 2, the data that is drawn with the path, has no correlation to the data of your bitmap, you initially created a bitmap with no color, except that it's completely transparent.
Here's the general code for the correct way how to achieve such a task:
class myDrawingView extends View() {
// all your class members you initialize here
#Override
public void onDraw(Canvas canvas) {
// get your width and height using startx, starty, highestx, highesty, lowesty
Bitmap buffer = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas.setBitmap(buffer);
canvas.drawPath(//draw your path here);
invalidate();
}
The Bitmap called buffer, now is the frame buffer for all your drawing so you can draw a path and that will be rendered on the buffer you defined. And you see that no transparency was used we just simply created a bitmap with no pixels assigned to it at first, once you draw to it via the canvas, your bitmaps pixels will be whatever you drew.

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);

Trouble making parts of a bitmap transparent

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.

Categories

Resources