Here is the code to generate a barcode based on the Id passed, the barcode is generated fine:
@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();
}
This code places the human readable value above the barcode:
bean.setMsgPosition(HumanReadablePlacement.HRP_TOP);
The human readable value can be placed either at the bottom or top or neither. Is it possible to add the human readable value parallel to the barcode or next to it.
Also could we reduce the size of the human readable value?
Why don't you create a method that allow user to set the parameters of the barcode? Allowing the user to change the barText, rotation, dpi number, & fontSize or anything. The program shall return a string path to the generated barcode image. The image can be stored in temp folder to avoid setting fix folder in any machine.
Here is my code that I used previously...
I'm using barcode4j-2.1.jar, poi-3.9.jar
This way, you can easily reduce the size of the "human readable value" by setting the fontSize
This is the sample of the generated barcode Image.
You can even rotate the barcode easily by setting rotation (e.g. 90)
You can follow SubOptimal's suggestion on placing barcode text next to it.