Dynamsoft Barcode SDK安装软件包附带强大的扫描码演示。 此Demo支持的条形码扫描功能包括文件读取、扫描仪图像读取和相机视频流读取。
但是,扫描仪和照相机的调用功能不是免费的,需要使用名为Dynamic .NET TWAIN的商用SDK。 本文介绍如何删除扫描仪功能,并将Webcam调用接口替换为DirectShow。
安装
Windows桌面条形码扫描应用
dynamsoftbarcodereader后,查看工程dynamsoftbarcodereadersamplesdesktopc#barcodereaderdemo依赖于此项目的DLL包括Dynamsoft.BarcodeReader.dll、Dynamsoft.ImageCore.dll、Dynamsoft.Forms.Viewer.dll、dynam soft
删除依赖于
扫码工程修改
扫描仪和相机的DLL: Dynamsoft.Camera.dll、Dynamsoft.PDF.dll、Dynamsoft.Twain.dll。从App.config文件中删除key=' DNT许可证' value='许可证- key ' :
? XML版本=' 1.0 '编码=' utf-8 '?
配置
应用程序设置
addkey=' DBR许可证' value='许可证- key ' /
addkey=' DNT许可证' value='许可证- key ' /
/appSettings
/configuration
要从BarcodeReaderDemo.cs中删除scanner选项:
//remove
mroundedrectanglepanelacquireload.controls.add (mthacquireimage;
试着调整一下UI :
//before
mth loadimage.size=new size (103,40 );
mhwebcamimage.location=new point (207,1 );
mhwebcamimage.size=new size (103,40 );
//after
mth loadimage.size=new size (156,40 );
mhwebcamimage.location=new point (157,1 );
mhwebcamimage.size=new size (156,40 );
删除Dynamic .NET TWAIN相关代码:
//remove
mwainmanager=newtwainmanager (dntlicensekeys;
mcamera manager=newcameramanager (dntlicensekeys;
mpdfrasterizer=newpdfrasterizer (dntlicensekeys;
.
构建项目将报告相关错误,并删除所有相应的代码。
调整后的接口:
使用DirectShowNet控制Webcam
创建用于控制DirectShow的DSManager.cs逻辑。定义两个用于存储相机相关参数的结构。
公共结构解决方案
{
公共资源(intwidth,int height ) )。
{
宽度=宽度;
Height=height;
}
公共插入宽度{ get; }
公共int height { get; }
公共复盖字符串tostring ()=#34; ((width ) x ) Height ) )、);
}
公共结构相机信息
{
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)); } }查找每个摄像头对应的分辨率:
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>(); } }把摄像头的名字和分辨率绑定到UI上显示。当设置UI索引的时候,对应的事件函数会被触发:
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 }); } }改造完成。以下是摄像头扫码功能的运行效果:
源码
https://github.com/yushulx/dotnet-webcam-barcode-reader