ColorConvertOp增加了目标图像的大小。当我们从颜色变为黑白时,有没有办法减小尺寸?

时间:2021-12-11 21:20:08

I am using java and the following code. Is there a way to use RenderingHints to accomplish this?

我正在使用java和以下代码。有没有办法使用RenderingHints来实现这一目标?

   try {
        sourceImage = ImageIO.read(new File("images.jpg"));

        BufferedImage dstImage = null;
        ColorSpace colorSpace = ColorSpace.getInstance(ColorSpace.CS_GRAY);
        ColorConvertOp op = new ColorConvertOp(colorSpace, null);
        dstImage = op.filter(sourceImage, null);

        ImageIO.write(dstImage, "jpeg", new File("output.jpg"));

        System.out.println("processing complete");

    } catch (IOException e) {
        e.printStackTrace();
    }

1 个解决方案

#1


0  

I got this working using ImageWriteParam.setCompressionQuality

我使用ImageWriteParam.setCompressionQuality工作了

        Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = (ImageWriter)iter.next();
        // instantiate an ImageWriteParam object with default compression options
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(0.5f);   // an integer between 0 and 1
        // 1 specifies minimum compression and maximum quality

        File file = new File("output.jpg");
        FileImageOutputStream output = new FileImageOutputStream(file);
        writer.setOutput(output);
        IIOImage image = new IIOImage(dstImage, null, null);
        writer.write(null, image, iwp);
        writer.dispose();

#1


0  

I got this working using ImageWriteParam.setCompressionQuality

我使用ImageWriteParam.setCompressionQuality工作了

        Iterator<ImageWriter> iter = ImageIO.getImageWritersByFormatName("jpeg");
        ImageWriter writer = (ImageWriter)iter.next();
        // instantiate an ImageWriteParam object with default compression options
        ImageWriteParam iwp = writer.getDefaultWriteParam();
        iwp.setCompressionMode(ImageWriteParam.MODE_EXPLICIT);
        iwp.setCompressionQuality(0.5f);   // an integer between 0 and 1
        // 1 specifies minimum compression and maximum quality

        File file = new File("output.jpg");
        FileImageOutputStream output = new FileImageOutputStream(file);
        writer.setOutput(output);
        IIOImage image = new IIOImage(dstImage, null, null);
        writer.write(null, image, iwp);
        writer.dispose();