View Javadoc

1   /*
2    * JCaptcha, the open source java framework for captcha definition and integration
3    * Copyright (c)  2007 jcaptcha.net. All Rights Reserved.
4    * See the LICENSE.txt file distributed with this package.
5    */
6   
7   /*
8    * jcaptcha, the open source java framework for captcha definition and integration
9    * copyright (c)  2007 jcaptcha.net. All Rights Reserved.
10   * See the LICENSE.txt file distributed with this package.
11   */
12  
13  /*
14   * jcaptcha, the open source java framework for captcha definition and integration
15   * copyright (c)  2007 jcaptcha.net. All Rights Reserved.
16   * See the LICENSE.txt file distributed with this package.
17   */
18  
19  package com.octo.captcha.engine.image.utils;
20  
21  import javax.imageio.ImageIO;
22  import javax.imageio.ImageWriter;
23  import javax.imageio.stream.ImageOutputStream;
24  import java.awt.image.BufferedImage;
25  import java.io.File;
26  import java.io.FileOutputStream;
27  import java.io.IOException;
28  import java.io.OutputStream;
29  
30  /***
31   * <p>Description: </p>
32   *
33   * @author <a href="mailto:mag@jcaptcha.net">Marc-Antoine Garrigue</a>
34   * @version 1.0
35   */
36  public class ImageToFile {
37  
38      private static ImageWriter writer = (ImageWriter)ImageIO.getImageWritersByFormatName("jpeg").next();
39      public ImageToFile() {
40      }
41  
42      public static void serialize(BufferedImage image, File file)
43              throws IOException {
44          file.createNewFile();
45          FileOutputStream fos = new FileOutputStream(file);
46          encodeJPG(fos, image);
47          fos.flush();
48          fos.close();
49      }
50  
51      public static void encodeJPG(OutputStream sos, BufferedImage image)
52              throws IOException {
53          ImageOutputStream ios = ImageIO.createImageOutputStream(sos);
54          writer.setOutput(ios);
55          writer.write(image);        
56      }
57  
58  
59  }
60