Camera.cs 4.9 KB
using Asa.Camera;
using CodeLibrary;
using OnlineStore;
using OnlineStore.Common;
using OnlineStore.DeviceLibrary;
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;

public class HIKCamera
{
    Thread camerathread;
    //static HIKCamera _camera;
    Asa.Camera.VisionLib camera;
    //public static HIKCamera Current
    //{
    //    get
    //    {
    //        if (_camera == null)
    //            _camera = new HIKCamera();
    //        return _camera;
    //    }
    //}

    public string Name = "";
    public event EventHandler<Bitmap> camera_event;
    public string DeviceName = "CameraA";
    public bool LoadCameraConfig(string CameraID, out string msg)
    {
        Name = CameraID;
        msg = "";
        string path = $".\\Config\\{CameraID}.json";
        if (!File.Exists(path))
        {
            msg = Name + "找不到监控相机配置文件";
            LogUtil.error(Name + "找不到监控相机配置文件" + path);
            return false;
        }
        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);
            DeviceName = m.Groups[1].Value;
            camera = new VisionLib(path, true, "HIK.IPCamera");
        }
        catch (Exception e)
        {
            msg = Name +  "加载监控相机配置文件失败:" + e.ToString();
            LogUtil.error(Name + "加载监控相机配置文件失败:" + e.ToString());
            return false;
        }
        for (int i = 0; i < 10; i++)
        {
            camera.Open(DeviceName);
            Thread.Sleep(100);
            Bitmap bmp = camera.GetImage(DeviceName);
            if (bmp != null)
            {
                return true;
            }
            camera.Close(DeviceName);
            Thread.Sleep(100);
        }
        msg = Name + "监控相机打开失败";
        LogUtil.error(Name + $"监控相机打开失败");
        return false;
        //camerathread = new Thread(new ThreadStart(startCamera));
        //camerathread.Start();
        //GC.KeepAlive(camerathread);

    }
    int errortimes = 0;
    bool camerathreadrun = true;
    public void stopCamera() {
        camerathreadrun = false;
    }
    void startCamera()
    {
        int errorsleeptime = 5000;
        camerathreadrun = true;
        while (camerathreadrun)
        {
            try
            {
                Bitmap bmp = camera.GetImage(DeviceName);
                if (bmp != null)
                {
                    errortimes = 0;
                    errorsleeptime = 5000;
                    camera_event?.Invoke(this, bmp);
                }
                else if (bmp == null && errortimes < 5)
                {
                    errortimes++;
                    LogUtil.error(Name + $"相机获取图像出错,{errortimes}");
                    Thread.Sleep(errorsleeptime);
                }
                else if (errortimes == 5)
                {
                    camera.Close(DeviceName);
                    Thread.Sleep(1000);
                    camera.Open(DeviceName);
                    errortimes=0;
                    errorsleeptime = errorsleeptime * 2;
                    LogUtil.error(Name + $"相机错误次数过多,重新打开,{errortimes}");
                }
                else if (errortimes == 6)
                {
                    LogUtil.error(Name + $"相机连接失败, 相机线程退出,{errortimes}");
                    break;
                }
                Thread.Sleep(1000 / 5);
            }
            catch
            {
                errortimes++;
            }
        }
        try
        {
            camera.Close(DeviceName);
        }
        catch { }
        camera.Dispose();
    }
    public Bitmap CameraGrabOne() {
        return camera.GetImage(DeviceName);
    }
    public void CameraGrabOne(string filename)
    {
        try
        {
            LogUtil.info(Name + "库位文件名:" + filename);
            Bitmap bmp = camera.GetImage(DeviceName);
            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());
        }
    }

}
public enum FixtureState
{
    FromIn,
    FromOut,
    FromInSide,
    FromOutSide,
    ToIn,
    ToOut,
    ToInSide,
    ToOutSide,
    ToFix,
}