c# 链接相机 vericode解码小程序

Posted 东东就是我

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了c# 链接相机 vericode解码小程序相关的知识,希望对你有一定的参考价值。

1.程序主体

2.演示效果

3.检测类

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Runtime.InteropServices;
using System.Drawing.Imaging;
using System.IO;
using HalconDotNet;

namespace VeriCode_Demo
{
    public partial class Form1 : Form
    {

        //---------------------------------------------------------------解码
        [DllImport("VRdll.dll", EntryPoint = "vcRead", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        private static extern short vcRead(short xSize, short ySize, int pimg, StringBuilder msg, int IsContrastNormal, ref short vcCorners, short SampleWidth, short BitsPerCell, short Prefiltering);

        [DllImport("VRdll.dll", EntryPoint = "GetDllVersion", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        private static extern short GetDllVersion(StringBuilder szbuf, int nBufSize);


        [DllImport("VRdll.dll", EntryPoint = "GetSymbolCorners", CharSet = CharSet.Ansi, CallingConvention = CallingConvention.Cdecl)]
        private static extern void GetSymbolCorners(ref short szbuf);


        [DllImport("kernel32.dll", EntryPoint = "RtlMoveMemory", CharSet = CharSet.Ansi)]
        public extern static long CopyMemory(int dest, int source, int size);

        int mwidth, mheight;
        int pt;
        byte[] byteAry;
        int totbyte;
        bool demo;
        short minVericode_length;
        BitmapData bitmapData;
        Rectangle rect;
        Pen RedPen = new Pen(Color.Lime, 3);
        bool License;

        public Form1()
        {
            InitializeComponent();
        }
        bool openCamear = false;//控制相机开关
        GrabImage grabimg = new GrabImage();//实例化GrabImage类
        //连接相机
        private void button1_Click(object sender, EventArgs e)
        {
            if (openCamear == false)
            {
                openCamear = true;
                grabimg.open();//调用open方法
                textBox1.AppendText("相机连接成功!" + "\\r\\n");
            }
            else
            {
                MessageBox.Show("相机已打开!");
            }


        }
        //单次采集图片
        private void button2_Click(object sender, EventArgs e)
        {


            if (openCamear == true)
            {
                timer1.Enabled = false;

                Bitmap img = grabimg.grabimage();
                listBox1_SelectedIndexChanged(img);
            }
            else
            {
                MessageBox.Show("相机未打开!");
            }


        }

        private void Form1_Load(object sender, EventArgs e)
        {

          
        }
        //退出
        private void button4_Click(object sender, EventArgs e)
        {
            System.Environment.Exit(0);

        }
        //连续采集图片
        private void button5_Click(object sender, EventArgs e)
        {
            if (openCamear == true)
            {
                timer1.Enabled = true;
            }
            else
            {
                MessageBox.Show("相机未打开!");
            }

        }
        //关闭相机
        private void button6_Click(object sender, EventArgs e)
        {
            if (openCamear == true)
            {
                openCamear = false;
                timer1.Enabled = false;
                grabimg.close();
            }
            else
            {
                MessageBox.Show("相机已关闭!");
            }

        }
        //获取图片
        private void timer1_Tick(object sender, EventArgs e)
        {

            Bitmap img= grabimg.grabimage();
            listBox1_SelectedIndexChanged(img);

        }
        //加载图片
        private void button3_Click_1(object sender, EventArgs e)
        {
            //选择文件
            OpenFileDialog dialog = new OpenFileDialog();
            dialog.Multiselect = false;//该值确定是否可以选择多个文件
            dialog.Title = "打开文件";
            dialog.Filter = "图片|*.jpg;*.png;*.gif;*.jpeg;*.bmp";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                string file = dialog.FileName;
                Bitmap img = (Bitmap)Image.FromFile(file);
                listBox1_SelectedIndexChanged(img);
            }
        }

        private void listBox1_SelectedIndexChanged(Bitmap img)
        {
            //Bitmap img;
            int[] ptr = new int[2];
            byte[] tmp_data;
            int i;
            int j;
            
            rect = new Rectangle(0, 0, img.Width, img.Height);
            pictureBox1.Image = img;
            pictureBox1.Refresh();
            mwidth = img.Width;
            mheight = img.Height;
            switch (img.PixelFormat)
            {
                case PixelFormat.Format8bppIndexed:
                    totbyte = img.Width * img.Height;
                    byteAry = new byte[totbyte];
                    bitmapData = img.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format8bppIndexed);
                    ptr[0] = VarPtr(byteAry);
                    ptr[1] = bitmapData.Scan0.ToInt32();
                    if (mwidth % 4 == 0)
                    {
                        CopyMemory(ptr[0], ptr[1], totbyte);
                    }
                    else
                    {
                        for (i = 0; i < mheight; i += 1)
                        {
                            CopyMemory(ptr[0], ptr[1], mwidth);
                            ptr[0] += mwidth;
                            ptr[1] += bitmapData.Stride;
                        }
                    }
                    img.UnlockBits(bitmapData);
                    break;
                case PixelFormat.Format24bppRgb:
                    totbyte = img.Width * img.Height * 3;
                    tmp_data = new byte[totbyte];
                    byteAry = new byte[totbyte / 3];
                    bitmapData = img.LockBits(rect, ImageLockMode.ReadOnly, PixelFormat.Format24bppRgb);
                    ptr[0] = VarPtr(tmp_data);
                    ptr[1] = bitmapData.Scan0.ToInt32();
                    if (mwidth % 4 == 0)
                    {
                        CopyMemory(ptr[0], ptr[1], totbyte);
                    }
                    else
                    {
                        for (i = 0; i < mheight; i += 1)
                        {
                            CopyMemory(ptr[0], ptr[1], mwidth * 3);
                            ptr[0] += mwidth * 3;
                            ptr[1] += bitmapData.Stride;
                        }
                    }
                    img.UnlockBits(bitmapData);
                    j = 0;
                    for (i = 0; i < totbyte; i += 3)
                    {
                        byteAry[j] = tmp_data[i];
                        j++;
                    }
                    break;
            }

            pt = VarPtr(byteAry);
            Chk_Vericode();

        }
        //指针地址
        private int VarPtr(object obj)
        {
            int returnValue;
            GCHandle GC = GCHandle.Alloc(obj, GCHandleType.Pinned);
            returnValue = GC.AddrOfPinnedObject().ToInt32();
            GC.Free();
            return returnValue;
        }
        private void Chk_Vericode()
        {
            short intRetCode = 0;
            string pucUserData = "";
            int pucMono;
            short[] vcCorners = new short[8];
            Point[] UL = new Point[2];
            Point[] UR = new Point[2];
            Point[] LL = new Point[2];
            Point[] LR = new Point[2];
            bool fg;
            int t1;
            int t2;
            Bitmap img1;
            Graphics g;
            pucMono = pt;
            t1 = Environment.TickCount;
            fg = Decode_Vericode((short)mwidth, (short)mheight, pucMono, ref pucUserData, ref vcCorners, ref intRetCode, demo, minVericode_length);

            t2 = Environment.TickCount;
            t2 = ((t2 < t1) ? (int)(49.7 * 86400000 + t2) : t2);
            if (fg)
            {
                UL[0].X = vcCorners[0];
                UL[0].Y = mheight - vcCorners[1] - 1;
                UL[1].X = vcCorners[2];
                UL[1].Y = mheight - vcCorners[3] - 1;
                UR[0].X = vcCorners[2];
                UR[0].Y = mheight - vcCorners[3] - 1;
                UR[1].X = vcCorners[6];
                UR[1].Y = mheight - vcCorners[7] - 1;
                LL[0].X = vcCorners[6];
                LL[0].Y = mheight - vcCorners[7] - 1;
                LL[1].X = vcCorners[4];
                LL[1].Y = mheight - vcCorners[5] - 1;
                LR[0].X = vcCorners[4];
                LR[0].Y = mheight - vcCorners[5] - 1;
                LR[1].X = vcCorners[0];
                LR[1].Y = mheight - vcCorners[1] - 1;
                img1 = new Bitmap(mwidth, mheight);
                g = Graphics.FromImage(img1);
                g.DrawImage(pictureBox1.Image, 0, 0, mwidth, mheight);
                g.DrawLine(RedPen, UL[0].X, UL[0].Y, UL[1].X, UL[1].Y);
                g.DrawLine(RedPen, UR[0].X, UR[0].Y, UR[1].X, UR[1].Y);
                g.DrawLine(RedPen, LL[0].X, LL[0].Y, LL[1].X, LL[1].Y);
                g.DrawLine(RedPen, LR[0].X, LR[0].Y, LR[1].X, LR[1].Y);

                Font font = new System.Drawing.Font("宋体", 18);
                SolidBrush brush = new SolidBrush(Color.Lime);
                g.DrawString(pucUserData, font, brush, LR[1].X, LR[1].Y);

                pictureBox1.Image = img1;

                textBox1.AppendText( "\\r\\n\\r\\nCode : " + pucUserData);
            }
            else
            {
                textBox1.AppendText( "\\r\\n\\r\\nCode : No Data Decoded&#

以上是关于c# 链接相机 vericode解码小程序的主要内容,如果未能解决你的问题,请参考以下文章

C#解码base64编码的二进制数据的代码

C#开发微信小程序

在 C# 中将 Base64 字节数组解码为图像

C#程序员经常用到的10个实用代码片段 - 操作系统

C#程序员经常用到的10个实用代码片段

片段中的 Android 相机预览