这是根据传递的 Id 生成条形码的代码,条形码生成良好:
@Override
public byte[] generateBarcodeForId(String Id) throws VisitMastException{
BarcodeUtil util = BarcodeUtil.getInstance();
BarcodeGenerator gen;
ByteArrayOutputStream bao = null;
try {
bao = new ByteArrayOutputStream();
//Create the barcode bean
Code128Bean bean = new Code128Bean();
int dpi = 150;
//Configure the barcode generator
bean.setModuleWidth(UnitConv.in2mm(1.1f / dpi)); //makes the narrow bar, width exactly one pixel
bean.doQuietZone(true);
bean.setBarHeight(4);
//bean.setVerticalQuietZone(3);
bean.setQuietZone(0);
bean.setMsgPosition(HumanReadablePlacement.HRP_TOP);
BitmapCanvasProvider canvas = new BitmapCanvasProvider(
bao, "image/jpeg", dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
bean.generateBarcode(canvas, Id);
canvas.finish();
} catch (IOException e) {
throw new VisitMastException(VisitMastException.BAD_REQUEST,
messageSource.getMessage(CodeEnum.BARCODE_GENERATING_ERROR.getValue(), null, Locale.ENGLISH));
}
return bao.toByteArray();
}
此代码将人类可读的值放在条形码上方:
bean.setMsgPosition(HumanReadablePlacement.HRP_TOP);
人类可读的值可以放置在底部或顶部,或者两者都不放置。是否可以在条形码旁边或旁边添加人类可读的值。
我们还可以减小人类可读值的大小吗?