加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

生成一维码的方法(用这个的原因是因为它所用的依赖是可以在mave

发布时间:2020-12-13 22:37:04 所属栏目:百科 来源:网络整理
导读: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.
 
  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. }

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读