翻譯|使用教程|編輯:莫成敏|2020-04-07 11:25:15.870|閱讀 547 次
概述:本文將分享如何從項目中刪除掃描儀模塊以及如何使用DirectShow進行網絡攝像頭控制。
# 界面/圖表報表/文檔/IDE等千款熱門軟控件火熱銷售中 >>
Dynamsoft Barcode Reader SDK一款多功能的條碼讀取控件,只需要幾行代碼就可以將條碼讀取功能嵌入到Web或桌面應用程序。這可以節省數月的開發時間和成本。能支持多種圖像文件格式以及從攝像機或掃描儀獲取的DIB格式。使用Dynamsoft Barcode Reader SDK,你可以創建強大且實用的條形碼掃描儀軟件,以滿足你的業務需求。
在Dynamsoft Barcode Reader SDK中,有一個名為BarcodeReaderDemo的精美網絡攝像頭條形碼示例,它支持從磁盤文件、TWAIN兼容的硬件掃描儀和USB網絡攝像頭讀取條形碼。但是,您必須了解的一件事是,從外圍設備獲取圖像的功能依賴于Dynamic .NET TWAIN,該功能也需要收費。幸運的是,圖像查看器庫和圖像編解碼器庫是免費使用的。在本文中,我將分享如何從項目中刪除掃描儀模塊以及如何使用DirectShow進行網絡攝像頭控制。
關于Dynamsoft Barcode Reader演示的知識
下載并安裝Dynamsoft Barcode Reader v7.3。
BarcodeReaderDemo項目位于<Dynamsoft Barcode Reader> \ Samples \ Desktop \ C#\ BarcodeReaderDemo中。它功能強大,可以執行各種條形碼掃描測試。
與項目相關的庫包括Dynamsoft.BarcodeReader.dll,Dynamsoft.ImageCore.dll,Dynamsoft.Forms.Viewer.dll,Dynamsoft.Camera.dll,Dynamsoft.PDF.dll和Dynamsoft.Twain.dll。
Windows安裝程序將為Dynamsoft Barcode Reader和Dynamic .NET TWAIN生成試用許可證,以應用程序配置文件:
<?xml version="1.0" encoding="utf-8" ?> <configuration> <appSettings> <add key ="DBRLicense" value ="LICENSE-KEY"/> <add key ="DNTLicense" value ="LICENSE-KEY"/> </appSettings> </configuration>
注意:Dynamsoft條形碼閱讀器許可證用于Dynamsoft.BarcodeReader.dll,而Dynamic .NET TWAIN許可證則用于Dynamsoft.Camera.dll,Dynamsoft.PDF.dll和Dynamsoft.Twain.dll。
用于網絡攝像頭控制的DirectShowNet
在尋找用于網絡攝像頭編程的C#代碼時,您可能會注意到許多開發人員經常推薦使用OpenCV。OpenCV提供的相機API非常容易使用。但是,OpenCV不適合獲取和設置復雜的攝像機參數,例如枚舉多個攝像機和相關分辨率。要完全使用C#控制網絡攝像頭,我們可以在Windows 10上使用DirectShowNet。
帶有DirectShowNet的網絡攝像頭條形碼掃描儀
在以下各段中,我將修改BarcodeReaderDemo項目,以用DirectShow替換Dynamic .NET TWAIN進行網絡攝像頭編程。
代碼更改
刪除BarcodeReaderDemo.cs中的掃描儀選項卡:
// remove mRoundedRectanglePanelAcquireLoad.Controls.Add(mThAcquireImage);
通過調整標簽大小來美化其余標簽:
// before mThLoadImage.Size = new Size(103, 40); mThWebCamImage.Location = new Point(207, 1); mThWebCamImage.Size = new Size(103, 40); // after mThLoadImage.Size = new Size(156, 40); mThWebCamImage.Location = new Point(157, 1); mThWebCamImage.Size = new Size(156, 40);
刪除動態.NET TWAIN相關代碼:
// remove mTwainManager = new TwainManager(dntLicenseKeys); mCameraManager = new CameraManager(dntLicenseKeys); mPDFRasterizer = new PDFRasterizer(dntLicenseKeys); …
創建一個DSManager.cs文件以實現DirectShow邏輯。
定義兩個結構來存儲攝像機信息:
public struct Resolution { public Resolution(int width, int height) { Width = width; Height = height; } public int Width { get; } public int Height { get; } public override string ToString() => $"({Width} x {Height})"; } public struct CameraInfo { public CameraInfo(DsDevice device, List<Resolution> resolutions) { Device = device; Resolutions = resolutions; } public DsDevice Device { get; } public List<Resolution> Resolutions { get; } }
枚舉相機設備:
DsDevice[] devices = DsDevice.GetDevicesOfCat(FilterCategory.VideoInputDevice); if (devices != null) { cameras = new List<CameraInfo>(); foreach (DsDevice device in devices) { List<Resolution> resolutions = GetAllAvailableResolution(device); cameras.Add(new CameraInfo(device, resolutions)); } }
以下獲取相機分辨率的代碼段來自StackOverflow:
private List<Resolution> GetAllAvailableResolution(DsDevice vidDev) { try { int hr, bitCount = 0; IBaseFilter sourceFilter = null; var m_FilterGraph2 = new FilterGraph() as IFilterGraph2; hr = m_FilterGraph2.AddSourceFilterForMoniker(vidDev.Mon, null, vidDev.Name, out sourceFilter); var pRaw2 = DsFindPin.ByCategory(sourceFilter, PinCategory.Capture, 0); var AvailableResolutions = new List<Resolution>(); VideoInfoHeader v = new VideoInfoHeader(); IEnumMediaTypes mediaTypeEnum; hr = pRaw2.EnumMediaTypes(out mediaTypeEnum); AMMediaType[] mediaTypes = new AMMediaType[1]; IntPtr fetched = IntPtr.Zero; hr = mediaTypeEnum.Next(1, mediaTypes, fetched); while (fetched != null && mediaTypes[0] != null) { Marshal.PtrToStructure(mediaTypes[0].formatPtr, v); if (v.BmiHeader.Size != 0 && v.BmiHeader.BitCount != 0) { if (v.BmiHeader.BitCount > bitCount) { AvailableResolutions.Clear(); bitCount = v.BmiHeader.BitCount; } AvailableResolutions.Add(new Resolution(v.BmiHeader.Width, v.BmiHeader.Height)); } hr = mediaTypeEnum.Next(1, mediaTypes, fetched); } return AvailableResolutions; } catch (Exception ex) { //MessageBox.Show(ex.Message); Console.WriteLine(ex.ToString()); return new List<Resolution>(); } }
將相機名稱和分辨率綁定到下拉列表。如果您更改下拉列表的索引,則會觸發選擇更改事件:
private void InitCameraSource() { cbxWebCamSrc.Items.Clear(); foreach (CameraInfo camera in mDSManager.GetCameras()) { cbxWebCamSrc.Items.Add(camera.Device.Name); } cbxWebCamSrc.SelectedIndex = 0; } private void cbxWebCamSrc_SelectedIndexChanged(object sender, EventArgs e) { picBoxWebCam.Visible = true; picBoxWebCam.BringToFront(); EnableControls(picboxReadBarcode); EnableControls(pictureBoxCustomize); InitCameraResolution(); } private void InitCameraResolution() { cbxWebCamRes.Items.Clear(); foreach (Resolution resolution in mDSManager.GetCameras()[cbxWebCamSrc.SelectedIndex].Resolutions) { cbxWebCamRes.Items.Add(resolution.ToString()); } cbxWebCamRes.SelectedIndex = 0; }
設置用于接收攝像機幀的委托功能:
TaskCompletedCallBack callback = FrameCallback; private volatile bool isFinished = true; public void FrameCallback(Bitmap bitmap) { if (isFinished) { this.BeginInvoke((MethodInvoker)delegate { isFinished = false; ReadFromFrame(bitmap); isFinished = true; }); } private void ReadFromFrame(Bitmap bitmap) { UpdateRuntimeSettingsWithUISetting(); TextResult[] textResults = null; int timeElapsed = 0; try { DateTime beforeRead = DateTime.Now; textResults = mBarcodeReader.DecodeBitmap(bitmap, ""); DateTime afterRead = DateTime.Now; timeElapsed = (int)(afterRead - beforeRead).TotalMilliseconds; if (textResults == null || textResults.Length <= 0) { return; } if (textResults != null) { mDSManager.StopCamera(); Bitmap tempBitmap = ((Bitmap)(bitmap)).Clone(new Rectangle(0, 0, bitmap.Width, bitmap.Height), bitmap.PixelFormat); this.BeginInvoke(mPostShowFrameResults, tempBitmap, textResults, timeElapsed, null); } } catch (Exception ex) { this.Invoke(mPostShowFrameResults, new object[] { bitmap, textResults, timeElapsed, ex }); } }
.NET網絡攝像頭條形碼掃描儀的屏幕截圖
本教程內容就是這樣了,希望對您有所幫助~感興趣的朋友可以下載Dynamsoft Barcode Reader和Dynamic .NET TWAIN試用版免費使用~
相關內容推薦:
Dynamsoft Barcode Reader:解讀條形碼閱讀器技術與開發的基礎知識!
本站文章除注明轉載外,均為本站原創或翻譯。歡迎任何形式的轉載,但請務必注明出處、不得修改原文相關鏈接,如果存在內容上的異議請郵件反饋至chenjj@fc6vip.cn
文章轉載自: