示例#1
0
        public gxImage GetGxImage()
        {
            if (this.cam == null)
            {
                return(null);
            }

            cam.GetCaptureInfo();
            int cap_frameix = cam.GetPropertyInt("captureinfo/newestframeix");

            gxImage image = new gxImage();

            //String fname = "";
            //fname = String.Format("getframe{0:00}.bmp", i);
            //Console.Write(String.Format("GetFrame {0}...", i));

            cam.GetFrame((int)FXCAM_GETFRAME_FLAGS.FXCAM_GETFRAME_NEWEST, 0, image);
            //  Console.WriteLine("OK");
            // Save to file
            if (image.GetPixelFormat() != (int)GX_PIXELFORMATS.GX_GRAY)
            {
                image.Convert((int)GX_PIXELFORMATS.GX_RGB);                                                             // color image
            }
            //Console.Write(String.Format("Save image: {0}...", fname));

            //image.Save(fname, (int)GX_IMGFILEFORMATS.GX_BMP);
            //Console.WriteLine("OK");

            //System.Drawing.Image img = System.Drawing.Image.FromStream(new System.IO.MemoryStream(image.SaveToMem((int)gx.GX_IMGFILEFORMATS.GX_JPEG)));
            return(image);
        }
        /// <summary>
        /// Chup hinh nhan dang
        /// </summary>
        /// <param name="pTransactionID"></param>
        /// <param name="pLaneID"></param>
        public int CaptureImage(string pTransactionID, string pLaneID)
        {
            int retValue = 0;

            try
            {
                // Unlock all locked frames if there is any
                //mFxCamera.UnlockFrames((ushort)FXCAM_UNLOCK_FLAGS.FXCAM_UNLOCK_RELEASE_ALL, 0, 0);

                // Start capture
                mFxCamera.StartCapture();

                //Waiting for the end of the camera initialization
                Thread.Sleep(DELAY_DURATION);

                // Wait for valid frame
                //mFxCamera.GetCaptureInfo();
                int cap_frameix = mFxCamera.GetPropertyInt("captureinfo/newestframeix");

                gxImage image = new gxImage();
                for (int i = 1; i <= NumPictures; i++)
                {
                    // Save image at local folder
                    mFxCamera.GetFrame((int)FXCAM_GETFRAME_FLAGS.FXCAM_GETFRAME_NEWEST, 0, image);
                    if (image.GetPixelFormat() != (int)GX_PIXELFORMATS.GX_GRAY)
                    {
                        image.Convert((int)GX_PIXELFORMATS.GX_RGB);     // color image
                    }
                    image.Save(DefaultFileName, (int)GX_IMGFILEFORMATS.GX_JPEG);

                    ///TEST LOG
                    //WriteLogFile(this.GetType() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, "Start save image at server");

                    // Save image at server
                    string   pFileName = pTransactionID + "_" + pLaneID + "_" + i.ToString();
                    string   subfolder = string.Empty;
                    string[] temp      = pFileName.Split('_');
                    string   path      = string.Empty;
                    if (System.IO.Directory.Exists(RootFolder))
                    {
                        path  = RootFolder;
                        path += @"\" + pFileName.Substring(0, 10);
                        path += @"\" + temp[1];//004
                        if (!System.IO.Directory.Exists(path))
                        {
                            //Neu thu muc chua ton tai->tao thu muc do'
                            Directory.CreateDirectory(path);
                        }

                        ///TEST LOG
                        //WriteLogFile(this.GetType() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, "Check exists recog folder");

                        if (System.IO.Directory.Exists(path))
                        {
                            using (Image tempPic = Image.FromFile(DefaultFileName)) //chỗ này nếu ko chụp dc hình thì phải tạo 1 ảnh tạm
                            {
                                string pFullPath = path + @"\" + pFileName + ".jpg";
                                tempPic.Save(pFullPath);
                            }
                        }
                        else
                        {
                            WriteLogFile(this.GetType() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, "Cannot create folder: " + path);
                            return(-1); //Khong copy hinh len server duoc
                        }
                    }
                    else
                    {
                        WriteLogFile(this.GetType() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, "Folder " + RootFolder + " doesn't exist");
                    }
                }
                // Stop capture
                mFxCamera.StopCapture();

                ///TEST LOG
                //WriteLogFile(this.GetType() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, "Stop Capture");

                retValue = 1;
            }
            catch (Exception ex)
            {
                WriteLogFile(this.GetType() + "." + System.Reflection.MethodBase.GetCurrentMethod().Name, ex.Message);
            }

            return(retValue);
        }