Exemplo n.º 1
0
        public static Int32 ConvertToMono8(object obj, IntPtr pInData, IntPtr pOutData, ushort nHeight, ushort nWidth, MyCamera.MvGvspPixelType nPixelType)
        {
            if (IntPtr.Zero == pInData || IntPtr.Zero == pOutData)
            {
                return(MyCamera.MV_E_PARAMETER);
            }
            if (obj == null)
            {
                return(-1);
            }

            int      nRet   = MyCamera.MV_OK;
            MyCamera device = obj as MyCamera;

            MyCamera.MV_PIXEL_CONVERT_PARAM stPixelConvertParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();

            stPixelConvertParam.pSrcData = pInData;//源数据
            if (IntPtr.Zero == stPixelConvertParam.pSrcData)
            {
                return(-1);
            }

            stPixelConvertParam.nWidth         = nWidth;     //图像宽度
            stPixelConvertParam.nHeight        = nHeight;    //图像高度
            stPixelConvertParam.enSrcPixelType = nPixelType; //源数据的格式
            stPixelConvertParam.nSrcDataLen    = (uint)(nWidth * nHeight * ((((uint)nPixelType) >> 16) & 0x00ff) >> 3);

            stPixelConvertParam.nDstBufferSize = (uint)(nWidth * nHeight * ((((uint)MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed) >> 16) & 0x00ff) >> 3);
            stPixelConvertParam.pDstBuffer     = pOutData;//转换后的数据
            stPixelConvertParam.enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
            stPixelConvertParam.nDstBufferSize = (uint)(nWidth * nHeight * 3);

            nRet = device.MV_CC_ConvertPixelType_NET(ref stPixelConvertParam);//格式转换
            if (MyCamera.MV_OK != nRet)
            {
                return(-1);
            }

            return(nRet);
        }
Exemplo n.º 2
0
        private static GrabInfo ConvertImage(MyCamera.MV_FRAME_OUT_INFO_EX frameInfo, IntPtr pData, MyCamera device)
        {
            uint channel = frameInfo.nFrameLen / frameInfo.nWidth / frameInfo.nHeight;

            if (channel == 1)
            {
                var data = new byte[frameInfo.nFrameLen];
                Marshal.Copy(pData, data, 0, (int)frameInfo.nFrameLen);
                return(new GrabInfo(EGrabResult.Success, frameInfo.nWidth, frameInfo.nHeight, 1, data));
            }
            else
            {
                var data = new byte[frameInfo.nWidth * frameInfo.nHeight * 3];

                var handle = GCHandle.Alloc(data, GCHandleType.Pinned);

                var stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
                stConverPixelParam.nWidth         = frameInfo.nWidth;
                stConverPixelParam.nHeight        = frameInfo.nHeight;
                stConverPixelParam.pSrcData       = pData;
                stConverPixelParam.nSrcDataLen    = frameInfo.nFrameLen;
                stConverPixelParam.enSrcPixelType = frameInfo.enPixelType;
                stConverPixelParam.enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed;
                stConverPixelParam.pDstBuffer     = handle.AddrOfPinnedObject();
                stConverPixelParam.nDstBufferSize = (uint)(frameInfo.nWidth * frameInfo.nHeight * 3);

                if (MyCamera.MV_OK != device.MV_CC_ConvertPixelType_NET(ref stConverPixelParam))
                {
                    handle.Free();
                    return(new GrabInfo(EGrabResult.Error));
                }

                handle.Free();

                return(new GrabInfo(EGrabResult.Success, frameInfo.nWidth, frameInfo.nHeight, 3, data));
            }
        }
Exemplo n.º 3
0
        private void ImageCallbackFunc(IntPtr pData, ref MyCamera.MV_FRAME_OUT_INFO_EX pFrameInfo, IntPtr pUser)
        {
            Console.WriteLine("Get one frame: Width[" + Convert.ToString(pFrameInfo.nWidth) + "] , Height[" + Convert.ToString(pFrameInfo.nHeight)
                              + "] , FrameNum[" + Convert.ToString(pFrameInfo.nFrameNum) + "]");


            MyCamera.MvGvspPixelType enDstPixelType;
            if (IsMonoData(pFrameInfo.enPixelType))
            {
                enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
            }
            else if (IsColorData(pFrameInfo.enPixelType))
            {
                enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed;
            }
            else
            {
                return;
            }


            byte[] m_pBufForSaveImage = new byte[3 * (pFrameInfo.nWidth * pFrameInfo.nHeight) + 2048];

            IntPtr pImage = Marshal.UnsafeAddrOfPinnedArrayElement(m_pBufForSaveImage, 0);

            MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
            stConverPixelParam.nWidth         = pFrameInfo.nWidth;
            stConverPixelParam.nHeight        = pFrameInfo.nHeight;
            stConverPixelParam.pSrcData       = pData;
            stConverPixelParam.nSrcDataLen    = pFrameInfo.nFrameLen;
            stConverPixelParam.enSrcPixelType = pFrameInfo.enPixelType;
            stConverPixelParam.enDstPixelType = enDstPixelType;
            stConverPixelParam.pDstBuffer     = pImage;
            stConverPixelParam.nDstBufferSize = (uint)(3 * (pFrameInfo.nWidth * pFrameInfo.nHeight) + 2048);
            nRet = getonecamera.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
            if (MyCamera.MV_OK != nRet)
            {
                return;
            }

            if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
            {
                //************************Mono8 转 Bitmap*******************************
                Bitmap bmp = new Bitmap(pFrameInfo.nWidth, pFrameInfo.nHeight, pFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, pImage);

                ColorPalette cp = bmp.Palette;
                // init palette
                for (int i = 0; i < 256; i++)
                {
                    cp.Entries[i] = Color.FromArgb(i, i, i);
                }
                // set palette back
                bmp.Palette = cp;

                Bitmap temp = (Bitmap)bmp.Clone();

                CallFunction(this.Name, bmp);

                GC.Collect();
            }
            else
            {
                //*********************RGB8 转 Bitmap**************************
                for (int i = 0; i < pFrameInfo.nHeight; i++)
                {
                    for (int j = 0; j < pFrameInfo.nWidth; j++)
                    {
                        byte chRed = m_pBufForSaveImage[i * pFrameInfo.nWidth * 3 + j * 3];
                        m_pBufForSaveImage[i * pFrameInfo.nWidth * 3 + j * 3]     = m_pBufForSaveImage[i * pFrameInfo.nWidth * 3 + j * 3 + 2];
                        m_pBufForSaveImage[i * pFrameInfo.nWidth * 3 + j * 3 + 2] = chRed;
                    }
                }
                try
                {
                    Bitmap bmp  = new Bitmap(pFrameInfo.nWidth, pFrameInfo.nHeight, pFrameInfo.nWidth * 3, PixelFormat.Format24bppRgb, pImage);
                    Bitmap temp = (Bitmap)bmp.Clone();

                    CallFunction(this.Name, bmp);

                    GC.Collect();
                }
                catch
                {
                }
            }
        }
Exemplo n.º 4
0
        private Bitmap ParseRawImageData(IntPtr pData, MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo)
        {
            int    nRet;
            UInt32 newBufferSize = 0;
            Bitmap output;

            MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();

            nRet = _myCamera.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
            if (MyCamera.MV_OK != nRet)
            {
                throw new InvalidOperationException("Can not get payload size");
            }

            newBufferSize = stParam.nCurValue;

            if (newBufferSize > _bufferSize)
            {
                _bufferSize = newBufferSize;
                _buffer     = new byte[_bufferSize];

                // ch:同时对保存图像的缓存做大小判断处理 | en:Determine the buffer size to save image
                // ch:BMP图片大小:width * height * 3 + 2048(预留BMP头大小) | en:BMP image size: width * height * 3 + 2048 (Reserved for BMP header)
                _buffSizeForSaveImage = _bufferSize * 3 + 2048;
                _bufForSaveImage      = new byte[_buffSizeForSaveImage];
            }

            MyCamera.MvGvspPixelType enDstPixelType;
            if (IsMonoData(stFrameInfo.enPixelType))
            {
                enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
            }
            else if (IsColorData(stFrameInfo.enPixelType))
            {
                enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed;
            }
            else
            {
                throw new NotSupportedException("Can not support such pixel type currently");
            }

            IntPtr pImage = Marshal.UnsafeAddrOfPinnedArrayElement(_bufForSaveImage, 0);

            MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM
            {
                nWidth         = stFrameInfo.nWidth,
                nHeight        = stFrameInfo.nHeight,
                pSrcData       = pData,
                nSrcDataLen    = stFrameInfo.nFrameLen,
                enSrcPixelType = stFrameInfo.enPixelType,
                enDstPixelType = enDstPixelType,
                pDstBuffer     = pImage,
                nDstBufferSize = _buffSizeForSaveImage
            };

            nRet = _myCamera.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
            if (MyCamera.MV_OK != nRet)
            {
                throw new InvalidOperationException("Unable to convert pixel type");
            }

            if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
            {
                //************************Mono8 转 Bitmap*******************************
                output = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 1,
                                    PixelFormat.Format8bppIndexed, pImage);

                ColorPalette cp = output.Palette;
                // init palette
                for (int i = 0; i < 256; i++)
                {
                    cp.Entries[i] = Color.FromArgb(i, i, i);
                }

                output.Palette = cp;
            }
            else
            {
                //*********************RGB8 转 Bitmap**************************
                for (int i = 0; i < stFrameInfo.nHeight; i++)
                {
                    for (int j = 0; j < stFrameInfo.nWidth; j++)
                    {
                        byte chRed = _bufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3];
                        _bufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3] =
                            _bufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3 + 2];
                        _bufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3 + 2] = chRed;
                    }
                }

                output = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 3,
                                    PixelFormat.Format24bppRgb, pImage);
            }

            return(output);
        }
Exemplo n.º 5
0
        private void bnSaveBmp_Click(object sender, EventArgs e)
        {
            if (false == m_bGrabbing)
            {
                ShowErrorMsg("Not Start Grabbing", 0);
                return;
            }

            if (RemoveCustomPixelFormats(m_stFrameInfo.enPixelType))
            {
                ShowErrorMsg("Not Support!", 0);
                return;
            }

            IntPtr pTemp = IntPtr.Zero;

            MyCamera.MvGvspPixelType enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Undefined;
            if (m_stFrameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8 || m_stFrameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_BGR8_Packed)
            {
                pTemp          = m_BufForDriver;
                enDstPixelType = m_stFrameInfo.enPixelType;
            }
            else
            {
                UInt32 nSaveImageNeedSize = 0;
                MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();

                lock (BufForDriverLock)
                {
                    if (m_stFrameInfo.nFrameLen == 0)
                    {
                        ShowErrorMsg("Save Bmp Fail!", 0);
                        return;
                    }

                    if (IsMonoData(m_stFrameInfo.enPixelType))
                    {
                        enDstPixelType     = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
                        nSaveImageNeedSize = (uint)m_stFrameInfo.nWidth * m_stFrameInfo.nHeight;
                    }
                    else if (IsColorData(m_stFrameInfo.enPixelType))
                    {
                        enDstPixelType     = MyCamera.MvGvspPixelType.PixelType_Gvsp_BGR8_Packed;
                        nSaveImageNeedSize = (uint)m_stFrameInfo.nWidth * m_stFrameInfo.nHeight * 3;
                    }
                    else
                    {
                        ShowErrorMsg("No such pixel type!", 0);
                        return;
                    }

                    if (m_nBufSizeForSaveImage < nSaveImageNeedSize)
                    {
                        if (m_BufForSaveImage != IntPtr.Zero)
                        {
                            Marshal.Release(m_BufForSaveImage);
                        }
                        m_nBufSizeForSaveImage = nSaveImageNeedSize;
                        m_BufForSaveImage      = Marshal.AllocHGlobal((Int32)m_nBufSizeForSaveImage);
                    }

                    stConverPixelParam.nWidth         = m_stFrameInfo.nWidth;
                    stConverPixelParam.nHeight        = m_stFrameInfo.nHeight;
                    stConverPixelParam.pSrcData       = m_BufForDriver;
                    stConverPixelParam.nSrcDataLen    = m_stFrameInfo.nFrameLen;
                    stConverPixelParam.enSrcPixelType = m_stFrameInfo.enPixelType;
                    stConverPixelParam.enDstPixelType = enDstPixelType;
                    stConverPixelParam.pDstBuffer     = m_BufForSaveImage;
                    stConverPixelParam.nDstBufferSize = m_nBufSizeForSaveImage;
                    int nRet = m_MyCamera.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
                    if (MyCamera.MV_OK != nRet)
                    {
                        ShowErrorMsg("Convert Pixel Type Fail!", nRet);
                        return;
                    }
                    pTemp = m_BufForSaveImage;
                }
            }

            lock (BufForDriverLock)
            {
                if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
                {
                    //************************Mono8 转 Bitmap*******************************
                    Bitmap bmp = new Bitmap(m_stFrameInfo.nWidth, m_stFrameInfo.nHeight, m_stFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, pTemp);

                    ColorPalette cp = bmp.Palette;
                    // init palette
                    for (int i = 0; i < 256; i++)
                    {
                        cp.Entries[i] = Color.FromArgb(i, i, i);
                    }
                    // set palette back
                    bmp.Palette = cp;
                    bmp.Save("image.bmp", ImageFormat.Bmp);
                }
                else
                {
                    //*********************BGR8 转 Bitmap**************************
                    try
                    {
                        Bitmap bmp = new Bitmap(m_stFrameInfo.nWidth, m_stFrameInfo.nHeight, m_stFrameInfo.nWidth * 3, PixelFormat.Format24bppRgb, pTemp);
                        bmp.Save("image.bmp", ImageFormat.Bmp);
                    }
                    catch
                    {
                        ShowErrorMsg("Write File Fail!", 0);
                    }
                }
            }

            ShowErrorMsg("Save Succeed!", 0);
        }
Exemplo n.º 6
0
        /// <summary>生成一个对应的Halcon图像对象</summary>
        public int GenHalcon(out object hoImg)
        {
            hoImg = null;
            HObject hImg = null;

            MyCamera.MvGvspPixelType dstPixType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;

            if (IsMono)
            {
                if (_frameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
                {
                    try
                    {
                        HOperatorSet.GenImage1(out hImg, "byte", _frameInfo.nWidth, _frameInfo.nHeight, Marshal.UnsafeAddrOfPinnedArrayElement(_dataBytes, 0));
                        hoImg = hImg;
                        return((int)ErrorDef.Success);
                    }
                    catch
                    {
                        return((int)ErrorDef.MemoryExcp);
                    }
                }
            }
            else if (IsColor)
            {
                if (_frameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Planar)
                {
                    try
                    {
                        IntPtr pDataR = Marshal.UnsafeAddrOfPinnedArrayElement(_dataBytes, 0);
                        IntPtr pDataG = pDataR + _frameInfo.nWidth * _frameInfo.nHeight;
                        IntPtr pDataB = pDataG + _frameInfo.nWidth * _frameInfo.nHeight;
                        HOperatorSet.GenImage3(out hImg, "byte", _frameInfo.nWidth, _frameInfo.nHeight, pDataR, pDataG, pDataB);
                        hoImg = hImg;
                        return((int)ErrorDef.Success);
                    }
                    catch
                    {
                        return((int)ErrorDef.MemoryExcp);
                    }
                }

                dstPixType = MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Planar; //将图像转化为RGB平面格式
            }
            else
            {
                return((int)ErrorDef.PixelFormatError);
            }

            int    tmpBuffLen = (int)(_frameInfo.nWidth * _frameInfo.nHeight * ((((uint)dstPixType) >> 16) & 0x00ff) >> 3);
            IntPtr pTmp       = Marshal.AllocHGlobal(tmpBuffLen);

            MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
            stConverPixelParam.nWidth         = _frameInfo.nWidth;
            stConverPixelParam.nHeight        = _frameInfo.nHeight;
            stConverPixelParam.pSrcData       = Marshal.UnsafeAddrOfPinnedArrayElement(_dataBytes, 0);
            stConverPixelParam.nSrcDataLen    = (uint)(_frameInfo.nWidth * _frameInfo.nHeight * ((((uint)_frameInfo.enPixelType) >> 16) & 0x00ff) >> 3);
            stConverPixelParam.enSrcPixelType = _frameInfo.enPixelType;
            stConverPixelParam.enDstPixelType = dstPixType;
            stConverPixelParam.pDstBuffer     = pTmp;
            stConverPixelParam.nDstBufferSize = (uint)tmpBuffLen;
            int err = _cmr.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);

            if (MyCamera.MV_OK != err)
            {
                Marshal.FreeHGlobal(pTmp);
                return((int)ErrorDef.InvokeFailed);
            }
            try
            {
                if (dstPixType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
                {
                    HOperatorSet.GenImage1(out hImg, "byte", _frameInfo.nWidth, _frameInfo.nHeight, pTmp);
                }
                else
                {
                    IntPtr pDataR = pTmp;
                    IntPtr pDataG = pDataR + _frameInfo.nWidth * _frameInfo.nHeight;
                    IntPtr pDataB = pDataG + _frameInfo.nWidth * _frameInfo.nHeight;
                    HOperatorSet.GenImage3(out hImg, "byte", _frameInfo.nWidth, _frameInfo.nHeight,
                                           pDataR, pDataG, pDataB);
                }
                Marshal.FreeHGlobal(pTmp);
                hoImg = hImg;
                return((int)ErrorDef.Success);
            }
            catch
            {
                Marshal.FreeHGlobal(pTmp);
                return((int)ErrorDef.MemoryExcp);
            }
        }
Exemplo n.º 7
0
        /// <summary>生成一个对应的bmp图像对象</summary>
        public int GenBmp(out Bitmap bmp)
        {
            bmp = null;
            IntPtr pBmpData = IntPtr.Zero;

            //IntPtr pImage = Marshal.UnsafeAddrOfPinnedArrayElement(_dataBytes, 0);
            MyCamera.MvGvspPixelType dstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8; //将要生成的BMP对象的图像格式
            int nBmpLen = (int)(_frameInfo.nWidth * _frameInfo.nHeight * ((((uint)MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8) >> 16) & 0x00ff) >> 3);

            if (IsMono)
            {
                pBmpData = Marshal.AllocHGlobal(nBmpLen);
            }
            else if (IsColor)
            {
                dstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_BGR8_Packed; //RGB24
                nBmpLen      = (int)(_frameInfo.nWidth * _frameInfo.nHeight * ((((uint)MyCamera.MvGvspPixelType.PixelType_Gvsp_BGR8_Packed) >> 16) & 0x00ff) >> 3);
                pBmpData     = Marshal.AllocHGlobal(nBmpLen);
            }
            else
            {
                return((int)ErrorDef.PixelFormatError);
            }

            MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
            stConverPixelParam.nWidth         = _frameInfo.nWidth;
            stConverPixelParam.nHeight        = _frameInfo.nHeight;
            stConverPixelParam.pSrcData       = Marshal.UnsafeAddrOfPinnedArrayElement(_dataBytes, 0);
            stConverPixelParam.nSrcDataLen    = (uint)(_frameInfo.nWidth * _frameInfo.nHeight * ((((uint)_frameInfo.enPixelType) >> 16) & 0x00ff) >> 3);
            stConverPixelParam.enSrcPixelType = _frameInfo.enPixelType;
            stConverPixelParam.enDstPixelType = dstPixelType;
            stConverPixelParam.pDstBuffer     = pBmpData;
            stConverPixelParam.nDstBufferSize = (uint)nBmpLen;
            int err = _cmr.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);

            if (MyCamera.MV_OK != err)
            {
                Marshal.FreeHGlobal(pBmpData);
                return((int)ErrorDef.InvokeFailed);
            }

            if (dstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
            {
                Bitmap bmpTmp = new Bitmap(_frameInfo.nWidth, _frameInfo.nHeight, _frameInfo.nWidth, PixelFormat.Format8bppIndexed, pBmpData);

                ColorPalette cp = bmpTmp.Palette;
                // init palette
                for (int i = 0; i < 256; i++)
                {
                    cp.Entries[i] = Color.FromArgb(i, i, i);
                }
                // set palette back
                bmpTmp.Palette = cp;
                bmp            = new Bitmap(bmpTmp);
                bmpTmp.Dispose();
                Marshal.FreeHGlobal(pBmpData);
                return((int)ErrorDef.Success);
            }
            ///将图像转化为RGB24
            try
            {
                bmp = new Bitmap(_frameInfo.nWidth, _frameInfo.nHeight, _frameInfo.nWidth * 3, PixelFormat.Format24bppRgb, pBmpData);
            }
            catch
            {
                Marshal.FreeHGlobal(pBmpData);
                return((int)ErrorDef.MemoryExcp);
            }
            return((int)ErrorDef.Success);
        }
Exemplo n.º 8
0
        public bool Capture(string sUserDefineName, out Bitmap outImage)
        {
            outImage = null;

            if (!m_bGrabbing)
            {
                StrErrorMsg = "相机" + sUserDefineName + "未开始抓取图片";
                return(false);
            }

            int nRet;
            var keys = m_listUserDefinedName.Where(q => q.Value == sUserDefineName).Select(q => q.Key).ToList <int>();  //get all keys

            if (keys.Count != 1)
            {
                StrErrorMsg = "相机" + sUserDefineName + "未连接";
                return(false);
            }
            int nIndex = keys[0];


            if (IsTriggerMode)
            {
                if (DoSoftTriggerSpecify(sUserDefineName))
                {
                    m_dicImageCallBackSignal[nIndex].WaitOne();
                }
                else
                {
                    return(false);
                }
            }

            IntPtr pImageBuf     = IntPtr.Zero;
            int    nImageBufSize = 0;
            IntPtr pTemp         = IntPtr.Zero;

            lock (m_dicSaveImge[nIndex].Lock)
            {
                if (IsColorPixelFormat(m_stFrameInfo[nIndex].enPixelType))
                {
                    if (m_stFrameInfo[nIndex].enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed)
                    {
                        pTemp = m_dicSaveImge[nIndex].Buf;
                    }
                    else
                    {
                        if (IntPtr.Zero == pImageBuf || nImageBufSize < (m_stFrameInfo[nIndex].nWidth * m_stFrameInfo[nIndex].nHeight * 3))
                        {
                            if (pImageBuf != IntPtr.Zero)
                            {
                                Marshal.FreeHGlobal(pImageBuf);
                                pImageBuf = IntPtr.Zero;
                            }

                            pImageBuf = Marshal.AllocHGlobal((int)m_stFrameInfo[nIndex].nWidth * m_stFrameInfo[nIndex].nHeight * 3);
                            if (IntPtr.Zero == pImageBuf)
                            {
                                StrErrorMsg = "相机" + sUserDefineName + "图片指向为空";
                                return(false);
                            }
                            nImageBufSize = m_stFrameInfo[nIndex].nWidth * m_stFrameInfo[nIndex].nHeight * 3;
                        }

                        MyCamera.MV_PIXEL_CONVERT_PARAM stPixelConvertParam = new MyCamera.MV_PIXEL_CONVERT_PARAM
                        {
                            pSrcData       = m_dicSaveImge[nIndex].Buf,         //源数据
                            nWidth         = m_stFrameInfo[nIndex].nWidth,      //图像宽度
                            nHeight        = m_stFrameInfo[nIndex].nHeight,     //图像高度
                            enSrcPixelType = m_stFrameInfo[nIndex].enPixelType, //源数据的格式
                            nSrcDataLen    = m_stFrameInfo[nIndex].nFrameLen,

                            nDstBufferSize = (uint)nImageBufSize,
                            pDstBuffer     = pImageBuf,//转换后的数据
                            enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed
                        };
                        nRet = m_dicMyCamera[nIndex].MV_CC_ConvertPixelType_NET(ref stPixelConvertParam);//格式转换
                        if (MyCamera.MV_OK != nRet)
                        {
                            StrErrorMsg = "相机" + sUserDefineName + "图片像素转换失败";
                            return(false);
                        }
                        pTemp = pImageBuf;
                    }

                    try
                    {
                        outImage = new Bitmap(m_stFrameInfo[nIndex].nWidth, (HTuple)m_stFrameInfo[nIndex].nHeight, m_stFrameInfo[nIndex].nWidth * 3, PixelFormat.Format24bppRgb, pTemp);
                    }
                    catch (Exception ex)
                    {
                        StrErrorMsg = "相机" + sUserDefineName + "数据转Bitmap失败" + ex.Message;
                        return(false);
                    }
                }
                else if (IsMonoPixelFormat(m_stFrameInfo[nIndex].enPixelType))
                {
                    if (m_stFrameInfo[nIndex].enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
                    {
                        pTemp = m_dicSaveImge[nIndex].Buf;
                    }
                    else
                    {
                        if (IntPtr.Zero == pImageBuf || nImageBufSize < (m_stFrameInfo[nIndex].nWidth * m_stFrameInfo[nIndex].nHeight))
                        {
                            if (pImageBuf != IntPtr.Zero)
                            {
                                Marshal.FreeHGlobal(pImageBuf);
                                pImageBuf = IntPtr.Zero;
                            }

                            pImageBuf = Marshal.AllocHGlobal((int)m_stFrameInfo[nIndex].nWidth * m_stFrameInfo[nIndex].nHeight);
                            if (IntPtr.Zero == pImageBuf)
                            {
                                StrErrorMsg = "相机" + sUserDefineName + "图片指向为空";
                                return(false);
                            }
                            nImageBufSize = m_stFrameInfo[nIndex].nWidth * m_stFrameInfo[nIndex].nHeight;
                        }

                        MyCamera.MV_PIXEL_CONVERT_PARAM stPixelConvertParam = new MyCamera.MV_PIXEL_CONVERT_PARAM
                        {
                            pSrcData       = m_dicSaveImge[nIndex].Buf,         //源数据
                            nWidth         = m_stFrameInfo[nIndex].nWidth,      //图像宽度
                            nHeight        = m_stFrameInfo[nIndex].nHeight,     //图像高度
                            enSrcPixelType = m_stFrameInfo[nIndex].enPixelType, //源数据的格式
                            nSrcDataLen    = m_stFrameInfo[nIndex].nFrameLen,

                            nDstBufferSize = (uint)nImageBufSize,
                            pDstBuffer     = pImageBuf,//转换后的数据
                            enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8
                        };
                        nRet = m_dicMyCamera[nIndex].MV_CC_ConvertPixelType_NET(ref stPixelConvertParam);//格式转换
                        if (MyCamera.MV_OK != nRet)
                        {
                            StrErrorMsg = "相机" + sUserDefineName + "图片像素转换失败";
                            return(false);
                        }
                        pTemp = pImageBuf;
                    }
                    try
                    {
                        outImage = new Bitmap(m_stFrameInfo[nIndex].nWidth, (HTuple)m_stFrameInfo[nIndex].nHeight, m_stFrameInfo[nIndex].nWidth, PixelFormat.Format8bppIndexed, pTemp);

                        ColorPalette cp = outImage.Palette;

                        for (int i = 0; i < 256; i++)
                        {
                            cp.Entries[i] = Color.FromArgb(i, i, i);
                        }

                        outImage.Palette = cp;
                    }
                    catch (Exception ex)
                    {
                        StrErrorMsg = "相机" + sUserDefineName + "数据转Bitmap失败" + ex.Message;
                        return(false);
                    }
                }
                if (pImageBuf != IntPtr.Zero)
                {
                    Marshal.FreeHGlobal(pImageBuf);
                    pImageBuf = IntPtr.Zero;
                }
            }
            return(true);
        }
Exemplo n.º 9
0
        private void HardCapture(IntPtr pData, ref MyCamera.MV_FRAME_OUT_INFO_EX pFrameInfo, IntPtr pUser)
        {
            int     nIndex = (int)pUser;
            int     nRet;
            IntPtr  pImageBuf     = IntPtr.Zero;
            int     nImageBufSize = 0;
            IntPtr  pTemp         = IntPtr.Zero;
            HObject outImage      = null;

            if (IsColorPixelFormat(pFrameInfo.enPixelType))
            {
                if (pFrameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed)
                {
                    pTemp = pData;
                }
                else
                {
                    if (IntPtr.Zero == pImageBuf || nImageBufSize < (pFrameInfo.nWidth * pFrameInfo.nHeight * 3))
                    {
                        if (pImageBuf != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(pImageBuf);
                            pImageBuf = IntPtr.Zero;
                        }

                        pImageBuf = Marshal.AllocHGlobal((int)pFrameInfo.nWidth * pFrameInfo.nHeight * 3);
                        if (IntPtr.Zero == pImageBuf)
                        {
                            return;
                        }
                        nImageBufSize = pFrameInfo.nWidth * pFrameInfo.nHeight * 3;
                    }

                    MyCamera.MV_PIXEL_CONVERT_PARAM stPixelConvertParam = new MyCamera.MV_PIXEL_CONVERT_PARAM
                    {
                        pSrcData       = pData,                  //源数据
                        nWidth         = pFrameInfo.nWidth,      //图像宽度
                        nHeight        = pFrameInfo.nHeight,     //图像高度
                        enSrcPixelType = pFrameInfo.enPixelType, //源数据的格式
                        nSrcDataLen    = pFrameInfo.nFrameLen,

                        nDstBufferSize = (uint)nImageBufSize,
                        pDstBuffer     = pImageBuf,//转换后的数据
                        enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed
                    };
                    nRet = m_dicMyCamera[nIndex].MV_CC_ConvertPixelType_NET(ref stPixelConvertParam);//格式转换
                    if (MyCamera.MV_OK != nRet)
                    {
                        return;
                    }
                    pTemp = pImageBuf;
                }

                try
                {
                    HOperatorSet.GenImageInterleaved(out outImage, (HTuple)pTemp, (HTuple)"rgb", (HTuple)pFrameInfo.nWidth, (HTuple)pFrameInfo.nHeight, -1, "byte", 0, 0, 0, 0, -1, 0);
                }
                catch (Exception ex)
                {
                    return;
                }
            }
            else if (IsMonoPixelFormat(pFrameInfo.enPixelType))
            {
                if (pFrameInfo.enPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
                {
                    pTemp = pData;
                }
                else
                {
                    if (IntPtr.Zero == pImageBuf || nImageBufSize < (pFrameInfo.nWidth * pFrameInfo.nHeight))
                    {
                        if (pImageBuf != IntPtr.Zero)
                        {
                            Marshal.FreeHGlobal(pImageBuf);
                            pImageBuf = IntPtr.Zero;
                        }

                        pImageBuf = Marshal.AllocHGlobal((int)pFrameInfo.nWidth * pFrameInfo.nHeight);
                        if (IntPtr.Zero == pImageBuf)
                        {
                            return;
                        }
                        nImageBufSize = pFrameInfo.nWidth * pFrameInfo.nHeight;
                    }

                    MyCamera.MV_PIXEL_CONVERT_PARAM stPixelConvertParam = new MyCamera.MV_PIXEL_CONVERT_PARAM
                    {
                        pSrcData       = pData,                  //源数据
                        nWidth         = pFrameInfo.nWidth,      //图像宽度
                        nHeight        = pFrameInfo.nHeight,     //图像高度
                        enSrcPixelType = pFrameInfo.enPixelType, //源数据的格式
                        nSrcDataLen    = pFrameInfo.nFrameLen,

                        nDstBufferSize = (uint)nImageBufSize,
                        pDstBuffer     = pImageBuf,//转换后的数据
                        enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8
                    };
                    nRet = m_dicMyCamera[nIndex].MV_CC_ConvertPixelType_NET(ref stPixelConvertParam);//格式转换
                    if (MyCamera.MV_OK != nRet)
                    {
                        return;
                    }
                    pTemp = pImageBuf;
                }
                try
                {
                    HOperatorSet.GenImage1Extern(out outImage, "byte", pFrameInfo.nWidth, pFrameInfo.nHeight, pTemp, IntPtr.Zero);
                }
                catch (Exception ex)
                {
                    return;
                }
            }
            if (pImageBuf != IntPtr.Zero)
            {
                Marshal.FreeHGlobal(pImageBuf);
                pImageBuf = IntPtr.Zero;
            }

            HardTriggerEvent?.Invoke(m_listUserDefinedName[nIndex], outImage);
        }
Exemplo n.º 10
0
        public Bitmap GetFrame()
        {
            int nRet;

            bmp = null;
            MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
            nRet = pCamera.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
            if (MyCamera.MV_OK != nRet)
            {
                return(null);
            }
            nPayloadSize = stParam.nCurValue;
            if (nPayloadSize > m_nBufSizeForDriver)
            {
                m_nBufSizeForDriver = nPayloadSize;
                m_pBufForDriver     = new byte[m_nBufSizeForDriver];

                // Determine the buffer size to save image
                // BMP image size: width * height * 3 + 2048 (Reserved for BMP header)
                m_nBufSizeForSaveImage = m_nBufSizeForDriver * 3 + 2048;
                m_pBufForSaveImage     = new byte[m_nBufSizeForSaveImage];
            }

            IntPtr pData = Marshal.UnsafeAddrOfPinnedArrayElement(m_pBufForDriver, 0);

            MyCamera.MV_FRAME_OUT_INFO_EX stFrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();
            //Get one frame timeout, timeout is 1 sec
            nRet = pCamera.MV_CC_GetOneFrameTimeout_NET(pData, m_nBufSizeForDriver, ref stFrameInfo, 1000);
            if (MyCamera.MV_OK != nRet)
            {
                return(null);
            }

            MyCamera.MvGvspPixelType enDstPixelType;
            if (IsMonoData(stFrameInfo.enPixelType))
            {
                enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
            }
            else if (IsColorData(stFrameInfo.enPixelType))
            {
                enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed;
            }
            else
            {
                return(null);
            }

            IntPtr pImage = Marshal.UnsafeAddrOfPinnedArrayElement(m_pBufForSaveImage, 0);

            MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
            stConverPixelParam.nWidth         = stFrameInfo.nWidth;
            stConverPixelParam.nHeight        = stFrameInfo.nHeight;
            stConverPixelParam.pSrcData       = pData;
            stConverPixelParam.nSrcDataLen    = stFrameInfo.nFrameLen;
            stConverPixelParam.enSrcPixelType = stFrameInfo.enPixelType;
            stConverPixelParam.enDstPixelType = enDstPixelType;
            stConverPixelParam.pDstBuffer     = pImage;
            stConverPixelParam.nDstBufferSize = m_nBufSizeForSaveImage;
            nRet = pCamera.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
            if (MyCamera.MV_OK != nRet)
            {
                return(null);
            }

            if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
            {
                //************************Mono8 转 Bitmap*******************************
                bmp = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 1, PixelFormat.Format8bppIndexed, pImage);

                ColorPalette cp = bmp.Palette;
                // init palette
                for (int i = 0; i < 256; i++)
                {
                    cp.Entries[i] = Color.FromArgb(i, i, i);
                }
                // set palette back
                bmp.Palette = cp;
            }
            else
            {
                //*********************RGB8 转 Bitmap**************************
                for (int i = 0; i < stFrameInfo.nHeight; i++)
                {
                    for (int j = 0; j < stFrameInfo.nWidth; j++)
                    {
                        byte chRed = m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3];
                        m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3]     = m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3 + 2];
                        m_pBufForSaveImage[i * stFrameInfo.nWidth * 3 + j * 3 + 2] = chRed;
                    }
                }
                try
                {
                    bmp = new Bitmap(stFrameInfo.nWidth, stFrameInfo.nHeight, stFrameInfo.nWidth * 3, PixelFormat.Format24bppRgb, pImage);
                }
                catch
                {
                }
            }
            return(bmp);
        }
Exemplo n.º 11
0
        static void Main(string[] args)
        {
            int      nRet   = MyCamera.MV_OK;
            MyCamera device = new MyCamera();

            do
            {
                // ch:枚举设备 | en:Enum deivce
                MyCamera.MV_CC_DEVICE_INFO_LIST stDevList = new MyCamera.MV_CC_DEVICE_INFO_LIST();
                nRet = MyCamera.MV_CC_EnumDevices_NET(MyCamera.MV_GIGE_DEVICE | MyCamera.MV_USB_DEVICE, ref stDevList);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Enum device failed:{0:x8}", nRet);
                    break;
                }
                Console.WriteLine("Enum device count :{0} ", stDevList.nDeviceNum);
                if (0 == stDevList.nDeviceNum)
                {
                    break;
                }

                MyCamera.MV_CC_DEVICE_INFO stDevInfo;

                // ch:打印设备信息 | en:Print device info
                for (Int32 i = 0; i < stDevList.nDeviceNum; i++)
                {
                    stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[i], typeof(MyCamera.MV_CC_DEVICE_INFO));

                    if (MyCamera.MV_GIGE_DEVICE == stDevInfo.nTLayerType)
                    {
                        MyCamera.MV_GIGE_DEVICE_INFO stGigEDeviceInfo = (MyCamera.MV_GIGE_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stGigEInfo, typeof(MyCamera.MV_GIGE_DEVICE_INFO));
                        uint nIp1 = ((stGigEDeviceInfo.nCurrentIp & 0xff000000) >> 24);
                        uint nIp2 = ((stGigEDeviceInfo.nCurrentIp & 0x00ff0000) >> 16);
                        uint nIp3 = ((stGigEDeviceInfo.nCurrentIp & 0x0000ff00) >> 8);
                        uint nIp4 = (stGigEDeviceInfo.nCurrentIp & 0x000000ff);
                        Console.WriteLine("\n" + i.ToString() + ": [GigE] User Define Name : " + stGigEDeviceInfo.chUserDefinedName);
                        Console.WriteLine("device IP :" + nIp1 + "." + nIp2 + "." + nIp3 + "." + nIp4);
                    }
                    else if (MyCamera.MV_USB_DEVICE == stDevInfo.nTLayerType)
                    {
                        MyCamera.MV_USB3_DEVICE_INFO stUsb3DeviceInfo = (MyCamera.MV_USB3_DEVICE_INFO)MyCamera.ByteToStruct(stDevInfo.SpecialInfo.stUsb3VInfo, typeof(MyCamera.MV_USB3_DEVICE_INFO));
                        Console.WriteLine("\n" + i.ToString() + ": [U3V] User Define Name : " + stUsb3DeviceInfo.chUserDefinedName);
                        Console.WriteLine("\n Serial Number : " + stUsb3DeviceInfo.chSerialNumber);
                        Console.WriteLine("\n Device Number : " + stUsb3DeviceInfo.nDeviceNumber);
                    }
                }

                Int32 nDevIndex = 0;
                Console.Write("\nPlease input index (0 -- {0:d}) : ", stDevList.nDeviceNum - 1);
                try
                {
                    nDevIndex = Convert.ToInt32(Console.ReadLine());
                }
                catch
                {
                    Console.Write("Invalid Input!\n");
                    break;
                }

                if (nDevIndex > stDevList.nDeviceNum - 1 || nDevIndex < 0)
                {
                    Console.Write("Input Error!\n");
                    break;
                }
                stDevInfo = (MyCamera.MV_CC_DEVICE_INFO)Marshal.PtrToStructure(stDevList.pDeviceInfo[nDevIndex], typeof(MyCamera.MV_CC_DEVICE_INFO));

                // ch:创建设备 | en: Create device
                nRet = device.MV_CC_CreateDevice_NET(ref stDevInfo);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Create device failed:{0:x8}", nRet);
                    break;
                }

                // ch:打开设备 | en:Open device
                nRet = device.MV_CC_OpenDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Open device failed:{0:x8}", nRet);
                    break;
                }

                // ch:探测网络最佳包大小(只对GigE相机有效) | en:Detection network optimal package size(It only works for the GigE camera)
                if (stDevInfo.nTLayerType == MyCamera.MV_GIGE_DEVICE)
                {
                    int nPacketSize = device.MV_CC_GetOptimalPacketSize_NET();
                    if (nPacketSize > 0)
                    {
                        nRet = device.MV_CC_SetIntValue_NET("GevSCPSPacketSize", (uint)nPacketSize);
                        if (nRet != MyCamera.MV_OK)
                        {
                            Console.WriteLine("Warning: Set Packet Size failed {0:x8}", nRet);
                        }
                    }
                    else
                    {
                        Console.WriteLine("Warning: Get Packet Size failed {0:x8}", nPacketSize);
                    }
                }

                // ch:设置触发模式为off || en:set trigger mode as off
                nRet = device.MV_CC_SetEnumValue_NET("TriggerMode", 0);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Set TriggerMode failed!");
                    break;
                }

                // ch:开启抓图 || en: start grab image
                nRet = device.MV_CC_StartGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Start grabbing failed:{0:x8}", nRet);
                    break;
                }

                // ch:获取包大小 || en: Get Payload Size
                MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
                nRet = device.MV_CC_GetIntValue_NET("PayloadSize", ref stParam);
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Get PayloadSize failed:{0:x8}", nRet);
                    break;
                }
                UInt32 nPayloadSize = stParam.nCurValue;

                // 申请内存按照目前相机最大分辨率设计,实际根据一帧大小可更改
                IntPtr pBufForDriver    = Marshal.AllocHGlobal((int)nPayloadSize);
                IntPtr pBufForSaveImage = IntPtr.Zero;
                MyCamera.MV_FRAME_OUT_INFO_EX FrameInfo = new MyCamera.MV_FRAME_OUT_INFO_EX();

                nRet = device.MV_CC_GetOneFrameTimeout_NET(pBufForDriver, nPayloadSize, ref FrameInfo, 1000);
                // 获取一帧图像
                if (MyCamera.MV_OK == nRet)
                {
                    Console.WriteLine("Get One Frame:" + "Width[" + Convert.ToString(FrameInfo.nWidth) + "] , Height[" + Convert.ToString(FrameInfo.nHeight)
                                      + "] , FrameNum[" + Convert.ToString(FrameInfo.nFrameNum) + "]");
                    if (pBufForSaveImage == IntPtr.Zero)
                    {
                        pBufForSaveImage = Marshal.AllocHGlobal((int)(FrameInfo.nWidth * FrameInfo.nHeight * 3 + 2048));
                    }
                    MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
                    stConverPixelParam.nWidth         = FrameInfo.nWidth;
                    stConverPixelParam.nHeight        = FrameInfo.nHeight;
                    stConverPixelParam.pSrcData       = pBufForDriver;
                    stConverPixelParam.nSrcDataLen    = FrameInfo.nFrameLen;
                    stConverPixelParam.enSrcPixelType = FrameInfo.enPixelType;
                    stConverPixelParam.enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed;
                    stConverPixelParam.pDstBuffer     = pBufForSaveImage;
                    stConverPixelParam.nDstBufferSize = (uint)(FrameInfo.nWidth * FrameInfo.nHeight * 3 + 2048);

                    nRet = device.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
                    if (MyCamera.MV_OK != nRet)
                    {
                        Console.WriteLine("Convert pixel type Failed:{0:x8}", nRet);
                        break;
                    }

                    // ch:将图像数据保存到本地文件 | en:Save image data to local file
                    byte[] data = new byte[stConverPixelParam.nDstLen];
                    Marshal.Copy(pBufForSaveImage, data, 0, (int)stConverPixelParam.nDstLen);
                    FileStream pFile = null;
                    try
                    {
                        pFile = new FileStream("AfterConvert_RGB.raw", FileMode.Create);
                        pFile.Write(data, 0, data.Length);
                    }
                    catch
                    {
                        Console.WriteLine("保存失败");
                    }
                    finally
                    {
                        pFile.Close();
                    }
                }
                Marshal.FreeHGlobal(pBufForDriver);
                Marshal.FreeHGlobal(pBufForSaveImage);

                // ch:停止抓图 | en:Stop grabbing
                nRet = device.MV_CC_StopGrabbing_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Stop grabbing failed{0:x8}", nRet);
                    break;
                }

                // ch:关闭设备 | en:Close device
                nRet = device.MV_CC_CloseDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Close device failed{0:x8}", nRet);
                    break;
                }

                // ch:销毁设备 | en:Destroy device
                nRet = device.MV_CC_DestroyDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Destroy device failed:{0:x8}", nRet);
                    break;
                }
            } while (false);

            if (MyCamera.MV_OK != nRet)
            {
                // ch:销毁设备 | en:Destroy device
                nRet = device.MV_CC_DestroyDevice_NET();
                if (MyCamera.MV_OK != nRet)
                {
                    Console.WriteLine("Destroy device failed:{0:x8}", nRet);
                }
            }

            Console.WriteLine("Press enter to exit");
            Console.ReadKey();
        }
Exemplo n.º 12
0
        /// <summary>
        /// 图像回调函数
        /// </summary>
        /// <param name="pData"></param>
        /// <param name="pFrameInfo"></param>
        /// <param name="pUser"></param>
        private void ImageCallbackFunc(IntPtr pData, ref MyCamera.MV_FRAME_OUT_INFO_EX pFrameInfo, IntPtr pUser)
        {
            //图像数据数组
            //UInt32 m_nBufSizeForDriver = 4096 * 3000;
            //byte[] m_pBufforDriver = new byte[4096 * 3000];
            //UInt32 m_nBufSizeForSaveImage = 4096 * 3000 * 3 + 3000;
            //byte[] m_pBufferForSaveImage = new byte[4096 * 3000 * 3 + 3000];
            try
            {
                grabTime = stopWatch.ElapsedMilliseconds;
                MyCamera.MvGvspPixelType enDstPixelType;
                if (IsMonoData(pFrameInfo.enPixelType))
                {
                    enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8;
                }
                else if (IsColorData(pFrameInfo.enPixelType))
                {
                    enDstPixelType = MyCamera.MvGvspPixelType.PixelType_Gvsp_RGB8_Packed;
                }
                else
                {
                    return;
                }
                //UInt32 nPayloadSize = 0;
                //MyCamera.MVCC_INTVALUE stParam = new MyCamera.MVCC_INTVALUE();
                //int nRet = camera.MV_CC_GetIntValue_NET("PayloadSize",ref stParam);
                //if (MyCamera.MV_OK!=nRet)
                //{
                //    return;
                //}
                //nPayloadSize = stParam.nCurValue;
                //if (nPayloadSize>m_nBufSizeForDriver)
                //{
                //    m_nBufSizeForDriver = nPayloadSize;
                //    m_pBufforDriver = new byte[m_nBufSizeForDriver];
                //    m_nBufSizeForSaveImage = m_nBufSizeForDriver * 3 + 2048;
                //    m_pBufferForSaveImage = new byte[m_nBufSizeForSaveImage];
                //}
                byte[] m_pBufForSaveImage = new byte[3 * (pFrameInfo.nWidth * pFrameInfo.nHeight) + 2048];
                ///数组字节转换为图像指针
                latestFrameAddress = Marshal.UnsafeAddrOfPinnedArrayElement(m_pBufForSaveImage, 0);
                ///转换图像格式并获取图像的指针地址
                MyCamera.MV_PIXEL_CONVERT_PARAM stConverPixelParam = new MyCamera.MV_PIXEL_CONVERT_PARAM();
                stConverPixelParam.nWidth         = pFrameInfo.nWidth;
                stConverPixelParam.nHeight        = pFrameInfo.nHeight;
                stConverPixelParam.pSrcData       = pData;
                stConverPixelParam.nSrcDataLen    = pFrameInfo.nFrameLen;
                stConverPixelParam.enSrcPixelType = pFrameInfo.enPixelType;
                stConverPixelParam.enDstPixelType = enDstPixelType;
                stConverPixelParam.pDstBuffer     = latestFrameAddress;
                stConverPixelParam.nDstBufferSize = (uint)(3 * (pFrameInfo.nWidth * pFrameInfo.nHeight) + 2048);
                //赋值图像指针地址
                int nRet = camera.MV_CC_ConvertPixelType_NET(ref stConverPixelParam);
                if (MyCamera.MV_OK != nRet)
                {
                    return;
                }
                if (enDstPixelType == MyCamera.MvGvspPixelType.PixelType_Gvsp_Mono8)
                {
                    //************************Mono8 转 Himage*******************************
                    // 转换为Halcon图像显示
                    Image.GenImage1("byte", pFrameInfo.nWidth, pFrameInfo.nHeight, latestFrameAddress);

                    GC.Collect();
                }
                else
                {
                    //*********************RGB8 转 HImage**************************
                    //for (int i = 0; i < pFrameInfo.nHeight; i++)
                    //{
                    //    for (int j = 0; j < pFrameInfo.nWidth; j++)
                    //    {
                    //        byte chRed = m_pBufForSaveImage[i * pFrameInfo.nWidth * 3 + j * 3];
                    //        m_pBufForSaveImage[i * pFrameInfo.nWidth * 3 + j * 3] = m_pBufForSaveImage[i * pFrameInfo.nWidth * 3 + j * 3 + 2];
                    //        m_pBufForSaveImage[i * pFrameInfo.nWidth * 3 + j * 3 + 2] = chRed;
                    //    }
                    //}
                    try
                    {
                        int imageWidth  = pFrameInfo.nWidth - 1;
                        int imageHeight = pFrameInfo.nHeight - 1;
                        Image.GenImageInterleaved(latestFrameAddress, "bgr",
                                                  imageWidth, imageHeight, -1, "byte", imageWidth, imageHeight, 0, 0, -1, 0);
                        GC.Collect();
                    }
                    catch
                    {
                    }
                }
                HImage newImage = Image.CopyImage();
                base.ProcessGrabTimeCallback(grabTime);
                // 抛出图像处理事件
                base.ProcessImageCallBack(newImage);
                Image.Dispose();
            }
            catch (Exception e)
            {
                WriteErrorLog($"{Info.UserID} camera {e.ToString()}");
            }
        }