resize image without losing quality - java - 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);

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?

Prevent image from changing color when written to a new TYPE_BYTE_INDEXED BufferedImage

I've been working on optimizing color PNG images for storage size by using the TYPE_BYTE_INDEXED BufferedImage to limit the number of unique colors each image can contain. I noticed that when I save an existing image to this format, the white (255,255,255) background color becomes an off-white color (252,252,252).
Is there any way to prevent this color shift from happening? I found this question which has a solution but it feels very hacky and is much slower than the Graphics2D implementation of drawImage.
Here's my code currently that's producing this issue. The original image is being loaded as a TYPE_3BYTE_BGR BufferedImage.
BufferedImage image = ImageIO.read(imageOutputPath.toFile());
BufferedImage indexedImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
Graphics2D g = indexedImage.createGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
I know that this type of BufferedImage supports white as I've tried adding white margins around the image and it displays correctly but with the off-white image centered on the page. It just seems like the white in my image isn't converting correctly.
Any ideas and help would be greatly appreciated. Thanks!

Drawing an image using its full size rather than asking me for its dimensions in JavaFX

I am loading an external image with JavaFX:
Image tile = new Image(imageFile.toURI().toURL().toString(),width, height, true,true);
You need to supply the width and height. How can I get the image's width and height?
Now, I know that the width and height of a PNG image are determined by the bytes from 12 to 20 or something like that, so technically you could open a byte stream and interpret the bytes. However, this seems overkill for the simple task of drawing an image in my JavaFX application. Is there not a way to draw an image with its full size without asking me to supply the dimensions?
Just create the Image using a constructor which does not include dimensions (e.g. new Image(url)):
Image image = new Image(url);

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.

Categories