如何使用 im4java 调整缓冲图像的大小并将输出作为 bufferedImage?

How to resize a buffered Image using im4java and get the output as bufferedImage?

当我尝试调整图像大小时出现异常 "java.lang.IllegalStateException: no ImageReader for given format"。

 public static void main(String... args) throws Exception {   
        IMOperation op = new IMOperation();
        op.addImage();                       
        op.resize(350)
        op.addImage()               
        BufferedImage images = ImageIO.read(new File("image.jpg")); 

        // set up command
        ConvertCmd convert = new ConvertCmd();
        Stream2BufferedImage s2b = new Stream2BufferedImage();
        convert.setOutputConsumer(s2b);

        // run command and extract BufferedImage from OutputConsumer
        convert.run(op,images);
        BufferedImage img = s2b.getImage();
    }
BufferedImage images = ImageIO.read(new File("image.jpg")); 
    int type = (images.getTransparency() == Transparency.OPAQUE)
            ? BufferedImage.TYPE_INT_RGB
            : Buffenter code hereeredImage.TYPE_INT_ARGB;
    BufferedImage finalImage = images;
    int w = 40;
    int h = 40;
    BufferedImage resizedImage = new BufferedImage(w, h, type);
    Graphics2D g2 = resizedImage.createGraphics();
    g2.drawImage(finalImage, 0, 0, null);
    g2.dispose();
    finalImage = resizedImage;

这会将 bufferedImage 调整为给定的尺寸和 returns 新的 bufferedImage

伙计们,我已经解决了这个问题。实际上我必须指定输出图像的图像类型

public static void main(String... args) throws Exception {   
           IMOperation op = new IMOperation();
            op.addImage();                       
            op.resize(350)
            op.addImage("jpg:-")               
            BufferedImage images = ImageIO.read(new File("image.jpg")); 

            // set up command
            ConvertCmd convert = new ConvertCmd();
            Stream2BufferedImage s2b = new Stream2BufferedImage();
            convert.setOutputConsumer(s2b);

            // run command and extract BufferedImage from OutputConsumer
            convert.run(op,images);
            BufferedImage img = s2b.getImage();
        }