Home >Java >javaTutorial >How Can I Improve Image Quality After Resizing in Java?
Low Image Quality After Resizing in Java
In an image resizing script that reduces an image from approximately 300x300 to 60x60, the resultant image quality is unsatisfactory.
One method to improve quality is to employ a "divide and conquer" approach. Instead of scaling the image in one step, this method scales it gradually in increments of 50% until the desired size is achieved. This gradual scaling process helps preserve quality.
The following Java code demonstrates the divide and conquer approach to resizing an image:
import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import javax.imageio.ImageIO; public class DivideAndConquerImageResize { public static void main(String[] args) { try { // Read the original image BufferedImage original = ImageIO.read(new File("master.jpg")); // Scale the image in 50% increments BufferedImage scaled = original; while (scaled.getWidth() > 60 || scaled.getHeight() > 60) { scaled = getScaledInstanceToFit(scaled, new Dimension(scaled.getWidth() / 2, scaled.getHeight() / 2)); } // Write the scaled image to a file ImageIO.write(scaled, "jpg", new File("scaled.jpg")); } catch (IOException e) { e.printStackTrace(); } } public static BufferedImage getScaledInstanceToFit(BufferedImage img, Dimension size) { float scaleFactor = getScaleFactorToFit(img, size); return getScaledInstance(img, scaleFactor); } public static float getScaleFactorToFit(BufferedImage img, Dimension size) { float scale = 1f; if (img != null) { int imageWidth = img.getWidth(); int imageHeight = img.getHeight(); scale = getScaleFactorToFit(new Dimension(imageWidth, imageHeight), size); } return scale; } public static float getScaleFactorToFit(Dimension original, Dimension toFit) { float scale = 1f; if (original != null && toFit != null) { float dScaleWidth = getScaleFactor(original.width, toFit.width); float dScaleHeight = getScaleFactor(original.height, toFit.height); scale = Math.min(dScaleHeight, dScaleWidth); } return scale; } public static float getScaleFactor(int iMasterSize, int iTargetSize) { float scale = 1; if (iMasterSize > iTargetSize) { scale = (float) iTargetSize / (float) iMasterSize; } else { scale = (float) iTargetSize / (float) iMasterSize; } return scale; } public static BufferedImage getScaledInstance(BufferedImage img, double dScaleFactor) { BufferedImage imgBuffer = null; imgBuffer = getScaledInstance(img, dScaleFactor, RenderingHints.VALUE_INTERPOLATION_BILINEAR, true); return imgBuffer; } protected static BufferedImage getScaledInstance(BufferedImage img, double dScaleFactor, Object hint, boolean higherQuality) { int targetWidth = (int) Math.round(img.getWidth() * dScaleFactor); int targetHeight = (int) Math.round(img.getHeight() * dScaleFactor); int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB; BufferedImage ret = (BufferedImage) img; if (targetHeight > 0 || targetWidth > 0) { int w, h; if (higherQuality) { w = img.getWidth(); h = img.getHeight(); } else { w = targetWidth; h = targetHeight; } do { if (higherQuality && w > targetWidth) { w /= 2; if (w < targetWidth) { w = targetWidth; } } if (higherQuality && h > targetHeight) { h /= 2; if (h < targetHeight) { h = targetHeight; } } BufferedImage tmp = new BufferedImage(Math.max(w, 1), Math.max(h, 1), type); Graphics2D g2 = tmp.createGraphics(); g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, hint); g2.drawImage(ret, 0, 0, w, h, null); g2.dispose(); ret = tmp; } while (w != targetWidth || h != targetHeight); } else { ret = new BufferedImage(1, 1, type); } return ret; } }
By gradually scaling the image, this method effectively reduces the impact of compression artifacts and preserves image sharpness.
The above is the detailed content of How Can I Improve Image Quality After Resizing in Java?. For more information, please follow other related articles on the PHP Chinese website!