/* * "getIconImage" uses the scaling version of "drawImage" * which ist faster than "java.awt.Image#getScaledInstance" */ public BufferedImage getIconImage(JPanel panel, BufferedImage image, int size) { int w = image.getWidth(); int h = image.getHeight(); double f = 0; if (w < h) { f = (double) h / (double) w; w = (int) (size / f); h = size; } else { f = (double) w / (double) h; w = size; h = (int) (size / f); } BufferedImage iconImage; GraphicsConfiguration conf = getGraphicsConfiguration(); iconImage = conf.createCompatibleImage(w, h, image.getTransparency()); Graphics2D g = iconImage.createGraphics(); g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage(image, 0, 0, w, h, null); g.dispose(); return iconImage; } ... BufferedImage pictureResized = getIconImage(p, image, pictureSize); try { String name = directory.getName(); name = name + "_" + (count2 < 10 ? "0" : ""); String saveName = saveDir.getAbsolutePath() + "\\" + name + count2 + ".jpg"; ImageIO.write(pictureResized, "jpg", new File(saveName)); count2++; } catch (IOException ex) { Logger.getLogger(PictureViewerDemo.class.getName()).log(Level.SEVERE, null, ex); }
|