openjdk ImageIO.write()时出现Invalid argument to native writeImage

原文链接: https://www.cnblogs.com/shiyun32/p/15918613.html

问题:在本地开发时使用的是oracle jdk8,没有问题,但是生产上面使用的是openjdk-8u252-b09,生成图片时一直报Invalid argument to native writeImage

复制代码
javax.imageio.IIOException: Invalid argument to native writeImage
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeImage(Native Method)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.writeOnThread(JPEGImageWriter.java:1067)
at com.sun.imageio.plugins.jpeg.JPEGImageWriter.write(JPEGImageWriter.java:363)
at javax.imageio.ImageWriter.write(ImageWriter.java:615)
at javax.imageio.ImageIO.doWrite(ImageIO.java:1622)
at javax.imageio.ImageIO.write(ImageIO.java:1548)
复制代码

代码(报错diam):

复制代码
public String exportImg(List<Node> nodes, String piclocation, Font font){
        //1.jpg模板路径
        Resource resource = new ClassPathResource(piclocation);
        String path = "";
        String fileName = "";
        BufferedImage buffImg = null;
        InputStream inputStream = null;
        try {
            inputStream = resource.getInputStream();
            buffImg = ImageIO.read(inputStream);
            //得到画笔对象
            Graphics g = buffImg.getGraphics();
            //设置颜色。
            g.setColor(Color.BLACK);
            Graphics2D tip = buffImg.createGraphics();
            tip.setColor(Color.BLACK);
            font = new Font("宋体", Font.PLAIN, 14);
            tip.setFont(font);
            picDraw(nodes, tip);
            g.dispose();
            path = System.getProperty("java.io.tmpdir") + File.separator + File.separator;
            File file = new File(path);
            file.mkdirs();
            fileName = UUID.randomUUID() + ".jpg";
            ImageIO.write(buffImg, "jpg", new File(path + fileName));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                //关闭流
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (Exception finalExcp) {
                return finalExcp.getMessage();
            }
        }

        return path + fileName;
    }
复制代码

 

解决:

将上面红色的代码改为:

ImageIO.write(buffImg, "png", new File(path + fileName));
posted @ 2022-09-09 17:33  枫树湾河桥  阅读(2307)  评论(0编辑  收藏  举报
Live2D