12

サイズが 170 kb のサイズが 800x800 のイメージがあります。この画像のサイズを 600x600 に変更したいと思います。サイズ変更後、画像サイズを縮小したい。これどうやってするの?

4

3 に答える 3

20

単純に画像のサイズを変更するための本格的な画像処理ライブラリは必要ありません。

推奨されるアプローチは、次のようにプログレッシブ バイリニア スケーリングを使用することです (コード内でこの方法を自由に使用してください)。

public BufferedImage scale(BufferedImage img, int targetWidth, int targetHeight) {

    int type = (img.getTransparency() == Transparency.OPAQUE) ? BufferedImage.TYPE_INT_RGB : BufferedImage.TYPE_INT_ARGB;
    BufferedImage ret = img;
    BufferedImage scratchImage = null;
    Graphics2D g2 = null;

    int w = img.getWidth();
    int h = img.getHeight();

    int prevW = w;
    int prevH = h;

    do {
        if (w > targetWidth) {
            w /= 2;
            w = (w < targetWidth) ? targetWidth : w;
        }

        if (h > targetHeight) {
            h /= 2;
            h = (h < targetHeight) ? targetHeight : h;
        }

        if (scratchImage == null) {
            scratchImage = new BufferedImage(w, h, type);
            g2 = scratchImage.createGraphics();
        }

        g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
                RenderingHints.VALUE_INTERPOLATION_BILINEAR);
        g2.drawImage(ret, 0, 0, w, h, 0, 0, prevW, prevH, null);

        prevW = w;
        prevH = h;
        ret = scratchImage;
    } while (w != targetWidth || h != targetHeight);

    if (g2 != null) {
        g2.dispose();
    }

    if (targetWidth != ret.getWidth() || targetHeight != ret.getHeight()) {
        scratchImage = new BufferedImage(targetWidth, targetHeight, type);
        g2 = scratchImage.createGraphics();
        g2.drawImage(ret, 0, 0, null);
        g2.dispose();
        ret = scratchImage;
    }

    return ret;

}

コードはFilthy Rich Clientsでオリジナルから変更および削除されました。


コメントに基づいて、次のように品質を下げて JPEG バイトをエンコードできます。

imageBufferedImage です。

ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageWriter writer = (ImageWriter) ImageIO.getImageWritersByFormatName("jpeg").next();

ImageWriteParam param = writer.getDefaultWriteParam();
param.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
param.setCompressionQuality(0.2f); // Change this, float between 0.0 and 1.0

writer.setOutput(ImageIO.createImageOutputStream(os));
writer.write(null, new IIOImage(image, null, null), param);
writer.dispose();

osは であるため、ByteArrayOutputStreamBase64 でエンコードできます。

String base64 = Base64.encode(os.toByteArray());
于 2012-10-14T06:13:51.660 に答える
6

100% Java の Apache2 オープン ソースimgscalr ライブラリ(単一の静的クラス) を使用して、次のようなことを行うことができます。

import org.imgscalr.Scalr.*; // static imports are awesome with the lib

... some class code ...

// This is a super-contrived method name, I just wanted to incorporate
// the details from your question accurately.
public static void resizeImageTo600x600(BufferedImage image) {
    ImageIO.write(resize(image, 600), "JPG", new File("/path/to/file.jpg"));
}

: 上記が奇妙に見える場合は、静的インポートにより、Scalr.resize(...)を指定せずにサイズ変更呼び出しを直接使用できます。

さらに、書き出されたスケーリングされた画像の品質が十分によく見えない場合 (ただし、すぐに書き出されます)、次のようにresizeメソッドにさらに引数を使用できます。

public static void resizeImageTo600x600(BufferedImage image) {
    ImageIO.write(resize(image, Method.ULTRA_QUALITY, 600), "JPG", new File("/path/to/file.jpg"));
}

.. BufferedImageOp を結果に適用して、縮小によって画像がギザギザに見える場合に柔らかくすることもできます。

public static void resizeImageTo600x600(BufferedImage image) {
    ImageIO.write(resize(image, Method.ULTRA_QUALITY, 600, Scalr.OP_ANTIALIAS), "JPG", new File("/path/to/file.jpg"));
}

Maven POM に次の dep エントリを追加するだけで、ライブラリをいじり始めることができます (imgscar は Maven 中央リポジトリにあります)。

<dependency> 
  <groupId>org.imgscalr</groupId>
  <artifactId>imgscalr-lib</artifactId>
  <version>4.2</version>
  <type>jar</type>
  <scope>compile</scope>
</dependency>
于 2012-10-17T16:45:24.467 に答える
3

IamageJ など、Java による画像処理をサポートする優れたフレームワークを使用する

また、MemoryImageSourcePixelGrabberなどのいくつかのクラスを介して Java で利用できる基本的なサポートもあります。

于 2012-10-14T05:24:47.320 に答える