簡單的Windows Webcam應用:Barcode Reader

來源:互聯網
上載者:User

標籤:圖片載入   reader   lib   products   屬性   com   height   new   callback   

原文:簡單的Windows Webcam應用:Barcode Reader

在Windows上用WinForm建立一個Webcam應用需要用到DirectShow。DirectShow沒有提供C#的介面。如果要用C#開發,需要建立一個橋接DLL。Touchless SDK是一個免費開源的.NET庫,對DirectShow進行了簡單的封裝。使用Touchless可以很方便的在WinForm應用中調用camera。這裡分享下如何建立一個調用webcam的barcode reader。

參考原文:WinForm Barcode Reader with Webcam and C#

Xiao Ling

翻譯:yushulx

WinForm Barcode Reader

下載Touchless SDK。

Dynamsoft Barcode Reader SDK用於barcode識別. 如要想用免費開源的,可以選擇ZXing.NET。

開啟Visual Studio 2015建立一個WinForm工程.

通過Nuget可以在工程中直接下載安裝Dynamsoft Barcode Reader:

在引用中添加TouchlessLib.dll:

把WebCamLib.dll添加到工程中。屬性中設定拷貝。這樣工程編譯之後就會把DLL拷貝到輸出目錄中,不需要再手動拷貝。

初始化Touchless和Dynamsoft Barcode Reader:

// Initialize Dynamsoft Barcode Reader_barcodeReader = new BarcodeReader();// Initialize Touchless_touch = new TouchlessMgr();

通過系統對話方塊把圖片載入到PictureBox中:

using (OpenFileDialog dlg = new OpenFileDialog()){    dlg.Title = "Open Image";     if (dlg.ShowDialog() == DialogResult.OK)    {        Bitmap bitmap = null;                 try        {            bitmap =  new Bitmap(dlg.FileName);        }        catch (Exception exception)        {            MessageBox.Show("File not supported.");            return;        }         pictureBox1.Image = new Bitmap(dlg.FileName);    }}

設定回呼函數啟動webcam:

// Start to acquire images_touch.CurrentCamera = _touch.Cameras[0];_touch.CurrentCamera.CaptureWidth = _previewWidth; // Set width_touch.CurrentCamera.CaptureWidth = _previewHight; // Set height_touch.CurrentCamera.OnImageCaptured += new EventHandler<CameraEventArgs>(OnImageCaptured); // Set preview callback function

camera的資料返回不是在UI線程。要顯示結果,需要調用UI線程:

private void OnImageCaptured(object sender, CameraEventArgs args){    // Get the bitmap    Bitmap bitmap = args.Image;     // Read barcode and show results in UI thread    this.Invoke((MethodInvoker)delegate    {        pictureBox1.Image = bitmap;        ReadBarcode(bitmap);    });}

識別barcode:

private void ReadBarcode(Bitmap bitmap){    // Read barcodes with Dynamsoft Barcode Reader    Stopwatch sw = Stopwatch.StartNew();    sw.Start();    BarcodeResult[] results = _barcodeReader.DecodeBitmap(bitmap);    sw.Stop();    Console.WriteLine(sw.Elapsed.TotalMilliseconds + "ms");     // Clear previous results    textBox1.Clear();     if (results == null)    {        textBox1.Text = "No barcode detected!";        return;    }     // Display barcode results    foreach (BarcodeResult result in results)    {                        textBox1.AppendText(result.BarcodeText + "\n");        textBox1.AppendText("\n");    }}

運行程式:

使用演算法介面的時候需要注意一下效能。可以使用Stopwatch來計算時間消耗:

Stopwatch sw = Stopwatch.StartNew();sw.Start();BarcodeResult[] results = _barcodeReader.DecodeBitmap(bitmap);sw.Stop();Console.WriteLine(sw.Elapsed.TotalMilliseconds + "ms");
源碼

https://github.com/yushulx/windows-webcam-barcode-reader

簡單的Windows Webcam應用:Barcode Reader

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.