gpt4 book ai didi

java - 文本文件到图像转换

转载 作者:行者123 更新时间:2023-12-02 00:08:52 25 4
gpt4 key购买 nike

我有一个字符串,我将其转换为 bytes[],然后对其进行编码以返回图像,但问题是它没有将其创建回图像

              BufferedReader reader2 = new BufferedReader(new FileReader("e:\\imageinString.txt"));
String buffer, lined = "";

while ((buffer = reader2.readLine()) != null) {
lined = lined + buffer;
}

byte[] byteArray = lined.getBytes("UTF-16");

InputStream in = new ByteArrayInputStream(byteArray);
BufferedImage bImageFromConvert = ImageIO.read(in);

ImageIO.write(bImageFromConvert, "bmp", new File("e:\\ppp.bmp"));
reader2.close();

我收到此错误,但我在控制台上收到此错误

     Exception in thread "main" java.lang.IllegalArgumentException: image == null!
at javax.imageio.ImageTypeSpecifier.createFromRenderedImage(ImageTypeSpecifier.java:925)
at javax.imageio.ImageIO.getWriter(ImageIO.java:1591)
at javax.imageio.ImageIO.write(ImageIO.java:1520)
at imagereading.Imagereading.main(Imagereading.java:47)

最佳答案

这会对你有帮助。

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_3BYTE_BGR);
Graphics2D g2 = image.createGraphics();
g2.drawString(s, x, y);
...
g2.dispose();
ImageIO.write(image, "jpg", file);

或者,如果您更喜欢导出为 png,那么您可以获得支持透明度的图像。

BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR);

关于java - 文本文件到图像转换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13285609/

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