Announcement

Collapse

Forum Guidelines

This forum is for finished source code that is working properly. If you have questions about this or any other source code, please post it in one of the Discussion Forums, not here.
See more
See less

EAN-8/13 Barcode example

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

    EAN-8/13 Barcode example

    A Barcode example. I don't have a reader here at the moment to test it, so if somebody has one, feel free to test it....
    Code:
    '==================================================================================================
    ' Graphical and printing demo of a Barcode in EAN8/13 form
    ' April 5, 2005 - Corrected bug in bar drawing
    '                 Changed:   For dwCnt = (Len(strEANCode) -1) \ 2 + [b]1[/b] To Len(strEANCode) into [b]2[/b]
    '--------------------------------------------------------------------------------------------------
    #Compile Exe
    #Compiler PBWin 8
    #Dim All
    #Include "win32api.inc
     
    %IDC_GRAPH = 1001 : %IDC_PRINT = 1002 : %IDC_TEXTBOX = 1003 : %IDC_UPDATE = 1004
    %BARHEIGHT = 40 : %X_OFFSET = 150 : %Y_OFFSET = 10 : %BARSCALE = 4
     
    Global hDlg As Dword, BC As String, X As Dword, Y As Dword, EANBar As String
     
    '==================================================================================================
    ' EAN2Bin.      (strEANCode - holds the numerical barcode)  Characters in return string:
    '               "B" = Black bar, "W" = White bar, "b" = Black guardian, "w" = White guardian.
    '==================================================================================================
    Function EAN2Bin(strEANCode As String) As String
      Dim dwCnt As Dword, strAux As String, strExit As String, strCode As String, Choice As Dword
      strEANCode = Trim$(strEANCode)
      If (Len(strEANCode) = 13) Then
        strCode = Choose$(Val(Left$(strEANCode, 1)) +1, "WWWWWW","WWBWBB","WWBBWB", _
        "WWBBBW","WBWWBB","WBBWWB","WBBBWW","WBWBWB","WBWBBW","WBBWBW")
      Else
        strCode = "BBBB"
      End If
      strExit = "wwwbwb"                    'Start "guardian" separator
     
      For dwCnt = 2 To (Len(strEANCode) -1) \ 2 + 1
        If Mid$(strCode, dwCnt -1, 1) = "W" Then
          StrExit = strExit & Choose$(Val(Mid$(strEANCode,dwCnt,1)) +1,"WWWBBWB","WWBBWWB", _
          "WWBWWBB","WBBBBWB","WBWWWBB","WBBWWWB","WBWBBBB","WBBBWBB","WBBWBBB","WWWBWBB")
        Else
          strExit = strExit & Choose$(Val(Mid$(strEANCode,dwCnt,1)) +1,"WBWWBBB","WBBWWBB", _
          "WWBBWBB","WBWWWWB","WWBBBWB","WBBBWWB","WWWWBWB","WWBWWWB","WWWBWWB","WWBWBBB")
        End If
      Next
     
      strExit = strExit & "wbwbw"           'Middle "guardian" separator
     
      For dwCnt = (Len(strEANCode) -1) \ 2 + 2 To Len(strEANCode)
        strExit = strExit & Choose$(Val(Mid$(strEANCode,dwCnt,1)) +1,"BBBWWBW","BBWWBBW", _
        "BBWBBWW","BWWWWBW","BWBBBWW","BWWBBBW","BWBWWWW","BWWWBWW","BWWBWWW","BBBWBWW")
      Next
     
      strExit = strExit & "bwbwww"          'End "guardian" separator
      Function = strExit
    End Function
     
    '==================================================================================================
    ' DrawEANCode.  Draws the bars and the numerical string into Graphic control.
    '               Digits:     A string containing the digits
    '==================================================================================================
    Function DrawEANCode(Digits As String) As Dword
      Local RowCnt As Dword, Result As Dword, ColCnt As Dword, Digit As Dword, MidSep As Dword
      Local DigCnt As Dword, Row As Dword, PosCnt As Dword, Col As Dword, RgbVal As Dword
      BC = EAN2Bin(Digits)
     
      Graphic Attach hDlg, %IDC_GRAPH, ReDraw
      Graphic Clear %White
      Graphic Get Client To x, y
      For PosCnt = 1 To Len(BC)
        Select Case Mid$(BC,PosCnt,1)
          Case "B" : Col = %BARHEIGHT : RgbVal = RGB(0,0,0)
          Case "b" : Col = %BARHEIGHT + 5 : RgbVal = RGB(0,0,0)
          Case "W" : Col = %BARHEIGHT : RgbVal = RGB(255,255,255)
          Case "w" : Col = %BARHEIGHT + 5 : RgbVal = RGB(255,255,255)
        End Select
        Graphic Line (PosCnt + 6, 2) - (PosCnt + 6, Col), RgbVal
      Next
     
      MidSep = InStr(BC,"wbwbw")
      Col = %BARHEIGHT +2
      For DigCnt = 1 To Len(Digits)
        Digit = Val(Mid$(Digits,DigCnt,1))
        Row = (DigCnt-1) * 6 + 3
        If DigCnt > 1 Then Row = Row + 8
        If (Row > MidSep) Then Row = Row + 12
        For RowCnt = 1 To 5
          Result = Val(Read$(Digit*5 + RowCnt))
          For ColCnt = 0 To 6
            If Bit(Result, ColCnt) Then Graphic Set Pixel (Row + RowCnt -1, Col + ColCnt), RGB(0,0,0)
          Next
        Next
      Next
      Graphic ReDraw
       
      '------------------------------------------------------------------------------------------------
      ' Data for digits 0...9 as dotmatrix character
      '------------------------------------------------------------------------------------------------
      Data &b00111110, &b01010001, &b01001001, &b01000101, &b00111110         'Data for 0
      Data &b00000000, &b01000010, &b01111111, &b01000000, &b00000000         'Data for 1
      Data &b01100010, &b01010001, &b01001001, &b01001001, &b01000110         'Data for 2
      Data &b00100010, &b01000001, &b01001001, &b01001001, &b00110110         'Data for 3
      Data &b00011000, &b00010100, &b00010010, &b01111111, &b00010000         'Data for 4
      Data &b00100111, &b01000101, &b01000101, &b01000101, &b00111001         'Data for 5
      Data &b00111100, &b01001010, &b01001001, &b01001000, &b00110000         'Data for 6
      Data &b00000001, &b00000001, &b01110001, &b00001001, &b00000111         'Data for 7
      Data &b00110110, &b01001001, &b01001001, &b01001001, &b00110110         'Data for 8
      Data &b00000110, &b00001001, &b01001001, &b00101001, &b00011110         'Data for 9
    End Function
     
    '==================================================================================================
    ' PrintEAN.     Prints the barcode to the printer.
    '==================================================================================================
    Function PrintEAN As Long
      XPrint Attach Choose
      XPrint Stretch hDlg, %IDC_GRAPH, (0,0) - (x-1,y-1) To (%X_OFFSET,%Y_OFFSET) - _
                           (%X_OFFSET + x * %BARSCALE, %Y_OFFSET + y * %BARSCALE)
      XPrint Close
    End Function
     
    '==================================================================================================
    CallBack Function CbMain () As Long
      Select Case CbMsg
        Case %WM_INITDIALOG
          DrawEANCode EANBar
        Case %WM_COMMAND
          Select Case CbCtl
            Case %IDC_PRINT : PrintEAN
            Case %IDC_UPDATE
              Control Get Text hDlg, %IDC_TEXTBOX To EANBar
              Control Set Focus hDlg, %IDC_TEXTBOX
              Control Send hDlg, %IDC_TEXTBOX, %EM_SETSEL, 0, -1
              DrawEANCode EANBar
          End Select
      End Select
    End Function
     
    Function PBMain () As Long
      Dialog New Pixels, 0, "Barcode example " ,,,160,180, %WS_SYSMENU Or %WS_VISIBLE To hDlg
      EANBar = "0000000000000"
      BC = EAN2Bin(EANBar)
      Control Add Graphic, hDlg, %IDC_GRAPH,"", 20, 15, Len(BC) + 15, %BARHEIGHT + 15 ,, %WS_EX_CLIENTEDGE
      Control Add Label  , hDlg, -1, "Digits:", 20, 102, 30, 20
      Control Add TextBox, hDlg, %IDC_TEXTBOX, "0000000000000", 55, 100, 85, 20, %ES_NUMBER Or %WS_TABSTOP, %WS_EX_CLIENTEDGE
      Control Send hDlg, %IDC_TEXTBOX, %EM_SETLIMITTEXT, 13, 0
      Control Add Button , hDlg, %IDC_PRINT, "Print"  , 20, 140, 50, 25
      Control Add Button , hDlg, %IDC_UPDATE, "Update", 90, 140, 50, 25
      Dialog Show Modal hDlg Call CbMain
    End Function
     
    '--------------------------------------------------------------------------------------------------
    ------------------
    Regards,
    Peter



    [This message has been edited by Peter Lameijn (edited April 05, 2005).]
    Regards,
    Peter

    "Simplicity is a prerequisite for reliability"

    #2
    I have tested the print out and it scans well

    I am trying to print barcodes onto a continuous roll of labels.
    I can do this from M/S Word by changing the page size.
    Do you know how to do this in PowerBASIC?

    Also the barcodes print out on my Epson EPL-5900L
    but not on a HP2300PCL6.
    Does anyone know why?

    Thanks
    Ian

    ------------------
    Ian Docksey, Trinitarian Bible Society
    Ian Docksey, Trinitarian Bible Society

    Comment


      #3
      Originally posted by Borje Hagsten:
      The easy-to-use PB commands have been tested to work with a zillion
      printers, give or take a few..
      Seeems that your HP2300PCL6 printer is one of the few...


      ------------------
      Regards,
      Peter
      Regards,
      Peter

      "Simplicity is a prerequisite for reliability"

      Comment

      Working...
      X
      😀
      🥰
      🤢
      😎
      😡
      👍
      👎