Web摄像机如何读取条码?本教程将分享在Barcode Reader SDK中用Web摄像机当做条码读取器读取条码。以C#代码为例为大家展示:
当用户启动开始时,摄像机里面所产生的画面作为图片。
1 // <summary> 2 /// Function to read barcodes from image (used by new created Thread object) 3 /// </summary> 4 public void <span data-scayt_word="startImageProcessing" data-scaytid="2">startImageProcessing</span>() 5 { 6 try 7 { 8 // work while user has not changed the status to false (i.e. false <span data-scayt_word="meanse" data-scaytid="3">meanse</span> user canceled the scan) 9 while (STATUS) 10 { 11 // get current frame bitmap from camera using <span data-scayt_word="Touchless" data-scaytid="4">Touchless</span> lib 12 Bitmap bitmap = MANAGER.CurrentCamera.GetCurrentImage(); 13 // search for bar codes 14 <span data-scayt_word="findBarcodes" data-scaytid="5">findBarcodes</span>(bitmap); 15 16 // wait for a little to lower the CPU load 17 Thread.Sleep(SCAN_DELAY); 18 } 19 } 20 catch 21 { 22 ; // suppress errors if any 23 } 24 }
BarCode Reader SDK浏览图片读取条码
1 /// <summary> 2 /// Finds barcodes from bitmap object 3 /// </summary> 4 /// <param name="img">input bitmap 5 private void <span data-scayt_word="findBarcodes" data-scaytid="8">findBarcodes</span>(Bitmap <span data-scayt_word="img" data-scaytid="10">img</span>) 6 { 7 // create barcode object 8 Reader reader = new Reader(); 9 10 // limit to <span data-scayt_word="1D" data-scaytid="12">1D</span> barcodes search only (<span data-scayt_word="2d" data-scaytid="14">2d</span> are not included to speed up search) 11 // change to SymbologyFilter.FindAll to scan for all possible <span data-scayt_word="1D" data-scaytid="13">1D</span> and <span data-scayt_word="2D" data-scaytid="15">2D</span> barcodes 12 reader.TypeToFind = <span data-scayt_word="GetBarcodeTypeToFindFromCombobox" data-scaytid="16">GetBarcodeTypeToFindFromCombobox</span>(); 13 14 // you may optimize processing by setting number of barcodes per page 15 //reader.MaxNumberOfBarcodesPerPage = 1; 16 17 // read barcodes from bitmap 18 reader.ReadFrom(<span data-scayt_word="img" data-scaytid="11">img</span>); 19 20 // save current time 21 String <span data-scayt_word="timeNow" data-scaytid="17">timeNow</span> = string.Format("{0:<span data-scayt_word="HH" data-scaytid="19">HH</span>:mm:<span data-scayt_word="ss" data-scaytid="20">ss</span>:<span data-scayt_word="tt" data-scaytid="21">tt</span>}", DateTime.Now); 22 23 // check barcode scan results 24 if (reader.FoundBarcodes.Length > 0) 25 { 26 textAreaBarcodes.SelectionStart = 0; 27 textAreaBarcodes.SelectionLength = 0; 28 textAreaBarcodes.SelectedText = "\<span data-scayt_word="nTime" data-scaytid="22">nTime</span>: " + <span data-scayt_word="timeNow" data-scaytid="18">timeNow</span> + "\n"; 29 30 // insert barcodes into the text area output 31 <span data-scayt_word="foreach" data-scaytid="23">foreach</span> (<span data-scayt_word="FoundBarcode" data-scaytid="24">FoundBarcode</span> barcode in reader.FoundBarcodes) 32 { 33 // make a sound that we found the barcode (not working on <span data-scayt_word="x64" data-scaytid="25">x64</span> due to Microsoft changed it) 34 Console.Beep(); 35 // form the string with barcode value 36 String <span data-scayt_word="barcodeValue" data-scaytid="26">barcodeValue</span> = String.Format("Found: {0} {1}" + "\r\n", barcode.Type, barcode.Value); 37 // add barcode to the text area output 38 textAreaBarcodes.SelectedText = <span data-scayt_word="barcodeValue" data-scaytid="27">barcodeValue</span> + "\n"; 39 // add barcode to the list of saved barcodes 40 m_foundBarcodes.Add(barcode); 41 // increase counter of found barcodes 42 <span data-scayt_word="foundBarcodesCount" data-scaytid="28">foundBarcodesCount</span>++; 43 // update status text with number of barcodes 44 lblFoundBarcodes.Text = String.Format("Found {0} barcodes:", <span data-scayt_word="foundBarcodesCount" data-scaytid="29">foundBarcodesCount</span>); 45 } 46 } 47 // make flickering for "Scanning..." label 48 lblScanning.Visible = !lblScanning.Visible; 49 lblScanning.Update(); 50 51 }
本文译自Barcode