drawing a Barcode without a Barcode font

Mark Wieder ahsoftware at sonic.net
Fri Jul 20 17:08:14 EDT 2018


On 07/20/2018 04:31 AM, Matthias Rebbe via use-livecode wrote:
> We tried here with iPhone and also with an USB scanner. Both devices scanned the code successfully.
> 
> I have just finished a program for shipment. It fetches customer and invoice data from the accounting software and creates then the shipment labels including a Code25i barcode and a QR code. The carrier is Trans-o-Flex. I need to send some sample labels to their IT department. They will check if the labels are readable by their scanners. But i am now confident, that this will work on their side also.

Cool. I played around with Mattias' version of Mike's excellent code a 
bit. Here's my take. Note that you really only need one graphic image to 
work with - here's how to do it with a single invisible filled rectangle 
graphic (I kept the name "narrowB"). This takes a string of numbers and 
generates the intermediate string and bar code.

-- 2of5 barcode

constant kBarCodeHeight = 69
constant kBarCodeWidth = 360
constant kInitialTopLeft = "60,286" -- defines where the first bar is placed
constant kGapBetweenBars = 0
constant kBarCodeGroupName = "barcode2of5"

local sLast
local sWideWidth

on mouseUp
    local tNumberString
    local tConvertedNumberString
    local tBarWidth

    set the height of graphic "narrowB" to kBarCodeHeight
    put field "barcodeNumber" into tNumberString

    -- need a checksum as the last digit?
    -- put checksumFrom (field "barcodeNumber") after tNumberString

    -- calculate the expected bar widths
    put convertNumberString(tNumberString) into tConvertedNumberString
    put barWidthFrom(tConvertedNumberString) into tBarWidth
    set the width of graphic "narrowB" to tBarWidth
    put tBarWidth * 2 into sWideWidth
    makeBarCodeFrom tConvertedNumberString
    -- add the number string to the barcode if it's not already on the form
    displayBarCodeNumber
end mouseUp

function barWidthFrom pNumberString
    local tHowManyChars
    local tTestString

    put pNumberString into tTestString
    replace "n" with empty in tTestString
    put length (tTestString) + length (pNumberString) into tHowManyChars
    return kBarCodeWidth / tHowManyChars
end barWidthFrom

on makeBarCodeFrom pChars
    local tIsFilled

    lock screen
    if there is a group kBarCodeGroupName then
       delete group kBarCodeGroupName
    end if
    put empty into sLast

    create group kBarCodeGroupName
    set the height of group kBarCodeGroupName to kBarCodeHeight
    set the width of group kBarCodeGroupName to kBarCodeWidth
    put true into tIsFilled
    repeat for each char tChar in pChars
       copy graphic "narrowB" to group kBarCodeGroupName
       switch tChar
          case "w"
             set the width of the last graphic to sWideWidth
             break
       end switch
       if tIsFilled then
          set the visible of the last graphic to true
       end if
       put not tIsFilled into tIsFilled
       if sLast is empty then
          set the topleft of the last graphic to kInitialTopLeft
       else
          set the topleft of the last graphic to horAdjust(kGapBetweenBars)
       end if
       put the short id of the last graphic into sLast -- the id of the 
most recently placed bar
    end repeat

    unlock screen
end makeBarCodeFrom

command displayBarCodeNumber
    copy field "barCodeNumber" to group kBarCodeGroupName
    set the opaque of the last field to false
    set the showborder of the last field to false
    set the textalign of the last field to "center"
    set the top of the last field to the bottom of group kBarCodeGroupName
    set the width of the last field to the width of group kBarCodeGroupName
    set the left of the last field to item 1 of kInitialTopLeft
end displayBarCodeNumber

function horAdjust pGap
    local tGap

    put the topright of graphic id sLast into tGap
    set the itemdelimiter to ","
    add pGap to item 1 of tGap
    return tGap
end horAdjust

local sConversionArray

command initializeConversionArray
    put "nnWWn" into sConversionArray["0"]
    put "WnnnW" into sConversionArray["1"]
    put "nWnnW" into sConversionArray["2"]
    put "WWnnn" into sConversionArray["3"]
    put "nnWnW" into sConversionArray["4"]
    put "WnWnn" into sConversionArray["5"]
    put "nWWnn" into sConversionArray["6"]
    put "nnnWW" into sConversionArray["7"]
    put "WnnWn" into sConversionArray["8"]
    put "nWnWn" into sConversionArray["9"]
end initializeConversionArray

function convertNumberString pNumberString
    local tWidthString
    local tString1, tString2
    local x, y

    initializeConversionArray
    put "nnnn" into tWidthString -- four-bar start code
    if the number of chars in pNumberString mod 2 is 1 then
       put "0" before pNumberString
    end if
    -- need to interleave two chars at a time
    repeat with x=1 to length (pNumberString) step 2
       put sConversionArray[char x of pNumberString] into tString1
       put sConversionArray[char x+1 of pNumberString] into tString2
       repeat with y=1 to 5
          put char y of tString1 & char y of tString2 after tWidthString
       end repeat
    end repeat
    put "wnn" after tWidthString -- three-bar stop code
    return tWidthString
end convertNumberString

function checksumFrom pNumberString
    local tEvenNumbers, tOddNumbers
    local tChecksum
    local tResult

    repeat with x=1 to length (pNumberString) step 2
       add char x of pNumberString to tOddNumbers
       add char x+1 of pNumberString to tEvenNumbers
    end repeat
    put tOddNumbers * 3 + tEvenNumbers into tChecksum
    switch tChecksum mod 10
       case 10
          put 0 into tResult
          break
       default
          put 10 - (tChecksum mod 10) into tResult
    end switch
    return tResult
end checksumFrom

-- 
  Mark Wieder
  ahsoftware at gmail.com




More information about the use-livecode mailing list