예제 #1
0
 public async Task Process()
 {
     var frames = 1;
     await Task.Run(() => {
         while (true)
         {
             Application.Current.Dispatcher.Invoke(() => {
                 var query       = capture.QueryFrame().ToImage <Gray, byte>();
                 CaptureArgs arg = new CaptureArgs()
                 {
                     Image = query
                 };
                 OnCapture?.Invoke(this, arg);
                 if (arg.OutPutImage != null)
                 {
                     //var arg.OutPutImage = query.Convert<Bgr, float>();
                     //CvInvoke.PutText(queryBgr, "FPS: " + (frames / seconds).ToString(), new System.Drawing.Point(10, 60), FontFace.HersheySimplex, 1.0, new Bgr(Color.Red).MCvScalar);
                     //ContextChanged = queryFunc.Convert<Bgr, float>().Bitmap.Convert();
                     //Context = queryBgr.Bitmap.Convert();
                 }
             });
             frames++;
             Task.Delay(16).Wait();
         }
     });
 }
예제 #2
0
파일: Target.cs 프로젝트: RoseDuf/Crafty
 private void Capture()
 {
     IsCaptured = true;
     starObject.GetComponent <Star>().Claim();
     TurnIntoStar();
     OnCapture?.Invoke(this);
 }
예제 #3
0
    /// <summary>
    /// 截屏
    /// </summary>
    /// <param name="rt">屏幕范围</param>
    /// <returns></returns>
    public static IEnumerator CaptureScreenshot(Rect rt, OnCapture onCapture)
    {
        yield return(new WaitForEndOfFrame());

        Texture2D screenShot = new Texture2D((int)rt.width, (int)rt.height, TextureFormat.RGB24, false);

        screenShot.ReadPixels(rt, 0, 0);
        screenShot.Apply();
        if (onCapture != null)
        {
            onCapture(screenShot);
        }
    }
        public void Start()
        {
            _run = true;

            var display = new NativeMethods.DISPLAY_DEVICE
            {
                cb = Marshal.SizeOf(typeof(NativeMethods.DISPLAY_DEVICE))
            };

            try
            {
                NativeMethods.EnumDisplayDevices(null, _displayIndex, ref display, 0);
            }
            catch (Exception ex)
            {
                Console.WriteLine($"{ex}");
            }

            var dm = new NativeMethods.DEVMODE
            {
                dmSize = (short)Marshal.SizeOf(typeof(NativeMethods.DEVMODE))
            };

            NativeMethods.EnumDisplaySettings(display.DeviceName, -1, ref dm);
            Task.Factory.StartNew(() =>
            {
                while (_run)
                {
                    using var bitmap = new Bitmap(dm.dmPelsWidth, dm.dmPelsHeight);
                    using var g      = Graphics.FromImage(bitmap);
                    g.CopyFromScreen(dm.dmPositionX, dm.dmPositionY, 0, 0, bitmap.Size);
                    using var ms = new MemoryStream();
                    bitmap.Save(ms, System.Drawing.Imaging.ImageFormat.Bmp);
                    OnCapture?.Invoke(this, ms.ToArray());
                }
            }, CancellationToken.None, TaskCreationOptions.None, TaskScheduler.Default);
        }
예제 #5
0
    void Update()
    {
        if (_selectedChessPiece == null)
        {
            ChessPieceBase chessPiece = GetRaycastedChessPiece();

            if (chessPiece != null)
            {
                if (Input.GetKeyDown(KeyCode.Mouse0))
                {
                    _selectedChessPiece = chessPiece;
                    OnChessPieceSelection?.Invoke(chessPiece);
                }
            }
        }
        else
        {
            Square square = GetRaycastedSquare();

            if (square != null)
            {
                if (Input.GetKeyDown(KeyCode.Mouse0) && _selectedChessPiece.GetMoves().Contains(square.transform.position))
                {
                    Vector3 temp = _selectedChessPiece.transform.position;
                    _selectedChessPiece.transform.position = square.transform.position;
                    if (!Board.CheckIfCheck(_selectedChessPiece.Color))
                    {
                        OnMove?.Invoke(_selectedChessPiece);
                        OnDeselection?.Invoke();
                        _selectedChessPiece = null;
                    }
                    else
                    {
                        _selectedChessPiece.transform.position = temp;
                    }
                }
            }

            ChessPieceBase chessPiece = GetRaycastedChessPiece();

            if (chessPiece != null && chessPiece.Type != PiecesType.King)
            {
                if (Input.GetKeyDown(KeyCode.Mouse0) && _selectedChessPiece != null && _selectedChessPiece.GetMoves().Contains(chessPiece.transform.position))
                {
                    Vector3 temp = _selectedChessPiece.transform.position;
                    _selectedChessPiece.transform.position = chessPiece.transform.position;
                    if (!Board.CheckIfCheck(_selectedChessPiece.Color))
                    {
                        OnMove?.Invoke(_selectedChessPiece);
                        OnCapture?.Invoke(chessPiece, _selectedChessPiece);
                        OnDeselection?.Invoke();
                        _selectedChessPiece = null;
                    }
                    else
                    {
                        _selectedChessPiece.transform.position = temp;
                    }
                }
            }
        }
        if (Input.GetKeyDown(KeyCode.Mouse1))
        {
            OnDeselection?.Invoke();
            _selectedChessPiece = null;
        }
    }
예제 #6
0
        private void OnLoaded()
        {
            _frame            = new Mat();
            cmdCapture.Click += (s, e) =>
            {
                // This
                _grabbedFrame = new Mat();
                _capt.Grab();
                _capt.Retrieve(_grabbedFrame, 0);
                // Or this
                //_grabbedFrame = _capt.QueryFrame();

                if (!_grabbedFrame.IsEmpty)
                {
                    ImageProcessing.ImageProcessingInstance.SaveDirectory = "Gallery/";
                    ImageProcessing.ImageProcessingInstance.SaveImageToFile(new Func <string>(() =>
                    {
                        var files = ImageProcessing.ImageProcessingInstance.GetFilesInSaveDir(ImageProcessing.FileNameSetting.WithoutExtension);
                        if (files.Count == 0)
                        {
                            return(0.ToString());
                        }
                        return((Convert.ToInt32(files.Last()) + 1).ToString());
                    }).Invoke(), _grabbedFrame.Bitmap, System.Drawing.Imaging.ImageFormat.Jpeg);

                    OnCapture?.Invoke(this, _grabbedFrame.Bitmap);
                }
            };

            try
            {
                _capt = new VideoCapture(0);

                _capt.ImageGrabbed += (s, e) =>
                {
                    if (_capt == null)
                    {
                        return;
                    }
                    if (_capt.Ptr == IntPtr.Zero)
                    {
                        return;
                    }
                    _capt.Retrieve(_frame, 0);
                    if (_frame.IsEmpty)
                    {
                        return;
                    }
                    if (_frame.Bitmap == null)
                    {
                        return;
                    }
                    //TryFindFace(_frame);
                    try
                    {
                        CvInvoke.PyrDown(_frame, _frame);
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine(ex.Message);
                    }

                    Dispatcher.Invoke(() =>
                    {
                        imgPreview.Source = BitmapSourceConvert.ToBitmapSource(_frame);
                    });
                };

                _capt.FlipHorizontal = !_capt.FlipHorizontal;
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Err", MessageBoxButton.OK, MessageBoxImage.Error);
            }

            //_capt.SetCaptureProperty(Emgu.CV.CvEnum.CapProp.Fps, 15); // Camera settings
            SetHighResolution();

            //_capt.Start();

            new Task(async() =>
            {
                await Task.Delay(1100);
                Dispatcher.Invoke(() =>
                {
                    grdMainContainer.Children.RemoveAt(grdMainContainer.Children.Count - 1);
                    imgPreview.Visibility = Visibility.Visible;
                    cmdCapture.Visibility = Visibility.Visible;
                });
            }).Start();
        }