1

I have a string like (01)8638634367382(15)230316(3103)000998(10)45456465604 that I want to do it as barcode png using barcode4j lib in java. I use this code

  // Create the barcode bean
        Code128Bean barcode = new Code128Bean();

        // Configure the barcode generator
        final int dpi = 400;
        barcode.setModuleWidth(0.2);
        barcode.doQuietZone(false);

        int codeset = Code128Constants.CODESET_C;
        for (int i = 0; i < input.length(); i++) {
            char c = input.charAt(i);
            if (!Character.isDigit(c)) {
                codeset = Code128Constants.CODESET_B;
                break;
            }
        }
        barcode.setCodeset(codeset);
        // Generate the barcode bitmap
        BitmapCanvasProvider canvas = new BitmapCanvasProvider(dpi, BufferedImage.TYPE_BYTE_BINARY, false, 0);
        barcode.generateBarcode(canvas, input);
        try {
            canvas.finish();
        } catch (IOException e) {
            throw new RuntimeException("Error generating barcode", e);
        }

        // Encode the bitmap as a base64 string
        ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(canvas.getBufferedImage(), "png", outputStream);
        } catch (IOException e) {
            throw new RuntimeException("Error encoding barcode as PNG", e);
        }
        byte[] barcodeBytes = outputStream.toByteArray();
        String base64Barcode = Base64.getEncoder().encodeToString(barcodeBytes);
        
        return base64Barcode;

but the generated barcode isn't recognisable by any barcode scanner software. Also I encode the image to base64 string and when I want to represent in any part of my program I decode it and show the image. Any idea what's wrong with this?

I expect to produce a readable barcode in this format (01)8638634367382(15)230316(3103)000998(10)45456465604 and of course it must be scannable by any software.

Terry Burton
  • 2,801
  • 1
  • 29
  • 41
unknownDev
  • 11
  • 4

2 Answers2

1

The example that you have supplied is a GS1 Application Identifier element string in bracketed representation.

Unless the library does it for you, you will need to convert this to unbracketed representation with FNC1 in first position, suitable for encoding directly into a Code 128. (It is this process that differentiates GS1-128 from plain Code 128.)

The bracketed AI element string that you provided —(01)8638634367382(15)230316(3103)000998(10)45456465604 — does not decode correctly:

GTIN                   (01) 8638634367382    <-- Invalid. Too short.
BEST BEFORE or BEST BY (15) 230316
NET WEIGHT (kg)        (3103) 000998
BATCH/LOT              (10) 45456465604

Replacing the GTIN with one that is valid — (01)0101234567890128 — would allow you to represent the data in FNC1 in first syntax as "{FNC1}01012345678901281523031631030009981045456465604" which you will need to pass to the library as input in its expected format.

According to the barcode4j documentation the escape sequence particular to their software for encoding an FNC1 non-data character is a literal 0xF1 ASCII value:

Functions 1 to 4 may be used. They are encoded as ASCII characters 0xF1 (241), 0xF2 (242), 0xF3 (243) and 0xF4 (244) in the message.

More detail concerning the various representations of GS1 data is provided in this article.

GS1 provide the Barcode Syntax Resource which is a native library, with bindings for Java, that can process GS1 Application Identifier syntax data.

Terry Burton
  • 2,801
  • 1
  • 29
  • 41
  • I read many of documentations about the correct message need to send to make the barcode correctly but nothing work. There is anything else to do or any pdf indicates me of how to to find the correct message before send it to library ? – unknownDev Mar 21 '23 at 09:41
  • "I tried stuff. It didn't work." is not a useful way of asking for help. You've been given links to various accurate resources that explain the overall approach and implement what is required. What exactly have you tried? What results did you expect (based on your understanding of what information)? What did you actually get? Essentially you are asking folk to do you homework for you, but instead you will be much more successful if you explain in detail what specific issues you are encountering, or what information you find difficult to understand. – Terry Burton Mar 21 '23 at 09:46
  • Folk are more prepared to provide explanatory content and solutions to specific issues for many future users to encounter, than to provide overall solutions to an end to end problem. So I advise to break the problem down, e.g. by splitting off the base64 image encoding part as it's unrelated to producing the initial image and seems to distract from the main issue of deciding what to put in the barcode in the first place. – Terry Burton Mar 21 '23 at 09:55
  • I try to encode this barcode data (01)8638634367382(15)230316(3103)000998(10)45456465604 and send it to library barcode4j to make it barcode GS1-128. I send this code in library but the generated barcode isn't scannable or recognized as Code128 instead of GS1-128. This is the problem. Also I try to insert special characters like FNC1 but nothing change. – unknownDev Mar 21 '23 at 10:15
  • What EXACTLY did you try? What EXACTLY was the result. For example, what is the value of the `input` variable (with FNC1 characters) in the code that you provided and what was the resulting image. If it takes you less than a minute to write your response then you are not trying to help yourself. – Terry Burton Mar 21 '23 at 15:36
  • Mr Burton thank you for your patient! I want to generate this string ((01)8638634367382(15)230316(3103)000998(10)45456465604) into a scannable barcode using Barcode4j. I want to find the correct way, library wants to send the barcode data. I tried many variations like {FNC1}XXXXXXXXXXX where XXXXXXXXXX is the actual barcode data like before. Also I tried with special character a strange one but nothing. So the real question is what is the correct form where the library needs to make my barcode scannable. I understand that my question is very general but I don't know the correct way to do it. – unknownDev Mar 21 '23 at 19:15
  • @unknownDev As requested, please amend the code in your question to show how you are setting the `input` variable, including FNC1, and what outputted image you are getting. Does the "special character" that you mention refer to the 0xF1 byte in my reply, or something else? If you are exact, by providing a minimal reproducible example, then your work can be replicated and the exact problem identified and fixed. Right now you are essentially asking folk to write something that works from scratch by guessing at what you did, and what you did wrong, because you have not given inputs and outputs. – Terry Burton Mar 21 '23 at 21:52
  • Mr Burton I try inputs like (01)8638634367382(15)230316(3103)000998(10)45456465604, 0186386343673821523031631030009981045456465604, {FNC1}0186386343673821523031631030009981045456465604 but the barcode image isn't scannable. Can you tell me what is the correct message format that library needs to have to produce the barcode? – unknownDev Mar 22 '23 at 08:55
  • Without greater specificity I will not engage further. Your are actively preventing anybody from helping you by not following instructions (troubleshooting steps or documentation snippets) and simply repeating yourself without increased detail. So all that I can do is to repeat myself: As requested, provide the image output. As requested, modify the code in the question to show the inputs that you provided. Provide minimal working code that others can run. As requested, read the answer that has already been given: Have you tried inserting a 0xF1 character for FNC1 as the documentation states? – Terry Burton Mar 22 '23 at 15:53
0

I had the same issue lastly and it seems to be not very clear in some point...what i have found after a long analysis is besides all you said previously you have to add some control characters if you want a valid gs1 code 128. For Example at the end of the string data you have to add a control character and eventually a stop character. All of them are printed by a char ascii number. By the way I didnt find a rutine or function in Java that passing a string (with the ais and the data) returned a string already parsed so i had to do one by my own (I "copied" one coded in vb so it was easy).