0

I have installed code128 font in C:/windows/fonts from this website and I have used java methods to generate string of barcode but when I put the result string in MS word I have this

where is the problem knowing that I have installed the font and use the code from the same website

1 Answer 1

2

I have converted c# code of this page to java and it works

here is the java code :

    public class NewCoder128 {


    public NewCoder128(){}


     public String StringToBarcode(String value)
     {
         // Parameters : a string
         // Return     : a string which give the bar code when it is dispayed with CODE128.TTF font
         //              : an empty string if the supplied parameter is no good
         int charPos, minCharPos;
         int currentChar, checksum;
         Boolean isTableB = true, isValid = true;
         String returnValue = "";

         if (value.length() > 0)
         {
             // Check for valid characters
             for (int charCount = 0; charCount < value.length(); charCount++)
             {
                 //currentChar = char.GetNumericValue(value, charPos);
                 //            (int)char.Parse(value.Substring(charCount, 1));
                 System.out.println("count "+charCount);
                 currentChar = value.charAt(charCount);
                 if (!(currentChar >= 32 && currentChar <= 126))
                 {
                     isValid = false;
                     break;
                 }
             }

             // Barcode is full of ascii characters, we can now process it
             if (isValid)
             {
                 charPos = 0;
                 while (charPos < value.length())
                 {
                     if (isTableB)
                     {
                         // See if interesting to switch to table C
                         // yes for 4 digits at start or end, else if 6 digits
                         if (charPos == 0 || charPos + 4 == value.length())
                             minCharPos = 4;
                         else
                             minCharPos = 6;

                         BarcodeConverter128 brc128 = new BarcodeConverter128();

                         minCharPos = brc128.IsNumber(value, charPos, minCharPos);

                         if (minCharPos < 0)
                         {
                             // Choice table C
                             if (charPos == 0)
                             {
                                 // Starting with table C
                                 returnValue =   String.valueOf((char) 205); // char.ConvertFromUtf32(205);
                             }
                             else
                             {
                                 // Switch to table C
                                 returnValue = returnValue + String.valueOf((char) 199);
                             }
                             isTableB = false;
                         }
                         else
                         {
                             if (charPos == 0)
                             {
                                 // Starting with table B
                                 returnValue = String.valueOf((char) 204); // char.ConvertFromUtf32(204);
                             }

                         }
                     }

                     if (!isTableB)
                     {
                         // We are on table C, try to process 2 digits
                         minCharPos = 2;
                         BarcodeConverter128 brcd = new BarcodeConverter128();
                         minCharPos = brcd.IsNumber(value, charPos, minCharPos);
                         if (minCharPos < 0) // OK for 2 digits, process it
                         {

                             currentChar = Integer.parseInt(value.substring(charPos, charPos+2));
                             currentChar = currentChar < 95 ? currentChar + 32 : currentChar + 100;
                             returnValue = returnValue + String.valueOf((char) currentChar);
                             charPos += 2;
                         }
                         else
                         {
                             // We haven't 2 digits, switch to table B
                             returnValue = returnValue + String.valueOf((char) 200);
                             isTableB = true;
                         }
                     }
                     if (isTableB)
                     {
                         // Process 1 digit with table B
                         returnValue = returnValue + value.substring(charPos, charPos+1);
                         charPos++;
                     }
                 }

                 // Calculation of the checksum
                 checksum = 0;
                 for (int loop = 0; loop < returnValue.length(); loop++)
                 {
                     currentChar = returnValue.charAt(loop);
                     currentChar = currentChar < 127 ? currentChar - 32 : currentChar - 100;
                     if (loop == 0)
                         checksum = currentChar;
                     else
                         checksum = (checksum + (loop * currentChar)) % 103;
                 }

                 // Calculation of the checksum ASCII code
                 checksum = checksum < 95 ? checksum + 32 : checksum + 100;
                 // Add the checksum and the STOP
                 returnValue = returnValue +
                         String.valueOf((char) checksum) +
                         String.valueOf((char) 206);
             }
         }

         return returnValue;
     }



}


    class BarcodeConverter128
    {
        /// <summary>
        /// Converts an input string to the equivilant string, that need to be produced using the 'Code 128' font.
        /// </summary>
        /// <param name="value">String to be encoded</param>
        /// <returns>Encoded string start/stop and checksum characters included</returns>
       public BarcodeConverter128(){}

        public int IsNumber(String InputValue, int CharPos, int MinCharPos)
        {
            // if the MinCharPos characters from CharPos are numeric, then MinCharPos = -1
            MinCharPos--;
            if (CharPos + MinCharPos < InputValue.length())
            {
                while (MinCharPos >= 0)
                {
                    if ((int)(InputValue.charAt(CharPos + MinCharPos)) < 48
                        ||(int)(InputValue.charAt(CharPos + MinCharPos)) > 57)
                    {
                        break;
                    }
                    MinCharPos--;
                }
            }
            return MinCharPos;
        }


    }

with this class you only need to download code128 font, install it on your system paste in windows/fonts and call this code as below and the generated barcode will be readable

NewCoder128 newCoder128 = new NewCoder128();    
String encoded_string = newCoder128.StringToBarcode("123456789")

// encoded string is    "Í,BXnÈ9oÎ"
Sign up to request clarification or add additional context in comments.

1 Comment

copied your output Í,BXnÈ9oÎ and pasted in excel and changed it's for to code 128 then when i try to scan it, it just wont scan. Is it working for you ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.