Newer
Older
IRIS_REFACTOR / irisRefactor / IrisThread / SaveImageTh.cs
yxw on 26 Apr 2021 5 KB 20210426
using irisHelper;
using irisRefactor.IrisScoket;
using MvCamCtrl.NET;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Threading;

namespace irisRefactor.IrisThread
{
    public class SaveImageTh
    {

        private static SaveImageTh _saveImageTh = null;
        private SaveImageTh() { }
        public static SaveImageTh GetInstance()
        {
            if (_saveImageTh == null)
                _saveImageTh = new SaveImageTh();

            return _saveImageTh;
        }
        //图象采集回调函数
        //pData [out] 保存图像数据的缓存地址
        //pFrameInfo [out] 获取到的帧信息,包括宽、高、像素格式等
        //pUser [out] 用户数据
        public void SaveImage(IntPtr pData, ref MyCamera.MV_FRAME_OUT_INFO pFrameInfo, IntPtr pUser)
        {
            try
            {
                //1.取出采集到的图像
                int ylength = pFrameInfo.nWidth * pFrameInfo.nHeight;
                byte[] imgBytes = new byte[ylength];
                Marshal.Copy(pData, imgBytes, 0, ylength);
                Image image = Raw8BitByteArrayToImage(imgBytes, pFrameInfo.nWidth, pFrameInfo.nHeight);

                //2.对图像进行人眼搜索处理
                if (image != null && ProMemory.isWait == false && ProMemory.findingEyes && !ProMemory.isSyning)
                {
                    try
                    {
                        //将图片放入堆栈1
                        lock (ProMemory.irisConfig.Q1)
                        {
                            ProMemory.irisConfig.Q1.Push(imgBytes);
                        }
                    }
                    catch (Exception ee)
                    {
                        LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "errorerrorerror : 采集线程push q1 :" + ee);
                    }

                    if (ProMemory.irisConfig.Key_FindEyes == 0)
                    {
                        ProMemory.irisConfig.Key_FindEyes = 1;
                        ProMemory.irisConfig.EyeFinderThreadRun = true;
                        Thread m_EyeFinderThread = new Thread(FindEyesTh.GetInstance().FindEyes);
                        m_EyeFinderThread.Name = "FindEyes";
                        m_EyeFinderThread.Start();
                    }

                }

                //释放内存
                imgBytes = null;
                image = null;
                try
                {
                    lock (ProMemory.irisConfig.Q1)
                    {
                        if (ProMemory.irisConfig.Q1.Count >= 30)
                        {
                            ProMemory.irisConfig.Q1.Clear(); //手动控制堆栈容量                   
                        }
                    }
                }
                catch (Exception ee)
                {
                    LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "errorerrorerror : 采集线程clear q1 :" + ee);
                }
            }
            catch (Exception ex)
            {
                LogHelper.WriteLog(MethodBase.GetCurrentMethod().DeclaringType, "图像采集线程CatchError:" + ex);
            }
        }

        private Image Raw8BitByteArrayToImage(byte[] byteArray, int width, int height)
        {
            if ((byteArray == null) || (width <= 0) || (height <= 0))
                return null;

            Bitmap bmp = new Bitmap(width, height, PixelFormat.Format8bppIndexed);
            ColorPalette palette = bmp.Palette;

            for (int index = 0; index < palette.Entries.Length; index++)
                palette.Entries[index] = Color.FromArgb(index, index, index);

            bmp.Palette = palette;

            BitmapData bData = bmp.LockBits(new Rectangle(new Point(), bmp.Size),
                                            ImageLockMode.WriteOnly, PixelFormat.Format8bppIndexed);

            ///水平镜像处理
            byte temp;
            for (int i = 0; i < height; i++)
            {
                for (int j = 0; j < width / 2; j++)
                {
                    //以水平中轴线为对称轴,两边像素值交换
                    temp = byteArray[i * width + j];
                    byteArray[i * width + j] = byteArray[(i + 1) * width - 1 - j];
                    byteArray[(i + 1) * width - 1 - j] = temp;
                }

            }
            ///水平镜像处理
            
            // Copy the bytes to the bitmap object
            Marshal.Copy(byteArray, 0, bData.Scan0, byteArray.Length);

            bmp.UnlockBits(bData);

            return bmp;
        }
        
        private Bitmap RevPic(Bitmap mybm, int width, int height)
        {
            Bitmap bm = new Bitmap(width, height);//初始化一个记录经过处理后的图片对象
            int x, y, z;//x,y是循环次数,z是用来记录像素点的x坐标的变化的
            Color pixel;

            for (y = height - 1; y >= 0; y--)
            {
                for (x = width - 1, z = 0; x >= 0; x--)
                {
                    pixel = mybm.GetPixel(x, y);//获取当前像素的值
                    bm.SetPixel(z++, y, Color.FromArgb(pixel.R, pixel.G, pixel.B));//绘图
                }
            }

            return bm;//返回经过翻转后的图片
        }

    }
}