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"));
您收到异常是因为您没有读取输入流。使用
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"));