应用barcode4j生成二维码

最近需要完成一个二维码生成功能,使用了barcode4j进行开发。

ps:附件即为barcode4j的src包和bin包

生成代码如下:

Java代码 
  1. public class CodeService {  
  2.     private static CodeService instance;  
  3.     private static DefaultConfiguration cfg;  
  4.   
  5.     /** 
  6.      * 二维码参数 
  7.      */  
  8.     private static final String FORMAT = MimeTypes.MIME_JPEG;  
  9.     private static final int ORIENTATION = 0;  
  10.     private static final int RESOLUTION = 300;  
  11.     private static final String BARCODE_TYPE = "datamatrix";  
  12.   
  13.     private CodeService() {  
  14.     }  
  15.   
  16.     public static CodeService getInstance() {  
  17.         if (instance == null) {  
  18.             instance = new CodeService();  
Java代码 
  1.                                                cfg = new DefaultConfiguration("barcode");  
  2.         DefaultConfiguration child = new DefaultConfiguration(BARCODE_TYPE );  
  3.   
  4.         cfg.addChild(child);  
  5.   
  6.         DefaultConfiguration attr;  
  7.   
  8.         attr = new DefaultConfiguration("height");  
  9.         attr.setValue(50);  
  10.         child.addChild(attr);  
  11.   
  12.         attr = new DefaultConfiguration("module-width");  
  13.         attr.setValue("0.6");  
  14.         child.addChild(attr);  
  15.   
  16.     }  
  17.   
  18.     return instance;  
  19. }  
  20.   
  21.   
  22. public InputStream getCodeImage(String code, String type) {  
  23.     InputStream ret = null;  
  24.     ByteArrayOutputStream baos = null;  
  25.     BitmapCanvasProvider bitmap = null;  
  26.     try {  
  27.         Configuration cfg = buildCfg(type);  
  28.         BarcodeUtil util = BarcodeUtil.getInstance();  
  29.         BarcodeGenerator gen = util.createBarcodeGenerator(cfg);  
  30.   
  31.         baos = new ByteArrayOutputStream(4096);  
  32.   
  33.         bitmap = new BitmapCanvasProvider(baos, FORMAT, RESOLUTION, BufferedImage.TYPE_BYTE_GRAY, true, ORIENTATION);  
  34.   
  35.         gen.generateBarcode(bitmap, code);  
  36.         bitmap.finish();  
  37.   
  38.         ret = FileUtil.getInputStreamFromBytes(baos.toByteArray());  
  39.   
  40.     } catch (Exception e) {  
  41.         e.printStackTrace();  
  42.     } finally {  
  43.         try {  
  44.             if (baos != null) {  
  45.                 baos.close();  
  46.             }  
  47.             bitmap = null;  
  48.         } catch (Exception e) {  
  49.         }  
  50.     }  
  51.     return ret;  
  52. }  
  53.   
  54. private Configuration buildCfg(String type) {  
  55.     return cfg;  
  56. }  

 

 这个方法使用了datamatrix的二维码生成算法,qr算法二维码似乎这个组件并不支持生成。

ps:调整二维码生成图片大小,需要设置的参数为module-width,设置height不起作用。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值