生成一维码的方法(用这个的原因是因为它所用的依赖是可以在maven中央仓库能找到的,不用付费的)

前端之家收集整理的这篇文章主要介绍了生成一维码的方法(用这个的原因是因为它所用的依赖是可以在maven中央仓库能找到的,不用付费的)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
 
  1. import java.awt.image.BufferedImage;
  2. import java.io.ByteArrayOutputStream;
  3. import java.io.File;
  4. import java.io.FileOutputStream;
  5. import java.io.IOException;

  6. import org.apache.avalon.framework.configuration.Configuration;
  7. import org.apache.avalon.framework.configuration.DefaultConfiguration;
  8. import org.apache.avalon.framework.logger.ConsoleLogger;
  9. import org.apache.avalon.framework.logger.Logger;
  10. import org.krysalis.barcode4j.BarcodeGenerator;
  11. import org.krysalis.barcode4j.BarcodeUtil;
  12. import org.krysalis.barcode4j.output.bitmap.BitmapCanvasProvider;
  13. import org.krysalis.barcode4j.tools.MimeTypes;

  14. public class Test1
  15. {
  16. private transient Logger log = new ConsoleLogger(ConsoleLogger.LEVEL_INFO);

  17. public static void main(String[] args)
  18. {
  19. Test1 t =new Test1();
  20. t.getBarcode();
  21. System.out.println("success");
  22. }

  23. protected void getBarcode()
  24. {
  25. String format = MimeTypes.MIME_JPEG;// MimeTypes.MIME_JPEG

  26. String text = "ABCDE0123456789";

  27. ByteArrayOutputStream bout = null;
  28. try
  29. {
  30. BarcodeUtil util = BarcodeUtil.getInstance();

  31. Configuration cfg = buildCfg();
  32. BarcodeGenerator gen = util.createBarcodeGenerator(cfg);

  33. bout = new ByteArrayOutputStream(4096);

  34. int dpi = 300; // 分辨率
  35. int orientation = 0;

  36. BitmapCanvasProvider bitmap = new BitmapCanvasProvider(bout,
  37. format,dpi,BufferedImage.TYPE_BYTE_BINARY,false,
  38. orientation);

  39. gen.generateBarcode(bitmap,text);
  40. bitmap.finish();

  41. File file = new File("c://"+text+".jpg");
  42. FileOutputStream fos = new FileOutputStream(file);
  43. fos.write(bout.toByteArray(),0,bout.size());

  44. // response.setContentType(format);
  45. // response.setContentLength(bout.size());
  46. // response.getOutputStream().write(bout.toByteArray());
  47. // response.getOutputStream().write(bout.toByteArray());
  48. // response.getOutputStream().flush();

  49. }
  50. catch (Exception e)
  51. {
  52. log.error("Error while generating barcode",e);

  53. }
  54. finally
  55. {
  56. if (bout != null)
  57. {
  58. try
  59. {
  60. bout.close();
  61. }
  62. catch (IOException e)
  63. {
  64. // TODO Auto-generated catch block
  65. e.printStackTrace();
  66. }
  67. }

  68. }

  69. }

  70. /**
  71. * Build an Avalon Configuration object from the request.
  72. *
  73. * @return the newly built COnfiguration object
  74. * @todo Change to bean API
  75. */
  76. protected Configuration buildCfg()
  77. {
  78. DefaultConfiguration cfg = new DefaultConfiguration("barcode");
  79. // Get type

  80. String type = "code128";
  81. DefaultConfiguration child = new DefaultConfiguration(type);
  82. cfg.addChild(child);

  83. // DefaultConfiguration attr;
  84. // // height
  85. // String height = "100px";
  86. // attr = new DefaultConfiguration("height");
  87. // attr.setValue(height);
  88. // child.addChild(attr);
  89. //
  90. // // width
  91. // String moduleWidth = "300px";
  92. // attr = new DefaultConfiguration("module-width");
  93. // attr.setValue(moduleWidth);
  94. // child.addChild(attr);

  95. return cfg;
  96. }

  97. }
原文链接:https://www.f2er.com/javaschema/285070.html

猜你在找的设计模式相关文章