RemoteDecodeHelper.cs 5.4 KB
//using CodeLibrary;
//using HalconDotNet;
//using OnlineStore.Common;
//using System;
//using System.Collections.Generic;
//using System.Diagnostics;
//using System.Drawing;
//using System.IO;
//using System.Linq;
//using System.Net;
//using System.Runtime.Serialization.Formatters.Binary;
//using System.Text;
//using System.Threading;
//using System.Threading.Tasks;
//using System.Xml;
//using System.Xml.Serialization;

//public class RemoteDecodeHelper
//{

//    private static int webclienttimeout = 30000;

//    private static Process p = new Process();

//    private const string serverhost = "http://127.0.0.1:58137/";

//    public static List<CodeInfo> DecodeRequest(HObject hoimg, RemoteDecodeParam remoteDecodeParam)
//    {
//        byte[] requestdata;
//        lock (hoimg)
//        {
//            using (MemoryStream memoryStream = new MemoryStream())
//            {
//                hoimg.Serialize(memoryStream);
//                requestdata = memoryStream.ToArray();
//                memoryStream.Close();
//            }
//        }

//        return DecodeRequest(requestdata, remoteDecodeParam, isHObject: true);
//    }

//    public static List<CodeInfo> DecodeRequest(Bitmap bitmap, RemoteDecodeParam remoteDecodeParam)
//    {
//        if (bitmap == null)
//        {
//            return null;
//        }

//        byte[] requestdata;
//        lock (bitmap)
//        {
//            using (MemoryStream memoryStream = new MemoryStream())
//            {
//                BinaryFormatter binaryFormatter = new BinaryFormatter();
//                binaryFormatter.Serialize(memoryStream, bitmap);
//                requestdata = memoryStream.ToArray();
//                memoryStream.Close();
//            }
//        }

//        return DecodeRequest(requestdata, remoteDecodeParam);
//    }

//    private static List<CodeInfo> DecodeRequest(byte[] requestdata, RemoteDecodeParam remoteDecodeParam, bool isHObject = false)
//    {
//        CheckAndRunServer();
//        string text;
//        using (MemoryStream memoryStream = new MemoryStream())
//        {
//            XmlSerializer xmlSerializer = new XmlSerializer(typeof(RemoteDecodeParam));
//            xmlSerializer.Serialize(memoryStream, remoteDecodeParam);
//            text = base64UrlEncode(memoryStream.ToArray());
//            memoryStream.Close();
//        }

//        string text2 = "http://127.0.0.1:58137/ProcessBitmap?param=";
//        if (isHObject)
//        {
//            text2 = "http://127.0.0.1:58137/Process?param=";
//        }

//        byte[] bytes;
//        try
//        {
//            MyWebClient myWebClient = new MyWebClient(webclienttimeout);
//            bytes = myWebClient.UploadData(text2 + text, requestdata);
//            requestdata = null;
//            myWebClient.Dispose();
//        }
//        catch (WebException)
//        {
//            return null;
//        }
//        catch
//        {
//            return null;
//        }

//        List<CodeInfo> result = null;
//        string text3 = Encoding.UTF8.GetString(bytes).Trim().Trim('"');
//        text3 = text3.Replace("\\/", "/");
//        try
//        {
//            byte[] buffer = Convert.FromBase64String(text3);
//            using (MemoryStream stream = new MemoryStream(buffer))
//            {
//                XmlSerializer xmlSerializer2 = new XmlSerializer(typeof(List<CodeInfo>));
//                result = (List<CodeInfo>)xmlSerializer2.Deserialize(stream);
//            }
//        }
//        catch
//        {
//            throw new Exception(text3);
//        }

//        return result;
//    }

//    private static string base64UrlEncode(byte[] input)
//    {
//        return Convert.ToBase64String(input).Replace('+', '-').Replace('/', '_');
//    }

//    private static void CheckAndRunServer()
//    {
//        lock (p)
//        {
//            Process[] processesByName = Process.GetProcessesByName("ScanCodeServer");
//            if (processesByName.Length != 0)
//            {
//                return;
//            }

//            string text = "ScanCodeServer\\ScanCodeServer.exe";
//            if (!File.Exists(text))
//            {
//                throw new Exception("找不到扫码服务器文件");
//            }

//            p.StartInfo = new ProcessStartInfo(text);
//            p.Start();
//            int num = 5;
//            while (num > 0)
//            {
//                num--;
//                Thread.Sleep(500);
//                MyWebClient myWebClient = new MyWebClient(webclienttimeout);
//                string text2 = myWebClient.DownloadString("http://127.0.0.1:58137/alive");
//                if (text2.Trim() == "\"1\"")
//                {
//                    return;
//                }
//            }

//            throw new Exception("扫码服务器打开失败");
//        }
//    }

//    [Serializable]
//    public struct RemoteDecodeParam
//    {
//        public string[] codeTypeList;

//        public int codeCount;

//        public int timeout;
//    }

//    public class MyWebClient : WebClient
//    {
//        private int _timeout;

//        public MyWebClient(int timeout)
//        {
//            _timeout = timeout;
//        }

//        protected override WebRequest GetWebRequest(Uri address)
//        {
//            WebRequest webRequest = base.GetWebRequest(address);
//            webRequest.Timeout = _timeout;
//            return webRequest;
//        }
//    }
//}