欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

WPFMediaKit摄像头处理

程序员文章站 2022-06-19 16:45:09
第三方库: WPFMediaKit.dll (WPFMediaKit摄像头处理) zing.dll NuGet安装这两个第三方dll 项目截图预览: 项目代码: 项目源代码地址:https://download.csdn.net/download/qingchundaima/11151083 ......

第三方库:

  wpfmediakit.dll (wpfmediakit摄像头处理)

  zing.dll

WPFMediaKit摄像头处理

 

nuget安装这两个第三方dll

 WPFMediaKit摄像头处理

WPFMediaKit摄像头处理

 

 

项目截图预览:

WPFMediaKit摄像头处理

 

 项目代码:

using system;
using system.collections.generic;
using system.drawing;
using system.io;
using system.linq;
using system.runtime.interopservices;
using system.text;
using system.threading.tasks;
using system.windows;
using system.windows.controls;
using system.windows.data;
using system.windows.documents;
using system.windows.input;
using system.windows.media;
using system.windows.media.imaging;
using system.windows.navigation;
using system.windows.shapes;
using system.windows.threading;
using wpfmediakit.directshow.controls;
using zxing;
using zxing.common;
using zxing.qrcode.internal;

namespace qrcode
{
    /// <summary>
    /// mainwindow.xaml 的交互逻辑
    /// </summary>
    public partial class mainwindow : window
    {

        /// <summary>
        /// zxing 二维码扫描类
        /// </summary>
        barcodereader codereader = new barcodereader();

        /// <summary>
        /// 定时器
        /// </summary>
        dispatchertimer cameratimer = new dispatchertimer();

        public mainwindow()
        {
            initializecomponent();

            // 配置的摄像头名称
            var camera = "lenovo easycamera";
            if (multimediautil.videoinputnames.contains(camera))
            {
                //控件制定摄像头
                vce.videocapturesource = camera;
                cameratimer.isenabled = false;
                cameratimer.interval = new timespan(200); //执行间隔0.2秒
                cameratimer.tick += cameratimer_tick;

            }


        }
        /// <summary>
        /// 计时器方法
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void cameratimer_tick(object sender, eventargs e)
        {
            rendertargetbitmap bmp = new rendertargetbitmap((int)vce.actualwidth, (int)vce.actualheight, 96, 96, pixelformats.default);
            vce.measure(vce.rendersize);
            vce.arrange(new rect(vce.rendersize));
            bmp.render(vce);
            bitmapencoder encoder = new jpegbitmapencoder();
            encoder.frames.add(bitmapframe.create(bmp));
            using (memorystream ms = new memorystream())
            {
                encoder.save(ms);
                bitmap btimap = new bitmap(ms);
                var result = codereader.decode(btimap);//解析条码
                if (result != null)
                {
                    // 1:停止识别
                    cameratimer.stop();
                    vce.play(); 
                    messagebox.show($"识别内容为:{result}");

                }
            }
        }

        private void btnshibie_click(object sender, routedeventargs e)
        {
            cameratimer.start();
        }

        private void btnshnegceng_click(object sender, routedeventargs e)
        {
           var codimg= create("hello world!!");
            imgqr.source = changebitmaptoimagesource(codimg);
        }
        /// <summary>
        /// 创建二维码
        /// </summary>
        /// <param name="msg">二维码中保存的信息</param>
        /// <returns></returns>
        public static bitmap create(string msg)
        {
            multiformatwriter writer = new multiformatwriter();
            dictionary<encodehinttype, object> hint = new dictionary<encodehinttype, object>();
            //设置二维码为utf-8编码
            hint.add(encodehinttype.character_set, "utf-8");
            //设置纠错等级, 高
            hint.add(encodehinttype.error_correction, errorcorrectionlevel.h);
            bitmatrix bm = writer.encode(msg, barcodeformat.qr_code, 200, 120, hint);
            barcodewriter barcodewriter = new barcodewriter();
            bitmap bitmap = barcodewriter.write(bm);
            string codepath = directory.getcurrentdirectory() + "/code.jpg";
            if (file.exists(codepath))
                file.delete(codepath);
            bitmap.save(codepath);
            return bitmap;
        }
        /// <summary>
        /// 从bitmap转换成imagesource
        /// </summary>
        /// <param name="icon"></param>
        /// <returns></returns>
        public static imagesource changebitmaptoimagesource(bitmap bitmap)
        {
            intptr hbitmap = bitmap.gethbitmap();

            imagesource wpfbitmap = system.windows.interop.imaging.createbitmapsourcefromhbitmap(
                hbitmap,
                intptr.zero,
                int32rect.empty,
                bitmapsizeoptions.fromemptyoptions());

            if (!deleteobject(hbitmap))
            {
                throw new system.componentmodel.win32exception();
            }
            return wpfbitmap;
        }

        [dllimport("gdi32.dll", setlasterror = true)]
        private static extern bool deleteobject(intptr hobject);
    }
}

  

 项目源代码地址: