1

当我尝试调整图像大小时,我收到一个异常消息“java.lang.IllegalStateException:给定格式没有 ImageReader”。

 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();
    }
4

2 回答 2

3

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

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();
        }
于 2015-09-24T09:51:42.680 回答
2
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到给定尺寸并返回 new bufferedImage

于 2015-09-23T13:26:04.703 回答