Generate Barcode Using Custom Width

Below are code snippets that create custom width bar code.

Create an Image With Exact Barcode Only

String dataDir = Utils.getDataDir(CreateAnImageWithExactBarcodeOnly.class) + "TechnicalArticles/";
// Generate the barcode
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.CODE_128);
// Set the code text
generator.getParameters().getBarcode().getCodeTextParameters().setTwoDDisplayText("123456");
// Get bitmap with exact barcode only
BufferedImage image = generator.generateBarCodeImage();
// Saving the buffered image
File outputfile = new File(dataDir + "custombarcode.png");
ImageIO.write(image, "png", outputfile);

Sample image

todo:image_alt_text

Below are code snippets that create custom width barcode.

Create and Set Size for the Whole Picture With Barcode

String dataDir = Utils.getDataDir(CreateAndSetSizeForWholePictureWithBarcode.class) + "TechnicalArticles/";
// Generate the bar code
BarcodeGenerator generator = new BarcodeGenerator(EncodeTypes.PDF_417);
// Set the code text
generator.setCodeText("One thing 2 thing");
// Set the code text location
generator.getParameters().getBarcode().getCodeTextParameters().setLocation(CodeLocation.NONE);
// Set margins
generator.getParameters().getBarcode().getPadding().getBottom().setPixels(0);
generator.getParameters().getBarcode().getPadding().getLeft().setPixels(0);
generator.getParameters().getBarcode().getPadding().getRight().setPixels(0);
generator.getParameters().getBarcode().getPadding().getTop().setPixels(0);
// Get BufferedImage with exact bar code only
BufferedImage img = generator.generateBarCodeImage();
// Saving the buffered image
File outputfile = new File(dataDir + "custombarcode.png");
ImageIO.write(img, "png", outputfile);
todo:image_alt_text
Figure: Sample image