Menu

PL/SQL (oracle) C128 encoder

2010-03-14
2015-03-18
  • David Pyke Le Brun

    here is a PL/SQL function (tested in Oracle 10g) for the Code128 encoder.

    it is based on the version in the excel macro.

    feel free to add it to your collection.

    CREATE OR REPLACE FUNCTION Code128(P_INPUT varchar2) return varchar2 
    DETERMINISTIC 
    IS
    /*
     *  This function is governed by the GNU Lesser General Public License (GNU LGPL)
     *  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
     *
     * the CODE128.TTF font can be source from http://sourceforge.net/projects/openbarcodes
     *
     * written by David Pyke (eselle@sourceforge.net)
     * based on code found at http://grandzebu.net/informatique/codbar-en/codbar.htm
     *                        http://sourceforge.net/projects/openbarcodes
     *
    */
        i   NUMBER;
        chksum  NUMBER;
        mini    NUMBER;
        dummy   NUMBER;
        tableB  BOOLEAN;
        c_pinput_length CONSTANT NUMBER := LENGTH(p_input);
        v_retval VARCHAR2(100);
        FUNCTION usetableC(p_start NUMBER, p_cnt NUMBER)
        RETURN BOOLEAN
        IS
            -- Determine if the p_cnt characters from p_start are numeric
        BEGIN
            FOR x in p_start .. p_start+p_cnt LOOP
    
                IF(x > c_pinput_length ) THEN
                    RETURN FALSE;
                END IF;
                IF ASCII(SUBSTR(p_input,x,1)) <48 or ASCII(SUBSTR(p_input,x,1)) >57 THEN
                    RETURN FALSE;
                END IF;
            END LOOP;
    
            RETURN TRUE;
        END usetableC;
    BEGIN
        IF c_pinput_length = 0 THEN
            RETURN NULL;
        END IF;
        --Check for valid characters
        --for c in 1..length(p_input) loop
            -- if not 21 -> 126 or 203.. return null
        --END LOOP;\
        tableB := true;
        i := 1;
        FOR c in 1..c_pinput_length LOOP
          EXIT when I >c_pinput_length;
            IF tableB THEN
                IF i =1 or (i+3) =length(p_input) THEN
                    mini := 4;
                ELSE
                    mini := 6;
                END IF;
                IF usetableC(i,mini) THEN
                    IF i=1 THEN
                        v_retval := CHR(210); --start with tableC
                    ELSE
                        v_retval := v_retval||CHR(204); --switch to tabelC
                    END IF;
                    tableB := FALSE;
                ELSE
                     IF i = 1 THEN 
                        v_retval := CHR(209); 
                     END IF; --Starting with table B
                END IF;
            END IF;
            IF NOT tableB THEN
                --We are on table C, try to process 2 digits
                mini := 2;
                if usetableC(I,MINI) THEN
                    dummy := TO_NUMBER(SUBSTR(p_input, i, 2));
                    IF(dummy < 95) THEN
                        dummy := dummy +32;
                    ELSE
                        dummy := dummy +105;
                    END IF;
                    v_retval := v_retval || CHR(dummy);
                     i := i + 2;
                ELSE
                    v_retval := v_retval || CHR(205);
                    tableB := TRUE; 
                END IF;
            END IF;
            IF tableB THEN
                 --Process 1 digit with table B
                v_retval := v_retval || SUBSTR(p_input, i, 1);
                i := i +1;
            END IF;
        END LOOP;
        --Calculation of the checksum
        FOR i IN 1 .. LENGTH(v_retval) LOOP
            dummy := ASCII(SUBSTR(v_retval,i,1));
            IF(dummy < 127) THEN
                dummy := dummy -32;
            ELSE
                dummy := dummy -105;
            END IF;
            IF i =1 then chksum := dummy; END IF;
    
            chksum := MOD((chksum + (i-1)*dummy),103);
        END LOOP;
    
        -- Calculation of the checksum ASCII code
        IF chksum <95 THEN
            chksum := chksum +32;
        ELSE
            chksum := chksum +105;
        END IF;
        --Add the checksum and the STOP codes
        v_retval := v_retval || CHR(chksum) || CHR(211);
        return v_retval;
    END Code128;
    /
    
     
    • makaveiljojo

      makaveiljojo - 2013-12-02

      I'm using a web-based free online code 128 barcode generator, designed for users to easily generate Code 128 (Code 128A, Code 128B, Code 128C) linear barcodes in Jpeg, Gif and Png image formats. Various parameter settings are available such as barcode size, rotation, resolution, image formats, text font etc.

       
  • Zac

    Zac - 2012-01-29

    A nice Code128 creator encodes code128, code128a, code128b, code128c

     
    • jack

      jack - 2013-07-04

      Code 128 creator library is not difficult to find. I used this one. It's also very good and powerful.

       
  • sss

    sss - 2012-09-03

    MacroBarcode Code 128 SQL Server Reporting Services Generator  can print bar code 128, GS1-128 (EAN 128) in Reporting Services. The generator control integrates with Reporting Services 2005 and Reporting Services 2008, and could be customized with C# & VB. No font or font tools is needed.

     
  • ursusmaj

    ursusmaj - 2013-02-12

    I believe that the loop in Function usetableC should have an upper bound of p_start+p_cnt-1 e.g. checking position 3 for a length of 6 should check positions 3 to 8 not 3 to 9. A case in point is when a string ends in 6 digits e.g. LI002058

     
  • Anonymous

    Anonymous - 2013-05-02

    PL/SQL function for various barcode font encoders for Oracle reports.

     
  • recthor

    recthor - 2013-06-21

    for solving your question, i think you can try this code 128 imaging sdk which can print bar code 128, GS1-128 (EAN 128) in Reporting Services. i have used it for several times , if you don´t have a better choice, you can have a try.

     

    Last edit: recthor 2013-07-29
  • makaveiljojo

    makaveiljojo - 2013-12-02

    I'm using a web-based online free code128 barcode generator. It is designed for users to easily generate Code 128 (Code 128A, Code 128B, Code 128C) linear barcodes in Jpeg, Gif and Png image formats. Various parameter settings are available such as barcode size, rotation, resolution, image formats, text font etc.

     
  • Zahoor ul Islam

    Zahoor ul Islam - 2015-03-18

    I must thanks David for fixing the code for unicode. Thanks, it been wonderful to have your help!. Great.

     

Log in to post a comment.