0
InputStream bais = new ByteArrayInputStream(b);
BufferedImage bImageFromConvert = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = bImageFromConvert.createGraphics();
g = cvrimg.getGraphics();
g.drawImage(bImageFromConvert, width, height, null);
ImageIO.write(bImageFromConvert, "png", new File("D:\\new.png"));

我收到以下异常: java.lang.IllegalArgumentException: im == null!

我应该怎么办 ?

这是示例代码:

BufferedImage cvrimg = ImageIO.read(file);
int height = cvrimg.getHeight();
int width = cvrimg.getWidth();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
ImageIO.write(new_img, "jpg", baos);
baos.flush();
byte[] imageInByte = baos.toByteArray();
byte[] b = new byte[imageInByte.length];
b[i] = fromUnsignedInt(b2);
InputStream bais = new ByteArrayInputStream(b);
BufferedImage bImageFromConvert = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);

// int inBytes = bais.available();

Graphics g = bImageFromConvert.createGraphics();
g = cvrimg.getGraphics();
g.drawImage(bImageFromConvert, width, height, null);
bImageFromConvert=ImageIO.read(bais);
ImageIO.write(bImageFromConvert, "png", new File("D:\\new.png"));
4

1 回答 1

1

由于您没有读取输入流,因此您遇到了异常。利用

BufferedImage im = ImageIO.read(bais);

来自您的代码的简单转换器,用于读取jpg文件并写入png文件。

BufferedImage cvrimg = ImageIO.read(new File("/path/to/images/old.jpg"));
int height = cvrimg.getHeight();
int width = cvrimg.getWidth();
BufferedImage bImageFromConvert = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D g = bImageFromConvert.createGraphics();
g.drawRenderedImage(cvrimg, null);
ImageIO.write(bImageFromConvert, "png", new File("/path/to/images/new.png"));
于 2012-11-04T10:44:27.707 回答