搜索
您的当前位置:首页ZBar条形码识别

ZBar条形码识别

来源:乌哈旅游

 

using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;

namespace Sci
{
    public class ZBarTool
    {
        public static string ScanBarCodeZbar(Image primaryImage)
        {
            return (string)CallStatic(ToolAsm(), "ZBarTool.API", "ScanBarCodeZbar", true, new object[] { primaryImage });
        }

        /// <summary>
        /// 识别primaryImage上的二维码或条形码(多个条形码、二维码)
        /// </summary>
        public static List<string> ScanBarCodeZbarList(System.Drawing.Image primaryImage)
        {
            var result = CallStatic(ToolAsm(), "ZBarTool.API", "ScanBarCodeZbarList", true, new object[] { primaryImage });
            return result as List<string>;
        }

        #region 工具插件

        /// <summary>
        ///  调用工具中的static方法
        /// </summary>
        /// <param name="assembly">Assembly</param>
        /// <param name="args">参数</param>
        /// <returns></returns>
        private static object CallStatic(Assembly assembly, string classFullName, string methodName, bool IsPublic, object[] args)
        {
            Type type = assembly.GetType(classFullName, true, true);    // 获取入口类 或 entryPoint.DeclaringType

            // 调用程序集的静态方法: Type.InvokeMember
            object obj = type.InvokeMember(methodName, BindingFlags.InvokeMethod | (IsPublic ? BindingFlags.Public : BindingFlags.NonPublic) | BindingFlags.Static, null, null, args);
            return obj;
        }

        /// <summary>
        /// 获取工具的Assembly
        /// </summary>
        /// <returns></returns>
        private static Assembly ToolAsm()
        {
            if (asm == null)
            {
                byte[] data = Convert.FromBase64String(File.ReadAllText("ZBarTool.txt"));
                asm = Assembly.Load(data);
            }
            return asm;
        }

        private static Assembly asm = null;

        #endregion
    }
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Top