gpt4 book ai didi

java - 将随机字符串图像写入资源

转载 作者:行者123 更新时间:2023-12-01 12:55:49 26 4
gpt4 key购买 nike

我想生成一个随机字符串,然后需要将其转换为图像。我想将此图像存储在我的 spring 资源文件夹中。

@Service
public class CapchaServiceImpl implements CapchaService{

@Override
public void CapchaString() {
String capchaString = UUID.randomUUID().toString();
ByteArrayInputStream bais = new ByteArrayInputStream(capchaString.getBytes());
try {
BufferedImage bi=ImageIO.read(bais);
File outputfile = new File("/resources/saved.png");
ImageIO.write(bi, "png", outputfile);

} catch (IOException e) {
throw new RuntimeException(e);
}
}

}

但是,当我使用这个时,我得到了 java.lang.IllegalArgumentException: image == null!异常(exception)。为什么以及我需要做什么?

enter image description here

最佳答案

试试这个

    public static void main(String args[])
{

String capchaString = UUID.randomUUID().toString();
// ByteArrayInputStream bais = new ByteArrayInputStream(capchaString.getBytes());
try {
BufferedImage bi=new BufferedImage(100, 100, BufferedImage.TYPE_INT_RGB);
Graphics g = bi.getGraphics();
g.drawString(capchaString, 10, 10);

File outputfile = new File("output.png");
ImageIO.write(bi, "png", outputfile);

} catch (IOException e) {
throw new RuntimeException(e);
}


}

关于java - 将随机字符串图像写入资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23910818/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com