===================
This is a project I've just started to try to get my webcam to recognise barcodes.
Eventually I hope to be able to get the script to automatically detect barcodes of
any type, and quickly return the encoded text.
Ok, first some disclaimers:
1) This only works with simple CODE128 barcodes in code C at the moment.
2) It is not 100% effective, I get 1 in 5 reads as successful.
3) Upon exit, sometimes the script hangs (anyone know what I'm doing wrong???)
Consider this very much a work-in-progress.
Example useage:
This is a barcode I created using my Barcode Generator (see sig) and then printed out :
Note: the text in the barcode is "123456"
This is how it looks in the GUI having successfully 'read' the barcode:
#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <StaticConstants.au3> #include <Array.au3> #Include <Color.au3> Opt("GUIOnEventMode",1) Opt("PixelCoordMode",2) HotKeySet("{F1}","_Cap") ;************************************************ ; Variables ;************************************************ ;----- Open DLLs ----- $dAviCap32 = DllOpen("avicap32.dll") $dUser32 = DllOpen("user32.dll") ;----- Arrays ----- Global $aPixels[321] Global $aLabels[321] Global $aSplit[1] Global $aBC128[104] $aBC128[1] = "0¬ ¬00¬11011001100" $aBC128[2] = "1¬!¬01¬11001101100" $aBC128[3] = '2¬"¬02¬11001100110' $aBC128[4] = "3¬#¬03¬10010011000" $aBC128[5] = "4¬$¬04¬10010001100" $aBC128[6] = "5¬%¬05¬10001001100" $aBC128[7] = "6¬&¬06¬10011001000" $aBC128[8] = "7¬'¬07¬10011000100" $aBC128[9] = "8¬(¬08¬10001100100" $aBC128[10] = "9¬)¬09¬11001001000" $aBC128[11] = "10¬*¬10¬11001000100" $aBC128[12] = "11¬+¬11¬11000100100" $aBC128[13] = "12¬,¬12¬10110011100" $aBC128[14] = "13¬-¬13¬10011011100" $aBC128[15] = "14¬.¬14¬10011001110" $aBC128[16] = "15¬/¬15¬10111001100" $aBC128[17] = "16¬0¬16¬10011101100" $aBC128[18] = "17¬1¬17¬10011100110" $aBC128[19] = "18¬2¬18¬11001110010" $aBC128[20] = "19¬3¬19¬11001011100" $aBC128[21] = "20¬4¬20¬11001001110" $aBC128[22] = "21¬5¬21¬11011100100" $aBC128[23] = "22¬6¬22¬11001110100" $aBC128[24] = "23¬7¬23¬11101101110" $aBC128[25] = "24¬8¬24¬11101001100" $aBC128[26] = "25¬9¬25¬11100101100" $aBC128[27] = "26¬:¬26¬11100100110" $aBC128[28] = "27¬;¬27¬11101100100" $aBC128[29] = "28¬<¬28¬11100110100" $aBC128[30] = "29¬=¬29¬11100110010" $aBC128[31] = "30¬>¬30¬11011011000" $aBC128[32] = "31¬?¬31¬11011000110" $aBC128[33] = "32¬@¬32¬11000110110" $aBC128[34] = "33¬A¬33¬10100011000" $aBC128[35] = "34¬B¬34¬10001011000" $aBC128[36] = "35¬C¬35¬10001000110" $aBC128[37] = "36¬D¬36¬10110001000" $aBC128[38] = "37¬E¬37¬10001101000" $aBC128[39] = "38¬F¬38¬10001100010" $aBC128[40] = "39¬G¬39¬11010001000" $aBC128[41] = "40¬H¬40¬11000101000" $aBC128[42] = "41¬I¬41¬11000100010" $aBC128[43] = "42¬J¬42¬10110111000" $aBC128[44] = "43¬K¬43¬10110001110" $aBC128[45] = "44¬L¬44¬10001101110" $aBC128[46] = "45¬M¬45¬10111011000" $aBC128[47] = "46¬N¬46¬10111000110" $aBC128[48] = "47¬O¬47¬10001110110" $aBC128[49] = "48¬P¬48¬11101110110" $aBC128[50] = "49¬Q¬49¬11010001110" $aBC128[51] = "50¬R¬50¬11000101110" $aBC128[52] = "51¬S¬51¬11011101000" $aBC128[53] = "52¬T¬52¬11011100010" $aBC128[54] = "53¬U¬53¬11011101110" $aBC128[55] = "54¬V¬54¬11101011000" $aBC128[56] = "55¬W¬55¬11101000110" $aBC128[57] = "56¬X¬56¬11100010110" $aBC128[58] = "57¬Y¬57¬11101101000" $aBC128[59] = "58¬Z¬58¬11101100010" $aBC128[60] = "59¬[¬59¬11100011010" $aBC128[61] = "60¬\¬60¬11101111010" $aBC128[62] = "61¬]¬61¬11001000010" $aBC128[63] = "62¬^¬62¬11110001010" $aBC128[64] = "63¬_¬63¬10100110000" $aBC128[65] = "64¬`¬64¬10100001100" $aBC128[66] = "65¬a¬65¬10010110000" $aBC128[67] = "66¬b¬66¬10010000110" $aBC128[68] = "67¬c¬67¬10000101100" $aBC128[69] = "68¬d¬68¬10000100110" $aBC128[70] = "69¬e¬69¬10110010000" $aBC128[71] = "70¬f¬70¬10110000100" $aBC128[72] = "71¬g¬71¬10011010000" $aBC128[73] = "72¬h¬72¬10011000010" $aBC128[74] = "73¬i¬73¬10000110100" $aBC128[75] = "74¬j¬74¬10000110010" $aBC128[76] = "75¬k¬75¬11000010010" $aBC128[77] = "76¬l¬76¬11001010000" $aBC128[78] = "77¬m¬77¬11110111010" $aBC128[79] = "78¬n¬78¬11000010100" $aBC128[80] = "79¬o¬79¬10001111010" $aBC128[81] = "80¬p¬80¬10100111100" $aBC128[82] = "81¬q¬81¬10010111100" $aBC128[83] = "82¬r¬82¬10010011110" $aBC128[84] = "83¬s¬83¬10111100100" $aBC128[85] = "84¬t¬84¬10011110100" $aBC128[86] = "85¬u¬85¬10011110010" $aBC128[87] = "86¬v¬86¬11110100100" $aBC128[88] = "87¬w¬87¬11110010100" $aBC128[89] = "88¬x¬88¬11110010010" $aBC128[90] = "89¬y¬89¬11011011110" $aBC128[91] = "90¬z¬90¬11011110110" $aBC128[92] = "91¬{¬91¬11110110110" $aBC128[93] = "92¬|¬92¬10101111000" $aBC128[94] = "93¬}¬93¬10100011110" $aBC128[95] = "94¬~¬94¬10001011110" $aBC128[96] = "95¬del¬95¬10111101000" $aBC128[97] = "96¬Fnc3¬96¬10111100010" $aBC128[98] = "97¬Fnc2¬97¬11110101000" $aBC128[99] = "98¬Shift¬98¬11110100010" $aBC128[100] = "99¬Code C¬99¬10111011110" $aBC128[101] = "100¬Fnc 4¬Code B¬10111101110" $aBC128[102] = "101¬Code A¬Code A¬11101011110" $aBC128[103] = "102¬Fnc 1¬Fnc 1¬11110101110" ;************************************************ ; GUI ;************************************************ $GUI = GUICreate("",350,200) GUISetOnEvent($GUI_EVENT_CLOSE,"_Close") ;************************************************ ; WebCam ;************************************************ $dWebCam = DllCall($dAviCap32,"int","capCreateCaptureWindow","str","cap","int", _ BitOR($WS_CHILD,$WS_VISIBLE),"int",15,"int",15,"int",320,"int",50,"hwnd",$GUI,"int",1) DllCall($dUser32,"int","SendMessage","hWnd",$dWebCam[0],"int",0x400 + 10,"int",0,"int",0) DllCall($dUser32,"int","SendMessage","hWnd",$dWebCam[0],"int",0x400 + 50,"int",1,"int",0) DllCall($dUser32,"int","SendMessage","hWnd",$dWebCam[0],"int",0x400 + 52,"int",1,"int",0) ;************************************************ ; Labels ;************************************************ For $i = 1 to 320 If IsInt($i / 50) Then $aLabels[$i] = GUICtrlCreateLabel("",14 + $i,80,1,10) Else $aLabels[$i] = GUICtrlCreateLabel("",14 + $i,80,1,5) EndIf GUICtrlSetBkColor(-1,0x000000) Next $interpretation = GUICtrlCreateLabel("",15,130,320,15) GUISetState(@SW_SHOW) ;################################################################ ; LOOP ;################################################################ While 1 Sleep(100) Wend ;################################################################ ; END LOOP ;################################################################ Func _Cap() ;----- Stop WebCam ----- DllCall($dUser32,"int","SendMessage","hWnd",$dWebCam[0],"int",0x400 + 52,"int",0,"int",0) ;----- Get pixels across center ----- $sString = "" For $i = 1 to 320 $aPixels[$i] = PixelGetColor(14 + $i, 15 + 25) GUICtrlSetBkColor($aLabels[$i], $aPixels[$i]) If _ColorGetRed($aPixels[$i]) > 100 Then $sString &= 0 Else $sString &= 1 EndIf Next ;_ArrayDisplay($aPixels) ;----- split up ----- $sString = StringReplace($sString,"10","1|0") $sString = StringReplace($sString,"01","0|1") $aSplit = StringSplit($sString,"|") ;----- get wdith ----- $iWidth = StringLen($aSplit[2]) / 2 ;----- apply width ----- For $i = 1 to (UBound($aSplit) - 1) $iDiv = StringLen($aSplit[$i]) / $iWidth Select Case $iDiv <= 1.4 $iDiv = 1 Case $iDiv >= 2.5 $iDiv = 3 Case Else $iDiv = 2 EndSelect $aSplit[$i] = $iDiv Next ;_ArrayDisplay($aSplit) ;----- keep only code ----- _ArrayPop($aSplit) _ArrayDelete($aSplit,1) ;_ArrayDisplay($aSplit) ;----- build 1/0 code ----- $sCode = "" $iBool = 1 For $i = 1 to (UBound($aSplit) - 1) For $j = 1 to $aSplit[$i] $sCode &= $iBool Next If $iBool = 1 Then $iBool = 0 Else $iBool = 1 EndIf Next ;----- Split into array ----- Dim $aSplit[1] For $i = 1 To StringLen($sCode) If IsInt($i / 11) Then _ArrayAdd($aSplit,StringMid($sCode,$i - 10,11)) Next ;----- search barcode array ----- $sCode = "" For $i = 1 to (UBound($aSplit) - 1) For $j = 1 to 103 $aData = StringSplit($aBC128[$j],"¬") If $aSplit[$i] = $aData[4] Then $sCode &= $aData[3] Next Next ;----- display text ----- GUICtrlSetData($interpretation,"Barcode text = " & StringTrimRight($sCode,2)) ;----- Start WebCam ----- DllCall($dUser32,"int","SendMessage","hWnd",$dWebCam[0],"int",0x400 + 52,"int",1,"int",0) EndFunc Func _Close() ;************************************************ ; Exit ;************************************************ DllCall($dUser32,"int","SendMessage","hWnd",$dWebCam[0],"int",0x400 + 181,"int",0,"int",0) DllCall($dUser32,"int","SendMessage","hWnd",$dWebCam[0],"int",0x400 + 11,"int",0,"int",0) DllClose($dUser32) DllClose($dAviCap32) Exit EndFunc
Credits go to Rysiora for the basic DLL calls to get the WebCam in the GUI:
http://www.autoitscript.com/forum/index.ph...5&hl=webcam
Have fun.
Edited by andybiochem, 03 February 2009 - 05:56 PM.






