在Java 中調整大小後影像品質較低
在將影像從大約300x300 縮小到60x60 的影像大小調整腳本中,最終的影像品質不令人滿意。
提高品質的一種方法是採用「分而治之」的方法。此方法不是一步縮放影像,而是以 50% 的增量逐漸縮放影像,直到達到所需的大小。這種逐漸縮放的過程有助於維持品質。
以下 Java 程式碼示範了調整影像大小的分而治之的方法:
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; } }
透過逐漸縮放影像,此方法有效減少了影響壓縮偽影並保留影像清晰度。
以上是在Java中調整大小後如何提升影像品質?的詳細內容。更多資訊請關注PHP中文網其他相關文章!