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.twod.pdf417;
28  
29  import net.sourceforge.barbecue.BarcodeException;
30  import net.sourceforge.barbecue.BlankModule;
31  import net.sourceforge.barbecue.Module;
32  import net.sourceforge.barbecue.linear.LinearBarcode;
33  import net.sourceforge.barbecue.output.Output;
34  import net.sourceforge.barbecue.output.GraphicsOutput;
35  import net.sourceforge.barbecue.output.OutputException;
36  
37  import java.awt.*;
38  import java.awt.image.BufferedImage;
39  
40  /**
41   * Implementation of the PDF417 two dimensional barcode format.
42   *
43   * <p/>Contributed by Alex Ferrer <alex@ftconsult.com>
44   *
45   * @author <a href="mailto:opensource@ianbourke.com">Ian Bourke</a>
46   */
47  // TODO: Make this extend TwoDimensionalBarcode (and implement that) - NOT linear barcode
48  public class PDF417Barcode extends LinearBarcode {
49  	private PDF417Module module;
50  	
51  	/**
52  	 * Constructs a new new PDF417 barcode with the specified data.
53  	 * @param data The data to encode
54  	 * @throws BarcodeException If the data to be encoded is invalid
55  	 */
56  	public PDF417Barcode(String data) throws BarcodeException {
57  		super(data);
58  		this.barWidth = 1;
59  		this.drawingText = false;
60  	}
61  
62  	/**
63  	 * Does nothing. Text is never drawn for PDF417.
64  	 * @param drawingText Ignored
65  	 */
66  	public void setDrawingText(boolean drawingText) {
67  		// Do nothing - we never draw the text for PDF417
68  	}
69  
70  	/**
71  	 * Does nothing. Fixed width for PDF417.
72  	 * @param barWidth Ignored
73  	 */
74  	public void setBarWidth(int barWidth) {
75  		// Fixed width
76  	}
77  
78  	/**
79  	 * Does nothing. Fixed height for PDF417.
80  	 * @param barHeight Ignored
81  	 */
82  	public void setBarHeight(int barHeight) {
83  		// Fixed height
84  	}
85  
86  	/**
87  	 * Does nothing. Fixed resolution for PDF417.
88  	 * @param resolution Ignored
89  	 */
90  	public void setResolution(int resolution) {
91  		// Fixed res
92  	}
93  
94  	/**
95  	 * Returns the minimum allowed height for the barcode for the given resolution.
96  	 * @param resolution The output resolution
97  	 * @return The minimum allowed barcode height
98  	 */
99  	protected int calculateMinimumBarHeight(int resolution) {
100 		initBarcode(data);
101 		return module.getBarcodeHeight();
102 	}
103 
104 	/**
105 	 * Returns the encoded data for the barcode.
106 	 * @return An array of modules that represent the data as a barcode
107 	 */
108 	protected Module[] encodeData() {
109 		initBarcode(data);
110 		return new Module[] {new PDF417Module(data)};
111 	}
112 
113 	/**
114 	 * Returns the checksum for the barcode, pre-encoded as a Module.
115 	 * @return A blank module
116 	 */
117 	protected Module calculateChecksum() {
118 		return new BlankModule(0);
119 	}
120 
121 	/**
122 	 * Returns the pre-amble for the barcode.
123 	 * @return A blank module
124 	 */
125 	protected Module getPreAmble() {
126 		return new BlankModule(0);
127 	}
128 
129 	/**
130 	 * Returns the post-amble for the barcode.
131 	 * @return A blank module
132 	 */
133 	protected Module getPostAmble() {
134 		return new BlankModule(0);
135 	}
136 	
137 	private void initBarcode(String data) {
138 		if (module == null) {
139 			this.module = new PDF417Module(data);
140 			Output params = new GraphicsOutput(
141 					(Graphics2D) new BufferedImage(1000, 1000,
142 					                 BufferedImage.TYPE_BYTE_GRAY).getGraphics(),
143 					null, Color.black, Color.white);
144 			try {
145 				module.draw(params, 0, 0, barWidth, barHeight);
146 			}
147 			catch (OutputException e) {
148 				// TODO: Something
149 			}
150 		}
151 	}
152 }