Exemple #1
0
        unsafe private void Button_CAPTURE_Click(object sender, EventArgs e)
        {
            if (m_nDeviceCurSel == -1)
            {
                return;
            }
            int nCaptureWidth    = 0;
            int nCaptureHeight   = 0;
            int nCaptureBitCount = 0;

            KSJApiTriggerMode.KSJ_TriggerModeSet(m_nDeviceCurSel, KSJApiTriggerMode.KSJ_TRIGGERMODE.KSJ_TRIGGER_SOFTWARE);
            int nRet = KSJApiBase.KSJ_CaptureGetSizeEx(m_nDeviceCurSel, ref nCaptureWidth, ref nCaptureHeight, ref nCaptureBitCount);

            PrintErrorMessage(nRet);

            byte[] pImageData = new byte[nCaptureWidth * nCaptureHeight * (nCaptureBitCount >> 3)];

            long counterStart = 0;

            KSJWin.QueryPerformanceCounter(ref counterStart);

            nRet = KSJApiBase.KSJ_CaptureRgbData(m_nDeviceCurSel, pImageData);
            PrintErrorMessage(nRet);
            if (nRet != KSJCode.RET_SUCCESS)
            {
                TextBox_ELAPSE_TIME.Text = "Capture Fail.";
            }

            long counterEnd = 0;

            KSJWin.QueryPerformanceCounter(ref counterEnd);

            long nFreq = 0;

            KSJWin.QueryPerformanceFrequency(ref nFreq);

            float fInterval = (float)(counterEnd - counterStart);
            float fElapse   = fInterval / (float)nFreq * 1000;  // MS

            bool bCheck = CheckBox_SAVE.Checked;

            if (bCheck)
            {
                string szFileName = string.Format("capture-{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}.bmp", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
                                                  DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond, fElapse);
                KSJApiBase.KSJ_HelperSaveToBmp(pImageData, nCaptureWidth, nCaptureHeight, nCaptureBitCount, szFileName);
            }

            TextBox_ELAPSE_TIME.Text = string.Format("Elapse: {0}ms", fElapse);

            Bitmap bitmap = BytesToBitmap(pImageData, nCaptureWidth, nCaptureHeight, nCaptureBitCount);

            PictureBox_PREVIEWWND.Image = bitmap;
        }
Exemple #2
0
        unsafe void HandleImage1(ref Jai_FactoryWrapper.ImageInfo ImageInfo)//回调函数,ImageInfo为图像结构体,ImageBuffer为图像数据指针
        {
            bool bCheck = CheckBox_SAVE.Checked;

            if (ImageInfo.PixelFormat == Jai_FactoryWrapper.EPixelFormatType.GVSP_PIX_MONO8)//黑白相机
            {
                long counterStart = 0;
                KSJWin.QueryPerformanceCounter(ref counterStart);
                long counterEnd = 0;
                KSJWin.QueryPerformanceCounter(ref counterEnd);
                long nFreq = 0;
                KSJWin.QueryPerformanceFrequency(ref nFreq);
                float fInterval = (float)(counterEnd - counterStart);
                float fElapse   = fInterval / (float)nFreq * 1000;  // MS
                TextBox_ELAPSE_TIME.Text = string.Format("Elapse: {0}ms", fElapse);
                if (bCheck)
                {
                    Jai_FactoryWrapper.J_Image_SaveFileEx(ref ImageInfo, "image.bmp", Jai_FactoryWrapper.ESaveFileFormat.Bmp, 75);        //保存函数
                }
            }
            else
            {
                Jai_FactoryWrapper.J_Image_Malloc(ref ImageInfo, ref m_ConvertedImageInfo);
                Jai_FactoryWrapper.J_Image_FromRawToImage(ref ImageInfo, ref m_ConvertedImageInfo, 4096, 4096, 4096);
                if (bCheck)
                {
                    long counterStart = 0;
                    KSJWin.QueryPerformanceCounter(ref counterStart);
                    long counterEnd = 0;
                    KSJWin.QueryPerformanceCounter(ref counterEnd);

                    long nFreq = 0;
                    KSJWin.QueryPerformanceFrequency(ref nFreq);
                    float fInterval = (float)(counterEnd - counterStart);
                    float fElapse   = fInterval / (float)nFreq * 1000;  // MS
                    TextBox_ELAPSE_TIME.Text = string.Format("Elapse: {0}ms", fElapse);
                    Jai_FactoryWrapper.J_Image_SaveFileEx(ref ImageInfo, "image.bmp", Jai_FactoryWrapper.ESaveFileFormat.Bmp, 75);
                    bCheck = false;
                    CheckBox_SAVE.Checked = false;
                }
                Jai_FactoryWrapper.J_Image_Free(ref m_ConvertedImageInfo);
            }

            return;
        }
Exemple #3
0
        unsafe void HandleImage1(ref Jai_FactoryWrapper.ImageInfo ImageInfo)
        {
            bool bCheck = CheckBox_SAVE.Checked;

            if (ImageInfo.PixelFormat == Jai_FactoryWrapper.EPixelFormatType.GVSP_PIX_MONO8)
            {
                long counterStart = 0;
                KSJWin.QueryPerformanceCounter(ref counterStart);
                HObject Hobj;
                HOperatorSet.GenImage1(out Hobj, "byte", ImageInfo.SizeX, ImageInfo.SizeY, ImageInfo.ImageBuffer);
                long counterEnd = 0;
                KSJWin.QueryPerformanceCounter(ref counterEnd);
                long nFreq = 0;
                KSJWin.QueryPerformanceFrequency(ref nFreq);
                float fInterval = (float)(counterEnd - counterStart);
                float fElapse   = fInterval / (float)nFreq * 1000;  // MS
                TextBox_ELAPSE_TIME.Text = string.Format("Elapse: {0}ms", fElapse);
                if (bCheck)
                {
                    HOperatorSet.WriteImage(Hobj, "bmp", 0, "JAIToHalcon.bmp");
                }
            }
            else
            {
                Jai_FactoryWrapper.J_Image_Malloc(ref ImageInfo, ref m_ConvertedImageInfo);
                Jai_FactoryWrapper.J_Image_FromRawToImage(ref ImageInfo, ref m_ConvertedImageInfo, 4096, 4096, 4096);
                if (bCheck)
                {
                    long counterStart = 0;
                    KSJWin.QueryPerformanceCounter(ref counterStart);
                    int size = (int)(ImageInfo.SizeX * ImageInfo.SizeY * 3);
                    Marshal.Copy((IntPtr)m_ConvertedImageInfo.ImageBuffer, imagedata, 0, size);
                    for (int i = 0; i < ImageInfo.SizeY; i++)
                    {
                        int nOffset     = (int)(i * ImageInfo.SizeX);
                        int nRealOffset = (int)(i * ImageInfo.SizeX * 3);
                        for (int j = 0; j < ImageInfo.SizeX; j++)
                        {
                            int nPixelOffset = j * 3;
                            arrayR[nOffset + j] = imagedata[nRealOffset + nPixelOffset + 2];
                            arrayG[nOffset + j] = imagedata[nRealOffset + nPixelOffset + 1];
                            arrayB[nOffset + j] = imagedata[nRealOffset + nPixelOffset];
                        }
                    }

                    HObject Hobj;
                    fixed(byte *pR = arrayR, pG = arrayG, pB = arrayB)
                    {
                        HOperatorSet.GenImage3(out Hobj, "byte", ImageInfo.SizeX, ImageInfo.SizeY, new IntPtr(pR), new IntPtr(pG), new IntPtr(pB));
                    }

                    long counterEnd = 0;
                    KSJWin.QueryPerformanceCounter(ref counterEnd);

                    long nFreq = 0;
                    KSJWin.QueryPerformanceFrequency(ref nFreq);
                    float fInterval = (float)(counterEnd - counterStart);
                    float fElapse   = fInterval / (float)nFreq * 1000;  // MS
                    TextBox_ELAPSE_TIME.Text = string.Format("Elapse: {0}ms", fElapse);
                    HOperatorSet.WriteImage(Hobj, "bmp", 0, "JAIToHalcon.bmp");
                    bCheck = false;
                    CheckBox_SAVE.Checked = false;
                }
                Jai_FactoryWrapper.J_Image_Free(ref m_ConvertedImageInfo);
            }

            return;
        }
Exemple #4
0
        unsafe private void Button_CAPTURE_Click(object sender, EventArgs e)
        {
            if (m_nDeviceCurSel == -1)
            {
                return;
            }
            int nCaptureWidth    = 0;
            int nCaptureHeight   = 0;
            int nCaptureBitCount = 0;

            int nRet = KSJApiBase.KSJ_CaptureGetSizeEx(m_nDeviceCurSel, ref nCaptureWidth, ref nCaptureHeight, ref nCaptureBitCount);

            PrintErrorMessage(nRet);

            byte[] pImageData = new byte[nCaptureWidth * nCaptureHeight * (nCaptureBitCount >> 3)];

            long counterStart = 0;

            KSJWin.QueryPerformanceCounter(ref counterStart);

            nRet = KSJApiBase.KSJ_CaptureRgbData(m_nDeviceCurSel, pImageData);
            PrintErrorMessage(nRet);
            if (nRet != KSJCode.RET_SUCCESS)
            {
                TextBox_ELAPSE_TIME.Text = "Capture Fail.";
            }

            bool    bCheck = CheckBox_SAVE.Checked;
            HObject HobjConvert;

            if (nCaptureBitCount == 8)
            {
                int    size    = pImageData.Length;
                IntPtr ptrdata = Marshal.AllocHGlobal(size);
                Marshal.Copy(pImageData, 0, ptrdata, size);
                HObject Hobj;
                HOperatorSet.GenImage1(out Hobj, "byte", nCaptureWidth, nCaptureHeight, ptrdata);
                HOperatorSet.MirrorImage(Hobj, out HobjConvert, "row");
                if (bCheck)
                {
                    HOperatorSet.WriteImage(HobjConvert, "bmp", 0, "KSJToHalcon.bmp");
                }
            }
            else
            {
                byte[] arrayR = new byte[nCaptureWidth * nCaptureHeight]; //红色数组
                byte[] arrayG = new byte[nCaptureWidth * nCaptureHeight]; //绿色数组
                byte[] arrayB = new byte[nCaptureWidth * nCaptureHeight]; //蓝色数组
                int    nPixel = nCaptureBitCount / 8;
                for (int i = 0; i < nCaptureHeight; i++)
                {
                    int nOffset     = i * nCaptureWidth;
                    int nRealOffset = i * nCaptureWidth * 3;
                    for (int j = 0; j < nCaptureWidth; j++)
                    {
                        int nPixelOffset = j * nPixel;
                        arrayR[nOffset + j] = pImageData[nRealOffset + nPixelOffset + 2];
                        arrayG[nOffset + j] = pImageData[nRealOffset + nPixelOffset + 1];
                        arrayB[nOffset + j] = pImageData[nRealOffset + nPixelOffset];
                    }
                }

                HObject Hobj;
                fixed(byte *pR = arrayR, pG = arrayG, pB = arrayB)
                {
                    HOperatorSet.GenImage3(out Hobj, "byte", nCaptureWidth, nCaptureHeight, new IntPtr(pR), new IntPtr(pG), new IntPtr(pB));
                }

                HOperatorSet.MirrorImage(Hobj, out HobjConvert, "row");
                if (bCheck)
                {
                    HOperatorSet.WriteImage(HobjConvert, "bmp", 0, "KSJToHalcon.bmp");
                }
            }

            long counterEnd = 0;

            KSJWin.QueryPerformanceCounter(ref counterEnd);

            long nFreq = 0;

            KSJWin.QueryPerformanceFrequency(ref nFreq);
            float fInterval = (float)(counterEnd - counterStart);
            float fElapse   = fInterval / (float)nFreq * 1000;  // MS

            HOperatorSet.SetPart(this.hWindowControl1.HalconWindow, 0, 0, nCaptureHeight - 1, nCaptureWidth - 1);
            HOperatorSet.DispObj(HobjConvert, this.hWindowControl1.HalconWindow);
            //if (bCheck)
            //{
            //    string szFileName = string.Format("capture-{0}-{1}-{2}-{3}-{4}-{5}-{6}-{7}.bmp", DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day,
            //        DateTime.Now.Hour, DateTime.Now.Minute, DateTime.Now.Second, DateTime.Now.Millisecond, fElapse);
            //    KSJApiBase.KSJ_HelperSaveToBmp(pImageData, nCaptureWidth, nCaptureHeight, nCaptureBitCount, szFileName);
            //}

            //TextBox_ELAPSE_TIME.Text = string.Format("Elapse: {0}ms", fElapse);
            //Bitmap bitmap = BytesToBitmap(pImageData, nCaptureWidth, nCaptureHeight, nCaptureBitCount);
            //PictureBox_PREVIEWWND.Image = Image.FromHbitmap(bitmap.GetHbitmap());
        }