Kualiti Imej Rendah Selepas Saiz semula dalam Java
Dalam skrip saiz semula imej yang mengurangkan imej daripada kira-kira 300x300 kepada 60x60, kualiti imej yang terhasil adalah tidak memuaskan.
Satu kaedah untuk meningkatkan kualiti adalah dengan menggunakan a pendekatan "pecah dan takluk". Daripada menskala imej dalam satu langkah, kaedah ini menskalakannya secara beransur-ansur dalam kenaikan 50% sehingga saiz yang dikehendaki dicapai. Proses penskalaan beransur-ansur ini membantu mengekalkan kualiti.
Kod Java berikut menunjukkan pendekatan bahagi dan takluk untuk mengubah saiz imej:
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; } }
Dengan menskala imej secara beransur-ansur, kaedah ini mengurangkan kesan secara berkesan artifak mampatan dan mengekalkan ketajaman imej.
Atas ialah kandungan terperinci Bagaimana Saya Boleh Meningkatkan Kualiti Imej Selepas Mengubah Saiz di Java?. Untuk maklumat lanjut, sila ikut artikel berkaitan lain di laman web China PHP!