How can I scale a bitmap and preserve it's neatness? - java

I'm using Bitmap.createScaledBitmap function to scale down my bitmaps, so they come out with artifacts. Is there any trick or work around to smooth the edges?
Example:

I think you are using this code.
bitmap=Bitmap.createScaledBitmap(src, dstWidth, dstHeight, filter);
If the filter is true then it will get smoother edge.Otherwise the image will not be good

I think you can solve the blury image problem by disabling scaling on bitmap load from resources:
Options options = new BitmapFactory.Options();
options.inScaled = false;
Bitmap source = BitmapFactory.decodeResource(a.getResources(), path, options);
Also createScaledBitmap has a flag where you can set if the scaled image should be filtered or not. Setting the flag will improve bitmap quality.

Related

How to make bitmap bigger without resizing the actual image

I am using Pax A920 device.
I have an issue when printing a bitmap, there is no option to center the bitmap(image) documentation
Methods reference:
The issue is when the bitmap's width is short, its left-aligned on the printed receipt.
I can scale the bitmap using:
Bitmap.createScaledBitmap(receiptLogo, 512, receiptLogo.getHeight(), false);
But it looks bad, I'm thinking of creating a new Bitmap with width of 512 & height of receipt.Logo.getHeight() then put the receipt logo in the middle so it wont look stretch.
Is this doable?

resize image without losing quality - java

How to resize image without losing its quality using java code ?
I have tried below code.
BufferedImage thumbnail =
Scalr.resize(inputImage,150, 100);
String formatName = outputImagePath.substring(outputImagePath
.lastIndexOf(".") + 1);
ImageIO.write(thumbnail, formatName, new File(outputImagePath));
It is not possible to resize an image without either throwing pixels away (when making it smaller) or interpolating/duplicating pixels (when making it larger). Thus, it is impossible to "resize" an image without sacrificing quality.
I have used this code, Short and works fine.
Bitmap yourBitmap;
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, newWidth, newHeight, true);

Android Bitmap resize

What is the best way of resizing a bitmap?
Using
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mandy_moore, options);
or
Bitmap resizedbitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
What is the best way of resizing a bitmap?
It depends the flexibility you need:
options.inSampleSize = N; means that you will obtain an image which is N times smaller than the original. Basically, the decoder will read 1 pixel every N pixel.
Use that option if you don't need a particular size for your bitmap but you need to make it smaller in order to use less memory. This is particularly useful for reading big images.
Bitmap.createScaledBitmap on the other hand give you more control: you can indicate precisely the final dimension of the bitmap, ...
The best is to use a combination of both method:
Determine the maximal value of inSampleSize you can use to decode the source image efficiently (such that the decoded image is still bigger than the final size you need)
Use Bitmap.createScaledBitmap to precisely control the resulting size
Use this to create sampled image maintaining same aspect ration as orignal image was.
This is best way to resize.
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.mandy_moore, options);
Bitmap resizedbitmap = Bitmap.createScaledBitmap(bitmap, 200, 200, true);
This will scale the bitmap in provided w and h.
This may distort the bitmap.
Use according to your need.
the method createScaledBitmap produce low quality images
take suggestion from this post bitmap image quality

Bitmap quality lost after scaling canvas

I want to paint a Bitmap scaled in a canvas (based on the Bitmap and the View size). The problem is that if I scale the canvas before painting the bitmap:
canvas.scale(1.5f,1.5f);
canvas.drawBitmap(mBitmap, 0, 0, paint);
This is the result:
But if instead of scaling the canvas I scale the bitmap:
Bitmap scaled = Bitmap.createScaledBitmap(
mBitmap, bitmapWidth, bitmapHeight,
true);
canvas.drawBitmap(scaledBitmap, 0, 0, paint);
The result is much better:
The problem is that the Bitmap is pretty big and having in memory both the original and the scaled bitmap may be problematic.
Is there any way to obtain the second result without creating a scaled Bitmap every time the Bitmap or the View bounds changes?
Use a matrix that contains the scaling info when drawing to canvas
canvas.drawBitmap(x,y,..., matrix...)
Use inSampleSize in BitmapOptions when reading the bitmap from your source to scale it accordingly (it is passed as an extra parameter).
This is androids preferred way of loading images in nearly all cases to avoid memory overflow, as the full image is Never loaded into memory.
The android docs on Loading BItmaps efficiently can be found here.
With example code on how to retrieve an image of desired size from your source. (resource, file, etc etc)
The two methods in Loading Large Bitmaps Efficiently are the ones you are looking for.
The solution to my problem was that the Paint hasn't enabled the FILTER_BITMAP_FLAG flag.

Android resolution independent Bitmap from ImageView

I have a Bitmap which is 1200 pixels in both height and width.
And I put it in ImageView. My app requires that later on I get the Bitmap from the ImageView so that I get the same quality of the Bitmap just when put in the ImageView. If I do it with getDrawingCache for the Layout I get bad image since I have to scale to the original size of the Bitmap and there is quality loss.
So is there any resolution independent way to get the image from the ImageView in its original size?
Thanks
Just use the getDrawable function.
Drawable image=imageview.getDrawable ()

Categories