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);
        }