翻譯|使用教程|編輯:莫成敏|2020-04-07 11:25:15.870|閱讀 547 次
概述:本文將分享如何從項(xiàng)目中刪除掃描儀模塊以及如何使用DirectShow進(jìn)行網(wǎng)絡(luò)攝像頭控制。
# 界面/圖表報(bào)表/文檔/IDE等千款熱門(mén)軟控件火熱銷(xiāo)售中 >>
Dynamsoft Barcode Reader SDK一款多功能的條碼讀取控件,只需要幾行代碼就可以將條碼讀取功能嵌入到Web或桌面應(yīng)用程序。這可以節(jié)省數(shù)月的開(kāi)發(fā)時(shí)間和成本。能支持多種圖像文件格式以及從攝像機(jī)或掃描儀獲取的DIB格式。使用Dynamsoft Barcode Reader SDK,你可以創(chuàng)建強(qiáng)大且實(shí)用的條形碼掃描儀軟件,以滿(mǎn)足你的業(yè)務(wù)需求。
在Dynamsoft Barcode Reader SDK中,有一個(gè)名為BarcodeReaderDemo的精美網(wǎng)絡(luò)攝像頭條形碼示例,它支持從磁盤(pán)文件、TWAIN兼容的硬件掃描儀和USB網(wǎng)絡(luò)攝像頭讀取條形碼。但是,您必須了解的一件事是,從外圍設(shè)備獲取圖像的功能依賴(lài)于Dynamic .NET TWAIN,該功能也需要收費(fèi)。幸運(yùn)的是,圖像查看器庫(kù)和圖像編解碼器庫(kù)是免費(fèi)使用的。在本文中,我將分享如何從項(xiàng)目中刪除掃描儀模塊以及如何使用DirectShow進(jìn)行網(wǎng)絡(luò)攝像頭控制。
關(guān)于Dynamsoft Barcode Reader演示的知識(shí)
下載并安裝Dynamsoft Barcode Reader v7.3。
BarcodeReaderDemo項(xiàng)目位于<Dynamsoft Barcode Reader> \ Samples \ Desktop \ C#\ BarcodeReaderDemo中。它功能強(qiáng)大,可以執(zhí)行各種條形碼掃描測(cè)試。
與項(xiàng)目相關(guān)的庫(kù)包括Dynamsoft.BarcodeReader.dll,Dynamsoft.ImageCore.dll,Dynamsoft.Forms.Viewer.dll,Dynamsoft.Camera.dll,Dynamsoft.PDF.dll和Dynamsoft.Twain.dll。
Windows安裝程序?qū)镈ynamsoft Barcode Reader和Dynamic .NET TWAIN生成試用許可證,以應(yīng)用程序配置文件:
<?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。
用于網(wǎng)絡(luò)攝像頭控制的DirectShowNet
在尋找用于網(wǎng)絡(luò)攝像頭編程的C#代碼時(shí),您可能會(huì)注意到許多開(kāi)發(fā)人員經(jīng)常推薦使用OpenCV。OpenCV提供的相機(jī)API非常容易使用。但是,OpenCV不適合獲取和設(shè)置復(fù)雜的攝像機(jī)參數(shù),例如枚舉多個(gè)攝像機(jī)和相關(guān)分辨率。要完全使用C#控制網(wǎng)絡(luò)攝像頭,我們可以在Windows 10上使用DirectShowNet。
帶有DirectShowNet的網(wǎng)絡(luò)攝像頭條形碼掃描儀
在以下各段中,我將修改BarcodeReaderDemo項(xiàng)目,以用DirectShow替換Dynamic .NET TWAIN進(jìn)行網(wǎng)絡(luò)攝像頭編程。
代碼更改
刪除BarcodeReaderDemo.cs中的掃描儀選項(xiàng)卡:
// remove mRoundedRectanglePanelAcquireLoad.Controls.Add(mThAcquireImage);
通過(guò)調(diào)整標(biāo)簽大小來(lái)美化其余標(biāo)簽:
// 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);
刪除動(dòng)態(tài).NET TWAIN相關(guān)代碼:
// remove mTwainManager = new TwainManager(dntLicenseKeys); mCameraManager = new CameraManager(dntLicenseKeys); mPDFRasterizer = new PDFRasterizer(dntLicenseKeys); …
創(chuàng)建一個(gè)DSManager.cs文件以實(shí)現(xiàn)DirectShow邏輯。
定義兩個(gè)結(jié)構(gòu)來(lái)存儲(chǔ)攝像機(jī)信息:
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; } }
枚舉相機(jī)設(shè)備:
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)); } }
以下獲取相機(jī)分辨率的代碼段來(lái)自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>(); } }
將相機(jī)名稱(chēng)和分辨率綁定到下拉列表。如果您更改下拉列表的索引,則會(huì)觸發(fā)選擇更改事件:
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; }
設(shè)置用于接收攝像機(jī)幀的委托功能:
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網(wǎng)絡(luò)攝像頭條形碼掃描儀的屏幕截圖
本教程內(nèi)容就是這樣了,希望對(duì)您有所幫助~感興趣的朋友可以下載Dynamsoft Barcode Reader和Dynamic .NET TWAIN試用版免費(fèi)使用~
相關(guān)內(nèi)容推薦:
Dynamsoft Barcode Reader:解讀條形碼閱讀器技術(shù)與開(kāi)發(fā)的基礎(chǔ)知識(shí)!
本站文章除注明轉(zhuǎn)載外,均為本站原創(chuàng)或翻譯。歡迎任何形式的轉(zhuǎn)載,但請(qǐng)務(wù)必注明出處、不得修改原文相關(guān)鏈接,如果存在內(nèi)容上的異議請(qǐng)郵件反饋至chenjj@fc6vip.cn
文章轉(zhuǎn)載自: