问题
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?
回答1:
This is not supported out of the box by Barcode4J. One solution (aside adding this feature to Barcode4J) could be to create a new image with the double size and copy the barcode and the text area into it.
Find a small PoC snippet demonstrating the general idea.
BitmapCanvasProvider canvas = new BitmapCanvasProvider(dpi,
BufferedImage.TYPE_BYTE_BINARY, false, 0);
bean.generateBarcode(canvas, Id);
canvas.finish();
BufferedImage image = canvas.getBufferedImage();
BufferedImage temp = new BufferedImage(image.getWidth() * 2,
image.getHeight() / 2 - 1, image.getType());
Graphics2D g = temp.createGraphics();
g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
RenderingHints.VALUE_ANTIALIAS_OFF);
g.drawImage(image, 0, -image.getHeight() / 2, null);
g.drawImage(image, image.getWidth(), 0, null);
g.dispose();
bao.reset();
ImageIO.write(temp, "png", bao);
The generated bytes are stored to file as
byte[] byteArray = generateBarcodeForId("1111");
BufferedImage image = ImageIO.read(new ByteArrayInputStream(byteArray));
ImageIO.write(image, "jpg", new File("code128.jpg"));
the resulting image code128.jpg
.
Another possiblity could be to generate the barcode with HumanReadablePlacement.HRP_NONE
and later draw the text using canvas.deviceText(...)
.
回答2:
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...
public class CustomBarcode {
public String getBufferedBarcodeImage(String barText, Integer rotation,
Integer dpi2, double fontSize) throws IOException {
ByteArrayOutputStream os = null;
ByteArrayInputStream fis = null;
OutputStream out = null;
// Configure the barcode generator
Code128Bean barcode128Bean = new Code128Bean();
barcode128Bean.setCodeset(Code128Constants.CODESET_A);
final int dpi = dpi2;
barcode128Bean.setBarHeight(15.0);
barcode128Bean.setFontSize(fontSize);
barcode128Bean.setQuietZone(5.0);
barcode128Bean.doQuietZone(true);
barcode128Bean.setModuleWidth(UnitConv.in2mm(1.6f / dpi)); // makes the
// narrow
// bar
String mime = MimeTypes.MIME_PNG;
File temp = null;
String tempFile = "";
try {
os = new ByteArrayOutputStream();
BitmapCanvasProvider canvasProvider = new BitmapCanvasProvider(os,
"image/x-png", dpi, BufferedImage.TYPE_BYTE_BINARY, false,
rotation);
barcode128Bean.generateBarcode(canvasProvider, barText);
canvasProvider.finish();
final BitmapEncoder encoder = BitmapEncoderRegistry
.getInstance(mime);
encoder.encode(canvasProvider.getBufferedImage(), os, mime, dpi); // get
// created
// barcode
fis = new ByteArrayInputStream(os.toByteArray());
temp = File.createTempFile("barcode", ".png");
IOUtils.copy(fis, new FileOutputStream(temp));
tempFile = temp.getAbsolutePath();
System.out.println("tempFile :" + tempFile);
temp.deleteOnExit();
// byte[] imageData = os.toByteArray();
}
catch (IOException ex) {
System.out.println("An Exception");
ex.printStackTrace();
}
finally {
os.flush();
os.close();
fis.close();
}
return tempFile;
}
}
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.
来源:https://stackoverflow.com/questions/37177314/barcode-human-readable-placing-parallel-to-barcode