Javaを用いて画像のリサイズを行いたいです。
現状はリサイズはできているのですが、
リサイズ後の画質が悪く、画質の劣化が少ない方法を探しております。
リサイズの参考にしたのがこちらです。
要件としては、
・画質の劣化が少ない
・引数としてBufferdImage形式もしくはFile形式で画像データを受け取って、リサイズ処理をさせて、BufferdImage形式もしくはFile形式とデータを出力させたい
・できるだけ処理スピードは早くしたい
です。
・リサイズのサイズを指定して、16:9の比率で短い方を基準にトリミングもしたい
なお、現在のコードは以下の通りです。
PlayFrameworkを利用しているため、FilePartというクラスも存在しますが、
基本Javaで処理させています。
こちらを元にアドバイスも頂けたらと思います。
宜しく御願い致します。
public static Boolean reduction(FilePart originalImage, String thumbnailName, double width, double height) { // サムネイル画像を表示させるかどうかの判定 Boolean isThumbnail = false; if (originalImage == null) { return isThumbnail; } // 原画をBufferedImageに変換 BufferedImage originalBufferedImage; try { originalBufferedImage = ImageIO.read(originalImage.getFile()); } catch (IOException e) { e.printStackTrace(); return isThumbnail; } ByteArrayOutputStream baos = new ByteArrayOutputStream(); // サイズ設定 double w = 0; double h = 0; if (originalBufferedImage.getWidth() * 16 > originalBufferedImage.getHeight() * 9) { h = height; w = originalBufferedImage.getWidth() * (height / originalBufferedImage.getHeight()); if (width > w) { h = h * (width / w); w = width; } } else { w = width; h = originalBufferedImage.getHeight() * (width / originalBufferedImage.getWidth()); if (height > h) { w = w * (height / h); h = height; } } BufferedImage thumbnailImage = resize(originalBufferedImage, (int) width, (int) height, (int) w, (int) h); try { baos.close(); } catch (IOException e1) { // TODO 自動生成された catch ブロック e1.printStackTrace(); return isThumbnail; } // 原画のwidthとheiightを取得 Integer originalWidth = originalBufferedImage.getWidth(); Integer originalHeight = originalBufferedImage.getHeight(); // 原画のサイズと指定のサイズの比較 if (!(originalWidth > width && originalHeight > height)) { return isThumbnail; } try { uploadFile(originalImage, thumbnailImage, thumbnailName); isThumbnail = true; } catch (IOException e) { e.printStackTrace(); } return isThumbnail; } // 画像リサイズ処理 private static BufferedImage resize(BufferedImage bufferedImage, Integer layoutWidth, Integer layoutHeight, Integer imageWidth, Integer imageHeight) { BufferedImage scaledBI = new BufferedImage(layoutWidth, layoutHeight, BufferedImage.TYPE_INT_RGB); Graphics2D g = scaledBI.createGraphics(); g.setColor(Color.WHITE); g.fillRect(0, 0, layoutWidth, layoutHeight); Integer x = (layoutWidth - imageWidth) / 2; Integer y = (layoutHeight - imageHeight) / 2; g.drawImage(bufferedImage, x, y, imageWidth, imageHeight, null); g.dispose(); return scaledBI; }
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。