EyemSamples.cs 3.2 KB
using Microsoft.Win32.SafeHandles;
using System;
using System.Runtime.InteropServices;

namespace eyemLib_Sharp
{
    public unsafe class EyemSamples
    {
        #region 通用

        // Win32 memory copy function
        [DllImport("ntdll.dll", CallingConvention = CallingConvention.Cdecl)]
        private static extern byte* memcpy(byte* dst, byte* src, int count);
        //读取图像
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemImageRead(string filename, int iFalgs, out EyemImage ucpImage);
        //保存图像
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemImageWrite(string filename, EyemImage ipImage);
        //释放图像资源
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern void eyemImageFree(IntPtr ipImage);

        #endregion

        //读码程序
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern int eyemDetectAndDecode(EyemImage tpImage, EyemRect tpRoi, string fileName, string strCodeType, out DataCodeHandle hObject, out EyemBarCode* tpResults, out int ipNum, bool bUseNiBlack, int iBlockSize, int iRangeC, int iSymbolMin, int iSymbolMax, double dToleErr = 0.5, double dMinorStep = 1.0);
        //释放工具
        [DllImport("eyemLib.dll", CharSet = CharSet.None, CallingConvention = CallingConvention.Cdecl)]
        private static extern bool eyemDetectAndDecodeFree(IntPtr hObject);

        public static void eyemReadImageTool(string fileName)
        {
            EyemImage ucpImage;
            int flag = eyemImageRead(fileName, -1, out ucpImage);
            if (flag != 0)
            {
                Console.WriteLine("读图失败!");
                return;
            }
            EyemRect tpRoi = new EyemRect();
            tpRoi.iXs = tpRoi.iYs = 0;
            tpRoi.iWidth = ucpImage.iWidth;
            tpRoi.iHeight = ucpImage.iHeight;

            string file = fileName.Split(new string[] { "\\" }, StringSplitOptions.RemoveEmptyEntries)[2];
            int ipNum; EyemBarCode* tpResults;
            DataCodeHandle hObject;
            eyemDetectAndDecode(ucpImage, tpRoi, file.Replace(".png", ""), "QRCode", out hObject, out tpResults, out ipNum, false, 11, 5, 128, 215);
            for (int i = 0; i < ipNum; i++)
            {
                Console.WriteLine("类型:" + Marshal.PtrToStringAnsi(tpResults[i].hType) + ";坐标" + "[" + tpResults[i].iCenterX.ToString() + "," + tpResults[i].iCenterY.ToString() + "]" + ";内容:" + Marshal.PtrToStringAnsi(tpResults[i].hText) + "");
            }
            //释放
            hObject.Dispose();
            //释放图像
            eyemImageFree(ucpImage.ucpImage);
        }

        //释放解码句柄
        public class DataCodeHandle : SafeHandleZeroOrMinusOneIsInvalid
        {
            public DataCodeHandle() : base(true) { }
            protected override bool ReleaseHandle()
            {
                return eyemDetectAndDecodeFree(handle);
            }
        }
    }
}