Barcode for human reading, parallel to barcode.

Here is the code to create a barcode based on the transmitted identifier, the barcode is generated perfectly:

 @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();
 }

Sample created barcode image

This code places a human-readable value above the barcode:

bean.setMsgPosition(HumanReadablePlacement.HRP_TOP);

A human-readable value can be placed either below, or above, or not. Is it possible to add a human-readable value parallel to or next to the barcode.

Can one also reduce the size of the human readable value?

+3
source share
2 answers

Barcode4J . ( Barcode4J) - .

PoC, .

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);

byte[] byteArray = generateBarcodeForId("1111");
BufferedImage image = ImageIO.read(new ByteArrayInputStream(byteArray));
ImageIO.write(image, "jpg", new File("code128.jpg"));

code128.jpg.

enter image description here

- HumanReadablePlacement.HRP_NONE canvas.deviceText(...).

+2

, -? barText, , fontSize . -. temp, .

, ...

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;
    }
}

barcode4j-2.1.jar, -3.9.jar

, " ", fontSize

- . enter image description here

-, (, 90) enter image description here

SubOptimal - .

0

Source: https://habr.com/ru/post/1664437/


All Articles