- import java.awt.image.BufferedImage;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import org.apache.avalon.framework.configuration.Configuration;
- import org.apache.avalon.framework.configuration.DefaultConfiguration;
- import org.apache.avalon.framework.logger.ConsoleLogger;
- import org.apache.avalon.framework.logger.Logger;
- import org.krysalis.barcode4j.BarcodeGenerator;
- import org.krysalis.barcode4j.BarcodeUtil;
- import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
- import org.krysalis.barcode4j.tools.MimeTypes;
- public class Test1
- {
- private transient Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);
- public static void main(String[] args)
- {
- Test1 t =new Test1();
- t.getBarcode();
- System.out.println("success");
- }
- protected void getBarcode()
- {
- String format = MimeTypes.MIME_JPEG;// MimeTypes.MIME_JPEG
- String text = "ABCDE0123456789";
- ByteArrayOutputStream bout = null;
- try
- {
- BarcodeUtil util = BarcodeUtil.getInstance();
- Configuration cfg = buildCfg();
- BarcodeGenerator gen = util.createBarcodeGenerator(cfg);
- bout = new ByteArrayOutputStream(4096);
- int dpi = 300; // 分辨率
- int orientation = 0;
- BitmapCanvasProvider bitmap = new BitmapCanvasProvider(bout,
- format,dpi,BufferedImage.TYPE_BYTE_BINARY,false,
- orientation);
- gen.generateBarcode(bitmap,text);
- bitmap.finish();
- File file = new File("c://"+text+".jpg");
- FileOutputStream fos = new FileOutputStream(file);
- fos.write(bout.toByteArray(),0,bout.size());
- // response.setContentType(format);
- // response.setContentLength(bout.size());
- // response.getOutputStream().write(bout.toByteArray());
- // response.getOutputStream().write(bout.toByteArray());
- // response.getOutputStream().flush();
- }
- catch (Exception e)
- {
- log.error("Error while generating barcode",e);
- }
- finally
- {
- if (bout != null)
- {
- try
- {
- bout.close();
- }
- catch (IOException e)
- {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
- }
- /**
- * Build an Avalon Configuration object from the request.
- *
- * @return the newly built COnfiguration object
- * @todo Change to bean API
- */
- protected Configuration buildCfg()
- {
- DefaultConfiguration cfg = new DefaultConfiguration("barcode");
- // Get type
- String type = "code128";
- DefaultConfiguration child = new DefaultConfiguration(type);
- cfg.addChild(child);
- // DefaultConfiguration attr;
- // // height
- // String height = "100px";
- // attr = new DefaultConfiguration("height");
- // attr.setValue(height);
- // child.addChild(attr);
- //
- // // width
- // String moduleWidth = "300px";
- // attr = new DefaultConfiguration("module-width");
- // attr.setValue(moduleWidth);
- // child.addChild(attr);
- return cfg;
- }
- }