예제 #1
0
        private void SetupConvert()
        {
            pcoDescr       = new PCO_Description();
            pcoDescr.wSize = (ushort)Marshal.SizeOf(pcoDescr);
            int err = 0;

            err = PCO_SDK_LibWrapper.PCO_GetCameraDescription(f_CameraHandle, ref pcoDescr);
            PCO_ConvertStructures.PCO_SensorInfo strsensorinf = new PCO_ConvertStructures.PCO_SensorInfo();
            PCO_ConvertStructures.PCO_Display    strDisplay   = new PCO_ConvertStructures.PCO_Display();
            strsensorinf.wSize              = (ushort)Marshal.SizeOf(strsensorinf);
            strDisplay.wSize                = (ushort)Marshal.SizeOf(strDisplay);
            strsensorinf.wDummy             = 0;
            strsensorinf.iConversionFactor  = 0;
            strsensorinf.iDataBits          = pcoDescr.wDynResDESC;
            strsensorinf.iSensorInfoBits    = 1;
            strsensorinf.iDarkOffset        = 100;
            strsensorinf.dwzzDummy0         = 0;
            strsensorinf.strColorCoeff.da11 = 1.0;
            strsensorinf.strColorCoeff.da12 = 0.0;
            strsensorinf.strColorCoeff.da13 = 0.0;
            strsensorinf.strColorCoeff.da21 = 0.0;
            strsensorinf.strColorCoeff.da22 = 1.0;
            strsensorinf.strColorCoeff.da23 = 0.0;
            strsensorinf.strColorCoeff.da31 = 0.0;
            strsensorinf.strColorCoeff.da32 = 0.0;
            strsensorinf.strColorCoeff.da33 = 1.0;
            strsensorinf.iCamNum            = 0;
            strsensorinf.hCamera            = f_CameraHandle;

            int errorCode;

            /* We created a pointer to a convert object here */
            errorCode = PCO_Convert_LibWrapper.PCO_ConvertCreate(ref f_ConvertHandle, ref strsensorinf, PCO_Convert_LibWrapper.PCO_COLOR_CONVERT);

            PCO_ConvertStructures.PCO_Convert pcoConv = new PCO_ConvertStructures.PCO_Convert();;

            pcoConv.wSize = (ushort)Marshal.SizeOf(pcoConv);
            errorCode     = PCOConvertDll.PCO_Convert_LibWrapper.PCO_ConvertGet(f_ConvertHandle, ref pcoConv);
            pcoConv.wSize = (ushort)Marshal.SizeOf(pcoConv);

            IntPtr debugIntPtr = f_ConvertHandle;

            PCO_ConvertStructures.PCO_Convert pcoConvertlocal = (PCO_ConvertStructures.PCO_Convert)Marshal.PtrToStructure(debugIntPtr, typeof(PCO_ConvertStructures.PCO_Convert));
        }
예제 #2
0
        private void OnGrabImage(object sender, EventArgs evt)
        {
            int err = 0;
            int size;

            System.IntPtr evhandle;
            Bitmap        imagebmp;
            UIntPtr       buf;
            bool          bauto = true;     // set this to true to get auto min max

            UInt16 width       = 0;
            UInt16 height      = 0;
            UInt16 widthmax    = 0;
            UInt16 heightmax   = 0;
            int    ishift      = 16 - pcoDescr.wDynResDESC;
            int    ipadd       = width / 4;
            int    iconvertcol = pcoDescr.wColorPatternDESC / 0x1000;
            int    max;
            int    min;

            ipadd *= 4;
            ipadd  = width - ipadd;

            err  = PCO_SDK_LibWrapper.PCO_GetSizes(f_CameraHandle, ref width, ref height, ref widthmax, ref heightmax);
            size = width * height * 2;

            buf      = UIntPtr.Zero;
            evhandle = IntPtr.Zero;
            if ((bufwidth != width) || (bufheight != height))
            {
                if (bufnr != -1)
                {
                    PCO_SDK_LibWrapper.PCO_FreeBuffer(f_CameraHandle, bufnr);
                }
                bufnr     = -1;
                imagedata = new byte[(width + ipadd) * height * 3];

                err = PCO_SDK_LibWrapper.PCO_AllocateBuffer(f_CameraHandle, ref bufnr, size, ref buf, ref evhandle);
                if (err == 0)
                {
                    bufwidth  = width;
                    bufheight = height;
                }
            }
            else
            {
                err = PCO_SDK_LibWrapper.PCO_GetBuffer(f_CameraHandle, bufnr, ref buf, ref evhandle);
            }

            if ((convertDialog == IntPtr.Zero) && (f_ConvertHandle != IntPtr.Zero))
            {
                var hdl = new WindowInteropHelper(this).Handle;
                PCO_Convert_LibWrapper.PCO_OpenConvertDialog(ref convertDialog, hdl, "Convert Dialog", WM_APPp100, f_ConvertHandle, 500, 100);
            }

            //Mandatory for Cameralink and GigE. Don't care for all other interfaces, so leave it intact here.

            err = PCO_SDK_LibWrapper.PCO_AddBufferEx(f_CameraHandle, 0, 0, bufnr, (UInt16)width, (UInt16)height, (UInt16)pcoDescr.wDynResDESC);

            bool bImageIsOk = false;
            uint res        = WaitForSingleObject(evhandle, 3000);

            if (res == 0)
            {
                bImageIsOk = true;
            }
            if (!bImageIsOk)
            {
                return;
            }
            // End Event Block

            unsafe
            {
                short *bufi = (short *)buf.ToPointer();
                max = 0;
                min = 65535;
                for (int i = 20 * width; i < height * width; i++)
                {
                    bufi[i] >>= ishift;
                    if (bufi[i] > max)
                    {
                        max = bufi[i];
                    }
                    if (bufi[i] < min)
                    {
                        min = bufi[i];
                    }
                }
                if (max <= min)
                {
                    max = min + 1;
                }
            }
            PCO_Convert_LibWrapper.PCO_Convert16TOCOL(f_ConvertHandle, 0, iconvertcol, width, height,
                                                      buf, imagedata);

            if ((convertDialog != IntPtr.Zero) && (f_ConvertHandle != IntPtr.Zero))
            {
                PCO_Convert_LibWrapper.PCO_SetDataToDialog(convertDialog, width, height, buf, imagedata);
            }

            if (bauto)
            {
                PCO_ConvertStructures.PCO_Display strDisplay = new PCO_ConvertStructures.PCO_Display(1)
                {
                    wSize = (ushort)Marshal.SizeOf(typeof(PCO_ConvertStructures.PCO_Display))
                };

                err = PCO_Convert_LibWrapper.PCO_ConvertGetDisplay(f_ConvertHandle, ref strDisplay);
                strDisplay.iScale_min = min;
                strDisplay.iScale_max = max;

                err = PCO_Convert_LibWrapper.PCO_ConvertSetDisplay(f_ConvertHandle, ref strDisplay);
                err = PCO_Convert_LibWrapper.PCO_SetConvertDialog(convertDialog, f_ConvertHandle);
            }
            Dispatcher.Invoke(() => Debug.WriteLine(CbPixelFormatConv.SelectedValue));
            imagebmp = new Bitmap(width, height, PixelFormat.Format24bppRgb);                                                 //Format24bppRgb
            Rectangle  dimension         = new Rectangle(0, 0, imagebmp.Width, imagebmp.Height);
            BitmapData picData           = imagebmp.LockBits(dimension, ImageLockMode.ReadWrite, PixelFormat.Format24bppRgb); //Format24bppRgb
            IntPtr     pixelStartAddress = picData.Scan0;

            //Copy the pixel data into the bitmap structure
            Marshal.Copy(imagedata, 0, pixelStartAddress, imagedata.Length);

            imagebmp.UnlockBits(picData);
            bool isOnSave = false;

            Dispatcher.Invoke(() => isOnSave = cbSaveCameraImage.IsChecked.Value);
            if (isOnSave)
            {
                string propPath = "";
                string prefix   = "00";
                Dispatcher.Invoke(() =>
                {
                    propPath = tbSaveCamPath.Text;
                    prefix   = tbSaveCamPrefix.Text;
                });
                if (propPath.Length <= 0)
                {
                    propPath = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
                }
                var dt    = DateTime.Now;
                var fName = $"{propPath}\\{prefix}-{dt.Month}-{dt.Day}-{dt.Hour}-{dt.Minute}-{dt.Second}_{dt.Millisecond}.jpg";
                if (!Properties.Settings.Default.CameraMaxFrameOn)
                {
                    imagebmp.Save(fName, ImageFormat.Jpeg);
                }
                else if (countSaved <= MaxCameraFrame)
                {
                    imagebmp.Save(fName, ImageFormat.Jpeg);
                    Dispatcher.Invoke(() => tbCurrentFrame.Text = countSaved.ToString());
                    countSaved++;
                }
            }

            Dispatcher.Invoke(() =>
            {
                BitmapImage bmpImage = BitmapToImageSource(imagebmp);
                PictureBox1.Source   = null;
                PictureBox1.Source   = bmpImage;
                LogBox.Items.Insert(0, new LogBoxItem {
                    Dt = DateTime.Now, LogText = "Получено изображение с камеры"
                });
            });
        }