WebCallWork.cs 3.9 KB
using System;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.IO;
using Model;
using System.Runtime.Serialization.Formatters.Binary;
using System.Drawing;
using System.Collections.Generic;
using BLL;

namespace SmartScan
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.Single, ConcurrencyMode = ConcurrencyMode.Single, IncludeExceptionDetailInFaults = true)]
    [AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
    internal class WebCallWork : IWeb
    {
        public WebCallWork()
        {
        }

        public void CloseApp()
        {
            LogNet.log.Info("WebService CloseApp 接口被调用");
            Common.frmMain.Invoke(new Action(() => { Common.frmMain.Close(); }));
        }

        public WebResultCamera WorkWithCamera()
        {
            LogNet.log.Info("WebService WorkWithCamera 接口被调用");
            WebCodeAll[] msg = null;
            Common.frmMain.Invoke(new Action(() => { msg = Common.frmMain.WebTouchWork(); }));
            return new WebResultCamera() { Data = msg };
        }

        public WebResultCode WorkWithCode(Stream json)
        {
            LogNet.log.Info($"WebService WorkWithCode 接口被调用");

            StreamReader sr = new(json);
            string input = sr.ReadToEnd();
            System.Web.Script.Serialization.JavaScriptSerializer serializer = new();
            object[] obj = (object[])serializer.DeserializeObject(input);
            string[] str = new string[obj.Length];
            for (int i = 0; i < str.Length; i++)
                str[i] = obj[i].ToString();

            WebCodeText[] msg = null;
            Common.frmMain.Invoke(new Action(() => { msg = Common.frmMain.WebTouchWork(str); }));
            return new WebResultCode() { Data = msg };
        }
        public string alive()
        {
            return "1";
        }
        public WebResultCode ProcessBitmap(Stream info, string param)
        {
            BinaryFormatter bf = new BinaryFormatter();
            Bitmap bitmap = (Bitmap)bf.Deserialize(info);
            try
            {
                List<CameraVisionLib.Model.BarcodeInfo> workCodeInfo = new();
                Dictionary<string, string> workCodeKeyword;// = new(StringComparer.OrdinalIgnoreCase);
                bool[] originalCodeIsUsed = null;
                workCodeInfo = Camera.GetBarCode(bitmap);

                if (workCodeInfo.Count == 0)
                    return new WebResultCode() { ErrorCode = -1, Msg = "扫码失败" };
                BLLCommon.mateEdit.CurrntBitmap = bitmap;
                bool rtn = BLLCommon.mateEdit.MatchingTemplate(workCodeInfo, BLLCommon.config.DefaultMaterialName, false, out string mateName, out workCodeKeyword, out originalCodeIsUsed);

                LogNet.log.Info("Work SetKey hasMatch:" + rtn);
                WebResultCode webResultCode = null;

                if (!BLLCommon.extension.SetKey(null, workCodeKeyword, rtn, out string errmsg))
                {
                    webResultCode = new WebResultCode() { ErrorCode = -2, Msg = errmsg };

                }

                if (webResultCode != null || !rtn)
                    return webResultCode;
                LogNet.log.Info("Work scan is done");
                List<KeyValuePair<string, string>> result = new List<KeyValuePair<string, string>>();

                foreach (var wc in workCodeKeyword)
                {
                    result.Add(new KeyValuePair<string, string>(wc.Key, wc.Value));
                }
                return new WebResultCode() { workCodeKeyword = result, workCodeInfo = workCodeInfo };
            }
            catch (Exception ex)
            {
                LogNet.log.Error("ProcessBitmap", ex);
                return new WebResultCode() { ErrorCode = -1, Msg = "扫码失败:" + ex };
            }
        }
    }


}