0

I want to generate a random string and then I need to convert it into an image. I want to store this image in my resource folder of 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);
          }
      }

}

However, when I used this, I am getting java.lang.IllegalArgumentException: image == null! Exception. Why and what do I need to do?

enter image description here

user2985842
  • 437
  • 9
  • 24

2 Answers2

1

Try this

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


    }
Hirak
  • 3,601
  • 1
  • 22
  • 33
  • well i used same piece of code. However i am getting "class path resource [output.png] cannot be resolved to URL because it does not exist" – user2985842 May 28 '14 at 14:20
  • and when i tried to give full path resources/output.png...I am getting NULL Point Exception – user2985842 May 28 '14 at 14:21
  • Can you post the stacktrace for the exception – Hirak May 28 '14 at 14:25
  • Is it working in a static project? Is there any way I can replicate this null pointer exception? – Hirak May 28 '14 at 14:35
  • i am using dynamic project. i really dont know. – user2985842 May 28 '14 at 14:36
  • Can you try ImageOutputStream out = new FileImageOutputStream(new File( "output.png")); ImageIO.write(bi, "png", out); – Hirak May 28 '14 at 14:38
  • when i tried this. i am getting "HTTP Status 500 - class path resource [output.png] cannot be resolved to URL because it does not exist" – user2985842 May 28 '14 at 14:44
  • Now you are getting the actual error. It is not finding path of the output that you are providing. Are you trying to save the file into classpath? can you refer http://stackoverflow.com/questions/4714645/how-can-i-save-a-file-to-the-class-path – Hirak May 28 '14 at 15:11
0

You're doing the equivalent of creating a text file, renaming it to .png and expecting it to magically become an image.

In order to create an image with some text on it, you have to

  • create a new BufferedImage (How large should it be? Fixed size, or calculated by text and font metrics?)
  • paint the text on it (by obtaining a Graphics context for the image)
  • save the image as a PNG (using ImageIO)
Peter Walser
  • 15,208
  • 4
  • 51
  • 78