BoxBean_Camera.cs 4.7 KB
using Asa.Camera;
using OnlineStore.Common;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;

namespace OnlineStore.DeviceLibrary
{
    partial class BoxBean
    {
        Thread camerathread;
        Asa.Camera.VisionLib camera;
        
        public event EventHandler<Bitmap> camera_event;
        public string CameraDeviceName = "monitor1";
        bool loop = true;
        public void LoadCameraConfig(string id)
        {
            string path = @".\StoreConfig\box_" + id + "_Camera.json";
            if (!File.Exists(path))
            {
                LogUtil.error(Name + "找不到监控相机配置文件" + path);
                return;
            }
            try
            {
                var configtxt = File.ReadAllText(path);
                if (configtxt.IndexOf("Pwd") > 0)
                {
                    configtxt = configtxt.Replace("Pwd", "Password");
                    configtxt = configtxt.Replace("\"8000\"", "8000");
                    File.WriteAllText(path, configtxt);
                }
                var m = Regex.Match(configtxt, "name\".*?\"(.+)\"", RegexOptions.IgnoreCase);
                CameraDeviceName = m.Groups[1].Value;
                camera = new VisionLib(path,true, "HIK.IPCamera");
            }
            catch (Exception e)
            {
                LogUtil.error(Name + "加载监控相机配置文件失败:" + e.ToString());
                return;
            }
            camera.Open(CameraDeviceName);
            Bitmap bmp = camera.GetImage(CameraDeviceName);
            if (bmp == null)
            {
                LogUtil.error(Name + $"监控相机打开失败");
            }
            loop = true;
            camerathread = new Thread(new ThreadStart(startCamera));
            camerathread.Start();
            GC.KeepAlive(camerathread);

        }
        int errortimes = 0;
        void startCamera()
        {
            while (loop)
            {
                try
                {
                    Bitmap bmp = camera.GetImage(CameraDeviceName);
                    if (bmp != null)
                    {
                        errortimes = 0;
                        camera_event?.Invoke(this, bmp);
                    }
                    else if (bmp == null && errortimes < 5)
                    {
                        errortimes++;
                        LogUtil.error(Name + $"相机获取图像出错,{errortimes}");
                    }
                    else if (errortimes == 5)
                    {
                        camera.Close(CameraDeviceName);
                        Thread.Sleep(1000);
                        camera.Open(CameraDeviceName);
                        errortimes = 0;
                        LogUtil.error(Name + $"相机错误次数过多,重新打开,{errortimes}");
                        Thread.Sleep(5000);
                    }                    
                    Thread.Sleep(1000 / 7);
                }
                catch
                {
                    errortimes++;
                }
            }
            camera.Close(CameraDeviceName);
            camera.Dispose();
        }
        public void CameraClose()
        {
            LogUtil.info(Name + "线程退出");
            loop = false;
        }
        void CameraGrabOne(string filename)
        {
            try
            {
                LogUtil.info(Name + "库位文件名:" + filename);
                Bitmap bmp = camera.GetImage(CameraDeviceName);
                if (bmp != null)
                {
                    if (File.Exists(filename))
                        File.Delete(filename);

                    bmp.Save(filename, ImageFormat.Jpeg);
                    bmp.Dispose();
                }
            }
            catch (Exception e)
            {
                LogUtil.error(Name + e.ToString());
            }
        }
        string GetFixtureStateFilename(InOutParam inOutParam, FixtureState fixtureState) {
            string dir = $"\\image\\Fixture\\{inOutParam.moveType}\\BOX{ID}\\{inOutParam.PosID}\\";
            Directory.CreateDirectory(dir);
            string filename = $"{inOutParam.WareCode}@@{StoreManager.LastVisualRfid}@@{fixtureState}.jpg";
            foreach (var x in Path.GetInvalidFileNameChars())
            {
                filename = filename.Replace(x.ToString(), "");
            }
            return dir+filename;
        }
        enum FixtureState {
            In,
            Up,
            Down,
            Out,
            DoorIn,
            DoorBack
        }
    }
}