Dynamsoft Barcode Reader教程:如何使用DirectShow进行 络摄像头控制

Dynamsoft Barcode Reader SDK一款多功能的条码读取控件,只需要几行代码就可以将条码读取功能嵌入到Web或桌面应用程序。这可以节省数月的开发时间和成本。能支持多种图像文件格式以及从摄像机或扫描仪获取的DIB格式。使用Dynamsoft Barcode Reader SDK,你可以创建强大且实用的条形码扫描仪软件,以满足你的业务需求。

Dynamsoft Barcode Reader正式版

关于Dynamsoft Barcode Reader演示的知识

下载并安装Dynamsoft Barcode Reader v7.3。

BarcodeReaderDemo项目位于<Dynamsoft Barcode Reader> Samples Desktop C# BarcodeReaderDemo中。它功能强大,可以执行各种条形码扫描测试。

Dynamsoft Barcode Reader教程:如何使用DirectShow进行 络摄像头控制

与项目相关的库包括Dynamsoft.BarcodeReader.dll,Dynamsoft.ImageCore.dll,Dynamsoft.Forms.Viewer.dll,Dynamsoft.Camera.dllDynamsoft.PDF.dllDynamsoft.Twain.dll

Windows安装程序将为Dynamsoft Barcode Reader和Dynamic .NET TWAIN生成试用许可证,以应用程序配置文件:

<ml version="1.0" encoding="utf-8" gt;<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.dllDynamsoft.PDF.dllDynamsoft.Twain.dll

用于 络摄像头控制的DirectShowNet

在寻找用于 络摄像头编程的C#代码时,您可能会注意到许多开发人员经常推荐使用OpenCV。OpenCV提供的相机API非常容易使用。但是,OpenCV不适合获取和设置复杂的摄像机参数,例如枚举多个摄像机和相关分辨率。要完全使用C#控制 络摄像头,我们可以在Windows 10上使用DirectShowNet。

带有DirectShowNet的 络摄像头条形码扫描仪

在以下各段中,我将修改BarcodeReaderDemo项目,以用DirectShow替换Dynamic .NET TWAIN进行 络摄像头编程。

代码更改

删除BarcodeReaderDemo.cs中的扫描仪选项卡:

// removemRoundedRectanglePanelAcquireLoad.Controls.Add(mThAcquireImage);

通过调整标签大小来美化其余标签:

// beforemThLoadImage.Size = new Size(103, 40);mThWebCamImage.Location = new Point(207, 1);mThWebCamImage.Size = new Size(103, 40);// aftermThLoadImage.Size = new Size(156, 40);mThWebCamImage.Location = new Point(157, 1);mThWebCamImage.Size = new Size(156, 40);

Dynamsoft Barcode Reader教程:如何使用DirectShow进行 络摄像头控制

删除动态.NET TWAIN相关代码:

// removemTwainManager = 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 &amp;&amp; mediaTypes[0] != null)        {            Marshal.PtrToStructure(mediaTypes[0].formatPtr, v);            if (v.BmiHeader.Size != 0 &amp;&amp; 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教程:如何使用DirectShow进行 络摄像头控制

Dynamsoft Barcode Reader教程:如何使用DirectShow进行 络摄像头控制

本教程内容就是这样了,希望对您有所帮助~感兴趣的朋友可以下载Dynamsoft Barcode Reader和Dynamic .NET TWAIN试用版免费使用~

相关内容推荐:

Dynamsoft Barcode Reader:解读条形码阅读器技术与开发的基础知识!


想要购买Dynamsoft Barcode Reader正版授权,或了解更多产品信息请点击【咨询在线客服】

Dynamsoft Barcode Reader教程:如何使用DirectShow进行 络摄像头控制

标签:

声明:本站部分文章及图片源自用户投稿,如本站任何资料有侵权请您尽早请联系jinwei@zod.com.cn进行处理,非常感谢!

上一篇 2020年3月4日
下一篇 2020年3月4日

相关推荐

发表回复

登录后才能评论