View Javadoc

1   /***********************************************************************************************************************
2   Copyright (c) 2003, International Barcode Consortium
3   All rights reserved.
4   
5   Redistribution and use in source and binary forms, with or without modification,
6   are permitted provided that the following conditions are met:
7   
8       * Redistributions of source code must retain the above copyright notice, this list of
9         conditions and the following disclaimer.
10      * Redistributions in binary form must reproduce the above copyright notice, this list of
11        conditions and the following disclaimer in the documentation and/or other materials
12        provided with the distribution.
13      * Neither the name of the International Barcode Consortium nor the names of any contributors may be used to endorse
14        or promote products derived from this software without specific prior written permission.
15  
16  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
17  IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY
18  AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
19  CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
21  SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
22  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23  OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
24  POSSIBILITY OF SUCH DAMAGE.
25  ***********************************************************************************************************************/
26  
27  package net.sourceforge.barbecue;
28  
29  import java.io.*;
30  import java.awt.*;
31  import java.awt.image.BufferedImage;
32  import javax.imageio.ImageIO;
33  import java.util.*;
34  import net.sourceforge.barbecue.output.OutputException;
35  
36  /**
37   * Utility class to provide convenience methods for converting barcodes
38   * to images and other misc barcode handling.
39   *
40   * @author <a href="mailto:opensource@ianbourke.com">Ian Bourke</a>
41   * @author Sean Sullivan
42   * 
43   */
44  public final class BarcodeImageHandler {
45  
46  	private static Set formats;
47  	
48  	static
49  	{
50  		Set s = new HashSet();
51  		s.add("gif");
52  		s.add("jpeg");
53  		s.add("png");
54  		formats = Collections.unmodifiableSet(s);
55  	}
56  	
57  	static public Set getImageFormats()
58  	{
59  		return formats;
60  	}
61  	
62  	private BarcodeImageHandler() {
63  	}
64  
65  
66  	/**
67  	 * Creates an image for a barcode that can be used in other GUI
68  	 * components (e.g. ImageIcon, JLabel etc).
69  	 * @param barcode The barcode to convert into an image
70  	 * @return The image
71  	 */
72  	public static BufferedImage getImage(Barcode barcode) throws OutputException {
73          BufferedImage bi = new BufferedImage(barcode.getWidth(), barcode.getHeight(), BufferedImage.TYPE_BYTE_INDEXED);
74          Graphics2D g = bi.createGraphics();
75          barcode.draw(g, 0, 0);
76          bi.flush();
77          return bi;
78      }
79  
80  	/**
81  	 * write a JPEG image to an OutputStream
82  	 * 
83  	 * @param barcode The barcode to output
84  	 * @param os The output stream to write the image to
85  	 * @throws OutputException If the image cannot be written to the output stream correctly
86  	 */
87  	public static void writeJPEG(Barcode barcode, OutputStream os) throws OutputException {
88  		writeImage(barcode, "jpeg", os);
89  	}
90  
91  	public static void saveJPEG(Barcode barcode, File f) throws OutputException {
92  		saveImage(barcode, "jpeg", f);
93  	}
94  
95  	public static void savePNG(Barcode barcode, File f) throws OutputException {
96  		saveImage(barcode, "png", f);
97  	}
98  	
99  	public static void saveGIF(Barcode barcode, File f) throws OutputException {
100 		saveImage(barcode, "gif", f);
101 	}
102 	
103 	private static void saveImage(Barcode barcode, String format, File f) throws OutputException {
104 	
105 		FileOutputStream fos = null;
106 		
107 		try {
108 			fos = new FileOutputStream(f);
109 			writeImage(barcode, format, fos);
110 			fos.flush();
111 		}
112 		catch (IOException ex) {
113 			throw new OutputException(ex);
114 		}
115 		finally {
116 			try {
117 				fos.close();
118 			}
119 			catch (IOException ignored) {
120 				// ignored
121 			}
122 		}
123 	}
124 	
125 	
126 	/**
127 	 * write a PNG image to an OutputStream
128 	 * 
129 	 * @param barcode The barcode to output
130 	 * @param os The output stream to write the image to
131 	 * @throws OutputException If the image cannot be written to the output stream correctly
132 	 */
133 	public static void writePNG(Barcode barcode, OutputStream os) throws OutputException {
134 		writeImage(barcode, "png", os);
135 	}
136 
137 	/**
138 	 * write a GIF image to an OutputStream
139 	 * 
140 	 * @param barcode The barcode to output
141 	 * @param os The output stream to write the image to
142 	 * @throws OutputException If the image cannot be written to the output stream correctly
143 	 */
144 	public static void writeGIF(Barcode barcode, OutputStream os) throws OutputException {
145 		writeImage(barcode, "gif", os);
146 	}
147 	
148 	
149 	/**
150 	 * 
151 	 * @param barcode The barcode to output
152 	 * @param formatName
153 	 * @param os The output stream
154 	 * @throws OutputException If an error occurred writing to the stream
155 	 */
156 	private static void writeImage(Barcode barcode, String formatName, OutputStream os) throws OutputException {
157 		BufferedImage image = getImage(barcode);
158 		try {
159 			ImageIO.write(image, formatName, os);
160 		}
161 		catch (IOException e) {
162 			throw new OutputException("exception while writing image", e);
163 		}
164 	}
165 }