# BarCoder

## Recommended Posts

Was messing with a barcode scanner app on my HTC Evo and decided to make this.

Yeah it's horribly sloppy, but it gets the job done.

Enjoy!

Screen Shot

Source code - Do what you wish with it.

```#include <GUIConstants.au3>
Local \$cur[2]

\$upc = InputBox('Barcode', 'Please enter an 11 or 12 digit UPC code.')
If check(\$upc) <> True Then die('Error processing bar code.')
If StringLen(\$upc) = 11 Then
\$upc2 = \$upc & checkDigit(\$upc)
Else
\$upc2 = \$upc
EndIf
\$c = array(\$upc2)
\$w = 1
\$h = 160

Global \$upc, \$upc2, \$c, \$cur[2], \$code, \$w, \$h, \$gui

\$code &= '101'
For \$i = 1 To 12
If between(\$i, 1, 6) Then
\$code &= c(\$c[\$i], 1)
EndIf
If \$i = 6 Then
\$code &= '01010'
EndIf
If between(\$i, 7, 12) Then
\$code &= c(\$c[\$i], 2)
EndIf
Next
\$code &= '101'

Global \$pix[StringLen(\$code)+1]

gui()

While GUIGetMsg(\$gui) <> \$GUI_Event_Close
Sleep(10)
WEnd

Func check(\$a)
If \$a <> 0 And between(StringLen(\$a), 11, 12) Then
Return True
Else
Return False
EndIf
EndFunc

Func checkDigit(\$a)
\$tog = -1
\$more = 0
For \$i = 1 To StringLen(\$a)
\$tog = \$tog * -1
\$num = StringLeft(\$a, \$i)
\$num = StringRight(\$num, 1)
If \$tog = 1 Then
\$cur[1] = \$cur[1] + \$num
Else
\$cur[0] = \$cur[0] + \$num
EndIf
Next

\$tot = (\$cur[1]*3)+\$cur[0]

For \$j = 0 To 9
If StringRight(\$tot, 1) = 0 Then
ExitLoop
Else
\$more = \$more + 1
\$tot = \$tot + 1
EndIf
Next

Return \$more
EndFunc

Func c(\$a, \$B)
If \$b = 1 Then
If \$a = 0 Then
\$xx = s(3) & b(2) & s(1) & b(1)
ElseIf \$a = 1 Then
\$xx = s(2) & b(2) & s(2) & b(1)
ElseIf \$a = 2 Then
\$xx = s(2) & b(1) & s(2) & b(2)
ElseIf \$a = 3 Then
\$xx = s(1) & b(4) & s(1) & b(1)
ElseIf \$a = 4 Then
\$xx = s(1) & b(1) & s(3) & b(2)
ElseIf \$a = 5 Then
\$xx = s(1) & b(2) & s(3) & b(1)
ElseIf \$a = 6 Then
\$xx = s(1) & b(1) & s(1) & b(4)
ElseIf \$a = 7 Then
\$xx = s(1) & b(3) & s(1) & b(2)
ElseIf \$a = 8 Then
\$xx = s(1) & b(2) & s(1) & b(3)
ElseIf \$a = 9 Then
\$xx = s(3) & b(1) & s(1) & b(2)
EndIf
ElseIf \$b = 2 Then
If \$a = 0 Then
\$xx = b(3) & s(2) & b(1) & s(1)
ElseIf \$a = 1 Then
\$xx = b(2) & s(2) & b(2) & s(1)
ElseIf \$a = 2 Then
\$xx = b(2) & s(1) & b(2) & s(2)
ElseIf \$a = 3 Then
\$xx = b(1) & s(4) & b(1) & s(1)
ElseIf \$a = 4 Then
\$xx = b(1) & s(1) & b(3) & s(2)
ElseIf \$a = 5 Then
\$xx = b(1) & s(2) & b(3) & s(1)
ElseIf \$a = 6 Then
\$xx = b(1) & s(1) & b(1) & s(4)
ElseIf \$a = 7 Then
\$xx = b(1) & s(3) & b(1) & s(2)
ElseIf \$a = 8 Then
\$xx = b(1) & s(2) & b(1) & s(3)
ElseIf \$a = 9 Then
\$xx = b(3) & s(1) & b(1) & s(2)
EndIf
EndIf
Return \$xx
EndFunc

Func s(\$a)
\$temp = ''
For \$i = 1 To \$a
\$temp = \$temp & '0'
Next
Return \$temp
EndFunc

Func b(\$a)
\$temp = ''
For \$i = 1 To \$a
\$temp = \$temp & '1'
Next
Return \$temp
EndFunc

Func array(\$a)
Local \$d[StringLen(\$a)+1]
\$e = ''
For \$i = 1 To StringLen(\$a)
\$d[\$i] = StringRight(StringLeft(\$a, \$i), 1)
Next
Return \$d
EndFunc

Func gui()
\$t = 'BarCoder'
Global \$gui = GUICreate(\$t, 140, 100, -1, -1, -1, 128)
GUISetBkColor(0xffffff)
GUISetFont(9)
GUICtrlCreateGroup('     ' & \$upc2, 5, 0, 130, 90)
For \$i = 1 To StringLen(\$code)
\$fn = StringRight(StringLeft(\$code, \$i), 1)
\$pix[\$i] = GUICtrlCreatePic(\$fn & '.jpg', 20+(\$i), 20, 1, 60)
Next
\$left = GUICtrlCreateLabel(\$c[1], 10, 73, 8, 15)
GUICtrlSetFont(-1, 8)
\$midL = GUICtrlCreateLabel(\$c[2] & \$c[3] & \$c[4] & \$c[5] & \$c[6], 32, 70, 34, 15)
\$midR = GUICtrlCreateLabel(\$c[7] & \$c[8] & \$c[9] & \$c[10] & \$c[11], 70, 70, 36, 15)
\$righ = GUICtrlCreateLabel(\$c[12], 122, 73, 8, 15)
GUICtrlSetFont(-1, 8)
GUISetState()
WinSetOnTop(\$t, '', 1)
EndFunc

Func die(\$x, \$y = '', \$z = -1)
MsgBox(262144, \$y, \$x, \$z)
Exit
EndFunc

Func between(\$num, \$min, \$max)
If \$num >= \$min And \$num <= \$max Then
Return 1
Else
Return 0
EndIf
EndFunc```

Images - You need these to display the bars. Put them in the same folder as the program.

0.jpg

1.jpg

Edit(s) - Fix-ups!

Edit2: Fixed the extra numeral being added to the topmost label.

Edit3: Added the images to the post.

Edited by cembry90

AutoIt Stuff:

UDFs: {Grow}

##### Share on other sites

Doesn't work when the UPC begins with zero. According to Wikipedia, zero is a common number for most products.

Nvm, I'm a dork.

Edited by jaberwocky6669

##### Share on other sites

Doesn't work when the UPC begins with zero. According to Wikipedia, zero is a common number for most products.

Nvm, I'm a dork.

Idk what issue you were referring to, but there is only one true problem afaik (and it would be user side at that). If you enter a UPC where the check digit (12th number) is incorrect, some scanners will reject that and not read it.

Example: the check digit is 2 for the UPC 123456789012. If you enter 12345679011, the barcode reader I use will not scan it. It will just keep trying to scan it, and nothing happening. (I'm using an app called Barcode Scanner.)

AutoIt Stuff:

UDFs: {Grow}

##### Share on other sites

Error check is calculated for 12-th position number (last number X), basicly to generate UPC when you enter 11 digits and the last digit is calculated from 11, so that you can generate working UPC with error check number. You buy new UPC code so they already have generated last number on UPC list when they send it to you. Generating your imaginary UPC is not something that you need to do becose you dont know if that UPC is already taken or not.

Ofc you can do if for fun if you wona test it.

Edit:

My way if i did it correctly from wiki page

```\$Upc_A = "036000291453"
Select
Case StringLen(\$Upc_A) = 11;generate 12-th number
\$s = StringSplit(\$Upc_A,"")
\$c1 = StringSplit(((((\$s[1] +\$s[3] +\$s[5] +\$s[7] +\$s[9] +\$s[11])*3)+(\$s[2]+\$s[4]+\$s[6]+\$s[8]+\$s[10]))/10),".")
\$c2 = 10- \$c1[2]
MsgBox(0,"Error number is",\$c2)
Case StringLen(\$Upc_A) = 12;is 12-th number valid?
\$s = StringSplit(StringLeft(\$Upc_A,11),"")
\$c1 = StringSplit(((((\$s[1] +\$s[3] +\$s[5] +\$s[7] +\$s[9] +\$s[11])*3)+(\$s[2]+\$s[4]+\$s[6]+\$s[8]+\$s[10]))/10),".")
\$c2 = 10- \$c1[2]
If \$c2 = StringRight(\$Upc_A,1) Then
MsgBox(0,"Last number is valid",StringRight(\$Upc_A,1))
Else
MsgBox(0,"not valid","Shud b '"&\$c2&"' instead '"&StringRight(\$Upc_A,1)&"'")
EndIf
EndSelect```
Edited by bogQ

TCP server and client - Learning about TCP servers and clients connection
Au3 oIrrlicht - Irrlicht project
Au3impact - Another 3D DLL game engine for autoit. (3impact 3Drad related)

There are those that believe that the perfect heist lies in the preparation.
Some say that it’s all in the timing, seizing the right opportunity. Others even say it’s the ability to leave no trace behind, be a ghost.

##### Share on other sites

Here's my brute, naive color-by-numbers implementation:

```Func checkDigit(Const \$a)
Local \$UPCArray = StringSplit(\$a, "", 2)
Local \$sum = 0
Local \$sum2 = 0
Local \$result = 0

For \$i = 0 To 10 Step 2
\$sum += \$UPCArray[\$i]
Next

For \$i = 1 To 9 Step 2
\$sum2 += \$UPCArray[\$i]
Next

\$result = Mod(((\$sum * 3) + \$sum2), 10)

If \$result <> 0 Then
Return 10 - \$result
Else
Return \$result
EndIf
EndFunc ;==>checkDigit```

## Create an account

Register a new account

×

• Wiki

• Back

• Git