12

Javaプログラムを使用して、画像のサイズ(幅と高さではなく)を縮小する必要があります。これに利用できる優れた API はありますか?

サイズを 1MB から約 50kb - 100kb に減らす必要があります。もちろん、解像度は低下しますが、それは問題ではありません。

4

4 に答える 4

10

このブログ投稿: http://i-proving.com/2006/07/06/java-advanced-imaging/によると、 Java Advanced Imaging Libraryを使用して、必要なことを行うことができます。次のサンプル コードは、出発点として適切です。これにより、高さと幅の両方、および画質の両方で画像のサイズが変更されます。画像が目的のファイル サイズになったら、画像を表示するときに目的のピクセルの高さと幅に戻すことができます。

// read in the original image from an input stream
SeekableStream s = SeekableStream.wrapInputStream(
  inputStream, true);
RenderedOp image = JAI.create("stream", s);
((OpImage)image.getRendering()).setTileCache(null);

// now resize the image

float scale = newWidth / image.getWidth();

RenderedOp resizedImage = JAI.create("SubsampleAverage", 
    image, scale, scale, qualityHints);


// lastly, write the newly-resized image to an
// output stream, in a specific encoding

JAI.create("encode", resizedImage, outputStream, "PNG", null);
于 2011-06-17T19:55:00.007 に答える
6

これは作業コードです

public class ImageCompressor {
    public void compress() throws IOException {
        File infile = new File("Y:\\img\\star.jpg");
        File outfile = new File("Y:\\img\\star_compressed.jpg");

        BufferedInputStream bis = new BufferedInputStream(new FileInputStream(
                infile));
        BufferedOutputStream bos = new BufferedOutputStream(
                new FileOutputStream(outfile));

        SeekableStream s = SeekableStream.wrapInputStream(bis, true);

        RenderedOp image = JAI.create("stream", s);
        ((OpImage) image.getRendering()).setTileCache(null);

        RenderingHints qualityHints = new RenderingHints(
                RenderingHints.KEY_RENDERING,
                RenderingHints.VALUE_RENDER_QUALITY);

        RenderedOp resizedImage = JAI.create("SubsampleAverage", image, 0.9,
                0.9, qualityHints);

        JAI.create("encode", resizedImage, bos, "JPEG", null);

    }

    public static void main(String[] args) throws IOException {

        new ImageCompressor().compress();
    }
}

このコードは私にとって素晴らしい働きをしています。画像のサイズを変更する必要がある場合は、ここでxとyのスケールを変更できますJAI.create("SubsampleAverage", image, xscale,yscale, qualityHints);

于 2011-06-17T22:37:52.460 に答える
0

JAIを使用すると、スケーリングを行わずに、書き込まれたJPEGの圧縮率を上げることができます。http://java.sun.com/products/java-media/jai/forDevelopers/jai1_0_1guide-unc/Encode.doc.htmlを参照してください

于 2011-06-17T20:07:20.053 に答える
0

画像タイプがの実装でサポートされている場合は、このImageWriteParamに示すように品質を調整できます。などの他の方法では、結果を最適化できる場合があります。ImageWriteParamgetBitRate()

于 2011-06-17T20:41:39.333 に答える