开发者

ColorConvertOp increasing the size of the target image. Is there a way to reduce the size when we go from color to black and white?

开发者 https://www.devze.com 2023-02-14 16:43 出处:网络
I am using java and the following code. Is there a way to use RenderingHints to accomplish this? try {

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

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

        BufferedImage dstImage = null;开发者_如何学C
        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();
    }


I got this working using 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();
0

精彩评论

暂无评论...
验证码 换一张
取 消