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"));

I am getting th following exception: java.lang.IllegalArgumentException: im == null!

What should I do ?

This is the sample code:

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"));
Roman C
  • 49,761
  • 33
  • 66
  • 176
Vivek Parekh
  • 11
  • 1
  • 2
  • There is no "im" variable in the code you sent us. The answer lies somewhere else. – Sanchit Nov 04 '12 at 10:36
  • "im" is the 1st parameter of ImageIO.write(). – Vivek Parekh Nov 04 '12 at 10:41
  • This is in the ImageIO library. `public static boolean write(RenderedImage im, String formatName, ImageOutputStream output) throws IOException { if (im == null) { throw new IllegalArgumentException("im == null!"); }` – Vivek Parekh Nov 04 '12 at 10:41
  • 2
    Well you have figured out the important piece of code then. Now just put a debugger on the first line and check which variables are null. – Sanchit Nov 04 '12 at 10:44
  • For better help sooner, post an [SSCCE](http://sscce.org/). – Andrew Thompson Nov 04 '12 at 11:03
  • the thing is none of the values are null. "b" returns `[B@34fbb7cb` "bais" returns `java.io.ByteArrayInputStream@79df8b99` "bImageFromConvert" returns `BufferedImage@43462851: type = 1 DirectColorModel: rmask=ff0000 gmask=ff00 bmask=ff amask=0 IntegerInterleavedRaster: width = 79 height = 22 #Bands = 3 xOff = 0 yOff = 0 dataOffset[0] 0` – Vivek Parekh Nov 04 '12 at 11:06
  • i want to know why am i not able to write the image file. – Vivek Parekh Nov 04 '12 at 11:06
  • *"i want to know why am i not able to write the image file."* I (and many others) could probably tell you within 10 minutes of seeing an SSCCE. – Andrew Thompson Nov 04 '12 at 13:14
  • @AndrewThompson - i post the SSCCE already..!! – Vivek Parekh Nov 07 '12 at 06:15
  • 1
    No you didn't. An SSCCE is not code snippets. Please read the article again and if there is anything you do not understand, ask me. I should be able to explain, ..since I wrote it. ;) – Andrew Thompson Nov 07 '12 at 06:22

1 Answers1

1

You are getting exception because you didn't read the input stream. Use

BufferedImage im = ImageIO.read(bais);

A simple converter from your code that reads a jpg file and writes to png file.

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"));
Roman C
  • 49,761
  • 33
  • 66
  • 176
  • @VivekParekh Look at the example I've made what you wanted to achieve. – Roman C Nov 04 '12 at 16:40
  • That is not a problem .. even when i read a png file and try to write the it in a new png file... i get the value im == null. im is the parameter that is passed by the ImageIO.write(RenderedImage im, ...,...) – Vivek Parekh Nov 07 '12 at 06:17
  • @VivekParekh You should write the image that you has drawn, not that you have created. – Roman C Nov 07 '12 at 09:15