public static BufferedImage createThumbnail(BufferedImage image, int maxWidth, int maxHeight) { if (image == null) { return null; } final double widthRate = (double) maxWidth / image.getWidth(); final double heightRate = (double) maxHeight / image.getHeight(); double rate = 1; if (widthRate > heightRate) { rate = heightRate; } else { rate = widthRate; } int type = image.getType(); if (type == BufferedImage.TYPE_CUSTOM) { type = BufferedImage.TYPE_INT_ARGB; } final int w = (int) (image.getWidth() * rate); final int h = (int) (image.getHeight() * rate); final Image newImage = Toolkit.getDefaultToolkit().createImage(new FilteredImageSource(image.getSource(), new AreaAveragingScaleFilter(w, h))); return imageToBufferedImage(newImage); }
public static void main(String args[]) { new AreaAveragingScaleFilter(100, 100).clone(); }
public static BufferedImage resizeImage(BufferedImage image, int drawingWidth, int drawingHeight, CenterOption centerOption, ScaleOption scaleOption){ int imageWidth=image.getWidth(); int imageHeight=image.getHeight(); double widthScale = drawingWidth/(double) imageWidth; double heightScale = drawingHeight/(double) imageHeight; double scale=Math.min(widthScale, heightScale); int scaleWidth=(int) (imageWidth*scale); int scaleHeight=(int) (imageHeight*scale); BufferedImage resizedImage = new BufferedImage(scaleWidth, scaleHeight, BufferedImage.TYPE_INT_ARGB); Graphics2D g = resizedImage.createGraphics(); if (scaleOption==ScaleOption.SCALE_AREA_AVERAGING){ ImageProducer prod = new FilteredImageSource(image.getSource(), new AreaAveragingScaleFilter(scaleWidth, scaleHeight)); Image img = Toolkit.getDefaultToolkit().createImage(prod); g.drawImage(img, 0, 0, scaleWidth, scaleHeight, null); // it's already scaled } else { if (scaleOption==ScaleOption.SCALE_NEAREST_NEIGHBOR) { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR); } else if (scaleOption==ScaleOption.SCALE_BICUBIC) { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BICUBIC); } else if (scaleOption==ScaleOption.SCALE_BILINEAR) { g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); } g.drawImage(image, 0, 0, scaleWidth, scaleHeight, 0, 0, imageWidth, imageHeight, null); } g.dispose(); int cropWidth = (int) Math.min(scaleWidth, drawingWidth); int cropHeight = (int) Math.min(scaleHeight, drawingHeight); int drawingLeft=0; int drawingTop=0; if ((centerOption==CenterOption.CENTER_HORIZONTAL) || (centerOption==CenterOption.CENTER_BOTH)){ drawingLeft=(int) ((drawingWidth-cropWidth)/2.0); } if ((centerOption==CenterOption.CENTER_VERTICAL) || (centerOption==CenterOption.CENTER_BOTH)){ drawingTop=(int) ((drawingHeight-cropHeight)/2.0); } BufferedImage croppedImage = resizedImage.getSubimage(0, 0, cropWidth, cropHeight); resizedImage=null; BufferedImage drawingImage = new BufferedImage(drawingWidth, drawingHeight, BufferedImage.TYPE_INT_ARGB); g = drawingImage.createGraphics(); g.drawImage(croppedImage, drawingLeft, drawingTop, drawingLeft+cropWidth, drawingTop+cropHeight, 0, 0, cropWidth, cropHeight, null); g.dispose(); croppedImage=null; return drawingImage; }
/** * 画像をリサイズする * * @param inputFile * リサイズ元画像ファイル * @param canvasWidth * リサイズ後の画像の幅 * @param canvasHeight * リサイズ後の画像の高さ * @param outputFile * リサイズ後画像ファイル * @throws IOException */ @VisibleForTesting void resizeImage(File inputFile, int canvasWidth, int canvasHeight, boolean keepAspectRatio, File outputFile) throws IOException { BufferedImage inputImage = ImageIO.read(inputFile); if (inputImage == null) { throw new IOException("ダウンロードした画像ファイル形式が判別できませんでした inputFile:" + inputFile); } int imageWidth = canvasWidth; int imageHeight = canvasHeight; int offsetX = 0; int offsetY = 0; if (keepAspectRatio) { if (inputImage.getWidth() * 3 <= inputImage.getHeight() * 4) { // 縦長 imageWidth = imageHeight * inputImage.getHeight() / inputImage.getWidth(); offsetX = (canvasWidth - imageWidth) / 2; } else { // 横長 imageHeight = imageWidth * inputImage.getWidth() / inputImage.getHeight(); offsetY = (canvasHeight - imageHeight) / 2; } } ImageFilter imageFilter = new AreaAveragingScaleFilter(imageWidth, imageHeight); Image middleImage = new Canvas().createImage(new FilteredImageSource(inputImage.getSource(), imageFilter)); BufferedImage outputImage = new BufferedImage(canvasWidth, canvasHeight, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = outputImage.createGraphics(); // 透明色が黒く表示されるバグへの対処 graphics.setColor(Color.WHITE); graphics.fill(new Rectangle(canvasWidth, canvasHeight)); graphics.drawImage(middleImage, offsetX, offsetY, imageWidth, imageHeight, null); ImageIO.write(outputImage, "jpeg", outputFile); logger.log(Level.INFO, String.format("%d bytes -> %d bytes (%s->%s)", inputFile.length(), outputFile.length(), inputFile.getPath(), outputFile.getPath())); }
/** * @param img * to scale * @param newWidth * desired width * @param newHeight * desired height * @return a scaled image */ private Image getScaledImage(Image img, int newWidth, int newHeight) { ImageFilter filter = new AreaAveragingScaleFilter(newWidth, newHeight); ImageProducer prod = new FilteredImageSource(img.getSource(), filter); return Toolkit.getDefaultToolkit().createImage(prod); }