locked
[WPSL][C#]PhotoCamera: tap focus error RRS feed

  • Question

  • Hi everyone. I'm trying to focus the PhotoCamera device and it works if I show a messagebox right before FocusAtPoint... but if I comment out the messagebox I get an error saying "The operation is not valid due to the current state of the object.." here is the code:
    void focus_Tapped(object sender, System.Windows.Input.GestureEventArgs e)
            {
                if (cam != null)
                {
                    if (cam.IsFocusAtPointSupported == true)
                    {
                        try
                        {
                            // Determine the location of the tap.
                            System.Windows.Point tapLocation = e.GetPosition(viewfinderRectangle);
    
                            // Show the focus brackets and focus at point.
                            focusBrackets.HorizontalAlignment = System.Windows.HorizontalAlignment.Left;
                            focusBrackets.VerticalAlignment = System.Windows.VerticalAlignment.Top;
                            focusBrackets.Margin = new Thickness(tapLocation.X - 30, tapLocation.Y - 28, 0, 0);
    
                            // Determine the focus point.
                            double focusXPercentage = tapLocation.X / cam.Resolution.Width;
                            double focusYPercentage = tapLocation.Y / cam.Resolution.Height;
    
                            // Show the focus brackets and focus at point.
                            focusBrackets.Visibility = Visibility.Visible;
                            //MessageBox.Show("k");
                            cam.FocusAtPoint(focusXPercentage, focusYPercentage);
                            // Write a message to the UI.
                            this.Dispatcher.BeginInvoke(delegate()
                            {
                                txtDebugPhoto.Text = String.Format("Camera focusing at point: x: {0:N2} , y: {1:N2}", (int)tapLocation.X, (int)tapLocation.Y);
                            });
    
                        }
                        catch (Exception focusError)
                        {
                            // Cannot focus when a capture is in progress.
                            this.Dispatcher.BeginInvoke(delegate()
                            {
                                // Write a message to the UI.
                                txtDebugPhoto.Text = focusError.GetBaseException().Message;
                                // Hide focus brackets.
                                focusBrackets.Visibility = Visibility.Collapsed;
                            });
                        }
                    }
                    else
                    {
                        // Write a message to the UI.
                        this.Dispatcher.BeginInvoke(delegate()
                        {
                            txtDebugPhoto.Text = "Camera does not support FocusAtPoint().";
                        });
                    }
                }
            }
    Why if I show a messagebox it works correctly but not if I run the code without the messagebox? Thank you in adavance.

    Friday, May 29, 2015 2:57 PM

Answers

  • There is a high chance that in your phone camera setting, tap is configured to take picture. This means whenever you try to tap, a capture of picture will be triggered. But when you show the message box, the operation will be done by then, hence you remaining code works without any issues. Disable it in the setting..

    Pictures > More More icon > Settings.

    Hope this helps.


    sivanitha

    Friday, May 29, 2015 3:18 PM

All replies

  • There is a high chance that in your phone camera setting, tap is configured to take picture. This means whenever you try to tap, a capture of picture will be triggered. But when you show the message box, the operation will be done by then, hence you remaining code works without any issues. Disable it in the setting..

    Pictures > More More icon > Settings.

    Hope this helps.


    sivanitha

    Friday, May 29, 2015 3:18 PM
  • Is there perhaps something else I can do instead of the messagebox, like block the Dispather Thread ect. something more user friendly? Thanks.

    I would like to do something like below but that code crashes the app...

    // Determine the focus point.
                            double focusXPercentage = tapLocation.X / cam.Resolution.Width;
                            double focusYPercentage = tapLocation.Y / cam.Resolution.Height;
    
                            // Show the focus brackets and focus at point.
                            focusBrackets.Visibility = Visibility.Visible;
                            //MessageBox.Show("Focus needs this MessageBox to display to complete sucessfully. Thank you.");
                            System.Threading.ThreadPool.QueueUserWorkItem(obj =>
                            {
                                System.Threading.Thread.Sleep(1000);
    
                                Dispatcher.BeginInvoke(() =>
                                {
                                    cam.FocusAtPoint(focusXPercentage, focusYPercentage);
                                    // Write a message to the UI.
                                    this.Dispatcher.BeginInvoke(delegate()
                                    {
                                        txtDebugPhoto.Text = String.Format("Camera focusing at point: x: {0:N2} , y: {1:N2}", (int)tapLocation.X, (int)tapLocation.Y);
                                    });
                                });
                            });

     
    Saturday, May 30, 2015 11:06 AM