Exemplo n.º 1
0
        /// <summary>
        /// Call Back 함수로 Image가 새로 들어올때 마다 Event를 발생시킴.
        /// </summary>
        /// <param Camera="callBackOwner"></param>
        /// <param name="image"></param>
        /// <returns></returns>
        public int ImageCallback(object callBackOwner, ref BGAPI.Image image)
        {
            // get image to IntPtr
            image.get(ref m_ImageBuffer);
            // Copy to Byte[]
            Marshal.Copy(m_ImageBuffer, m_ImgBits, 0, m_CameraWidth * m_CameraHeight);
            // MIL Buffer에 Copy
            MIL.MbufPut(m_MilImage, m_ImgBits);
            MIL.MbufControl(m_MilImage, MIL.M_MODIFIED, MIL.M_DEFAULT);

            // Timer 확인
            MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_READ, ref dGrabInterval);

            String strResult = String.Format(" Scan Time : {0:0.00} ", dGrabInterval);

            // Timer Reset
            MIL.MappTimer(MIL.M_DEFAULT, MIL.M_TIMER_RESET, ref dGrabInterval);
            // new image 호출 (Image Grab 지령)
            m_pCamera.GetCamera().setImage(ref image);

            return(SUCCESS);
        }
        /*
         * private int CallbackFunction(object callBackOwner, ref BGAPI.Image image)
         * {
         *
         * /*
         * int bgapierror = 0;
         * System.Drawing.Imaging.ColorPalette palette;
         * System.Drawing.Imaging.PixelFormat format = new System.Drawing.Imaging.PixelFormat(); // UNDEFINED
         * //TransformationType tmpConvertImage = mConvertImage; // AFTERCAPTURE
         *
         * int w = 0;
         * int h = 0;
         * int len = 0;
         * IntPtr buffer = new IntPtr(0);
         * int pixelformat = 0; // 173....
         * int stride = 0;
         * image.getPixelFormat(ref pixelformat);
         * image.getSize(ref w, ref h); //172 130
         * image.getImageLength(ref len); //22360
         * //image.getState(ref imagestatus); // GOOD
         * Size CurrentImageSize = new Size(w, h);
         * draw.Bitmap bitmap = new Bitmap(w, h, );
         * bitmap = new Bitmap
         * bgapierror = image.get(ref buffer);
         * if (pixelformat == BGAPI.Pixtype.MONO8)
         * {
         *  stride = w;
         *  format = System.Drawing.Imaging.PixelFormat.Format8bppIndexed;
         *  Rectangle rect = new Rectangle(0, 0, w, h);
         *  System.Drawing.Imaging.BitmapData bmpData =
         *           bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
         *           bitmap.PixelFormat);
         *  unsafe
         *  {
         *    byte* bitmapBuffer = (byte*)bmpData.Scan0;
         *    byte* byteBuffer = (byte*)buffer.ToPointer();
         *    // Kopieren der Bytes innerhalb des Buffers
         *    for (int i = 0; i < len; i++)
         *    {
         *      bitmapBuffer[i] = byteBuffer[i];
         *    }
         *  }
         *  bitmap.UnlockBits(bmpData);
         *
         *
         *  palette = bitmap.Palette;
         *  int nColors = 256;
         *  for (uint i = 0; i < nColors; i++)
         *  {
         *    uint Alpha = 0xFF;                      // Colors are opaque.
         *    uint Intensity = (uint)(i * 0xFF / (nColors - 1));    // Even distribution.
         *    palette.Entries[i] = Color.FromArgb(
         *        (int)Alpha,
         *        (int)Intensity,
         *        (int)Intensity,
         *        (int)Intensity);
         *  }
         *  bitmap.Palette = palette;
         * }
         *  return 1;
         *
         */

        private int CallbackFunction(object callBackOwner, ref BGAPI.Image image)
        {
            draw.Bitmap bitmap;

            System.Drawing.Imaging.PixelFormat format = new System.Drawing.Imaging.PixelFormat();

            format = System.Drawing.Imaging.PixelFormat.Format8bppIndexed; // Format24bppRgb;
            int len = 0;
            int w   = 0;
            int h   = 0;

            IntPtr imagebuffer = new IntPtr(0);
            int    pixelformat = 0;

            image.getPixelFormat(ref pixelformat);

            image.get(ref imagebuffer);
            image.getSize(ref w, ref h);
            image.getImageLength(ref len);
            image.setDestinationPixelFormat(BGAPI.Pixtype.MONO8);///%% RGB8_Packed
            image.getTransformBufferLength(ref len);
            draw.Rectangle rect = new draw.Rectangle(0, 0, w, h);

            //int stride = w * 3; // old version

            int stride = w;

            bitmap = new draw.Bitmap(w, h, stride, format, imagebuffer);
            System.Drawing.Imaging.BitmapData bmpData =
                bitmap.LockBits(rect, System.Drawing.Imaging.ImageLockMode.ReadWrite,
                                bitmap.PixelFormat);
            unsafe
            {
                byte *bitmapBuffer = (byte *)bmpData.Scan0;
                byte *byteBuffer   = (byte *)imagebuffer.ToPointer();
                // Kopieren der Bytes innerhalb des Buffers
                for (int i = 0; i < len; i++)
                {
                    bitmapBuffer[i] = byteBuffer[i];
                }
            }
            bitmap.UnlockBits(bmpData);
            System.Drawing.Imaging.ColorPalette palette;
            palette = bitmap.Palette;
            int nColors = 256;

            for (uint i = 0; i < nColors; i++)
            {
                uint Alpha     = 0xFF;                             // Colors are opaque.
                uint Intensity = (uint)(i * 0xFF / (nColors - 1)); // Even distribution.
                palette.Entries[i] = Color.FromArgb(
                    (int)Alpha,
                    (int)Intensity,
                    (int)Intensity,
                    (int)Intensity);
            }

            bitmap.Palette = palette;

            if (res != BGAPI.Result.OK)
            {
                Console.WriteLine("bgapi error");
            }

            //-----------------------------------------
            if (OnImageReady != null)
            {
                OnImageReady(bitmap);
            }
            //after you are ready with this image, return it to the camera for the next image
            ((BGAPI.Camera)callBackOwner).setImage(ref image);
            return(res);
        }
Exemplo n.º 3
0
        /// <summary>
        /// Camera 초기화 진행함
        /// </summary>
        /// <param 카메라 번호="iCamNo"></param>
        /// <param 상위 GigE System="pSystem"></param>
        /// <returns></returns>
        public int Initialize(int iCamNo, BGAPI.System pSystem)
        {
            m_iCamID = iCamNo;
            // System 을 받아옴.
            m_CamSystem = pSystem;

            m_CamDeviceInfo = new BGAPI.BGAPIX_CameraInfo();
            m_CamImage      = new BGAPI.Image();

            // create camera
            m_iResult = m_CamSystem.createCamera(iCamNo, ref m_Camera);
            if (m_iResult != BGAPI.Result.OK)
            {
                //"System create camera failed!"
                return(GenerateErrorCode(ERR_VISION_CAMERA_CREATE_FAIL));
            }

            // get camera device information
            m_iResult = m_Camera.getDeviceInformation(ref m_CamState, ref m_CamDeviceInfo);
            m_cCameraData.m_CamDeviceInfo = m_CamDeviceInfo;
            if (m_iResult != BGAPI.Result.OK)
            {   // "Camera get Device Information failed!"
                return(GenerateErrorCode(ERR_VISION_CAMERA_GET_INFO_FAIL));
            }

            // get camera Image information
            m_iResult = m_Camera.getImageFormatDescription(1, ref m_CamImageInfo);

            if (m_iResult != BGAPI.Result.OK)
            {   // "Camera get Device Information failed!"
                return(GenerateErrorCode(ERR_VISION_CAMERA_GET_INFO_FAIL));
            }
            else
            {
                // Camera Pixel Size을 적용한다.
                m_CamPixelSize.Width  = (int)(m_CamImageInfo.iScaleRoiX * m_CamImageInfo.iSizeX);
                m_CamPixelSize.Height = (int)(m_CamImageInfo.iScaleRoiY * m_CamImageInfo.iSizeY);
            }

            // camera open
            m_iResult = m_Camera.open();
            if (m_iResult != BGAPI.Result.OK)
            {   //"Camera open failed!"
                return(GenerateErrorCode(ERR_VISION_CAMERA_CONNECT_FAIL));
            }

            // image create
            m_iResult = BGAPI.EntryPoint.createImage(ref m_CamImage);
            if (m_iResult != BGAPI.Result.OK)
            {   // Create Image failed
                return(GenerateErrorCode(ERR_VISION_CAMERA_CREATE_IMAGE_FAIL));
            }

            // camera & image connect
            m_iResult = m_Camera.setImage(ref m_CamImage);
            if (m_iResult != BGAPI.Result.OK)
            {   // Camera set Image failed!
                return(GenerateErrorCode(ERR_VISION_CAMERA_GET_IMAGE_FAIL));
            }

            // System Init 결과 저장
            m_iResult = SUCCESS;

            return(SUCCESS);
        }