标签:条码 条形码 outputWidth int 白边 zxing 宽度 new MARGIN
版本:aliyun-sdk-oss-2.7 zxing-core
public static void encode128(String contents,String path) {
//配置条码参数
Map<EncodeHintType,Object> hints = new HashMap<>();
//设置条码两边空白边距为0,默认为10,如果宽度不是条码自动生成宽度的倍数则MARGIN无效
hints.put(EncodeHintType.MARGIN, 0);
//为了无边距,需设置宽度为条码自动生成规则的宽度
int width = new Code128Writer().encode(contents).length;
//前端可控制高度,不影响识别
int height = 70;
//条码放大倍数
int codeMultiples = 1;
//获取条码内容的宽,不含两边距,当EncodeHintType.MARGIN为0时即为条码宽度
int codeWidth = width * codeMultiples;
/* ZXing 条码边距及总宽度-默认计算规则
codeWidth: 自定义的条码宽度
fullWidth: 条码根据编码内容自动生成编码数组长度(new Code128Writer().encode(contents).length)+边距MARGIN
outputWidth: codeWidth 与 fullWidth 的最大值
//放大倍数(取整)
int multiple = outputWidth / fullWidth;
//边距
int leftPadding = (outputWidth - (inputWidth * multiple)) / 2;
生成条码长度为: outputWidth + 2 * leftPadding
*/
try {
// 图像数据转换,使用了矩阵转换 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,
BarcodeFormat.CODE_128, codeWidth, height, hints);
MatrixToImageWriter.writeToStream(bitMatrix, "jpg", new FileOutputStream(new File(path)));
// MatrixToImageWriter.writeToStream(bitMatrix, "png", new FileOutputStream("d:/code39.png"));
} catch (Exception e) {
e.printStackTrace();
}
}
标签:条码,条形码,outputWidth,int,白边,zxing,宽度,new,MARGIN 来源: https://blog.csdn.net/weixin_41461651/article/details/122721303
本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享; 2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关; 3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关; 4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除; 5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。