Question

Je ne sais pas comment mettre ça mais je ferai de mon mieux.

J'ai une application de formulaire Windows qui utilise une webcam pour prendre une photo d'un utilisateur qui fonctionne bien, j'utilise la bibliothèque DirectShownet trouvée ici http://directshownet.sourceforge.net/ et ont utilisé l'échantillon DXSNAP pour prendre la photo.

La partie suivante de l'application utilise un lecteur de balises RFID qu'une fois qu'une balise est numérisée, il appelle la méthode Take Photo. Et c'est là que le problème arrive, car la méthode d'écoute RFID est exécutée dans un thread séparé sur le thread de l'interface graphique, car il s'agit d'une boucle infinie.

La classe utilisée pour prendre l'image prend une instance d'un contrôle d'image en tant que paramètre pour son constructeur, qui est créé dans le thread principal, et je pense que c'est là que se trouve le problème.

Chaque partie de l'application fonctionne bien séparément, mais lorsque j'essaie d'appeler la méthode pour prendre la photo du fil d'écoute, tout l'enfer se détache et l'application se bloque.

Est-ce que quelqu'un sait comment je pourrais appeler une méthode de l'objet qui est initialisé dans le fil principal (qui prend la photo) du fil d'écoute?

J'ai essayé de déléguer et d'invoquer, mais je ne peux pas le comprendre car je ne veux pas manipuler directement le contrôle, mais plutôt laisser l'objet le faire dans le fil principal.

Voici un code:

    private Capture cam;
    private int portIndex = -1;
    private ArrayList AlreadyOpenPortList = new ArrayList();
    private byte readerAddr = 0;
    private Thread listenThread;
    IntPtr m_ip = IntPtr.Zero;

    public podiumForm()
    {
        InitializeComponent();

        // scanner and camera startup
        startCam();
        openComs();
        openRF();
        startListening();
    }


 private void startListening()
    {
        listenThread = new Thread(new ThreadStart(this.Listen));
        listenThread.Start();
    }

    /// <summary>
    /// Method of retrieving code tag details from reader
    /// </summary>
    private void Listen()        
    {
        int fCmdRet = 0x30;
        byte state = 1;
        byte AFI = 00;
        byte[] DSFIDAndUID = new byte[9];
        byte cardNumber = 0;
        string strDSFIDAndUID = "";
        byte outputSet;

        if (!GetCurrentUsePort())
        {
            MessageBox.Show("Open ComPort, Please");
            return;
        }


        while (true)
        {
            fCmdRet = StaticClassReaderA.Inventory(ref readerAddr, ref state, ref AFI, DSFIDAndUID, ref cardNumber, portIndex);
            if (fCmdRet == 0)
            {
                outputSet = 0;
                fCmdRet = StaticClassReaderA.SetGeneralOutput(ref readerAddr, ref outputSet, portIndex);
                strDSFIDAndUID = ByteArrayToHexString(DSFIDAndUID).Replace(" ", "");
                outputSet = 3;
                fCmdRet = StaticClassReaderA.SetGeneralOutput(ref readerAddr, ref outputSet, portIndex);
                SavePic(strDSFIDAndUID.Substring(2, 16));
                //MessageBox.Show(strDSFIDAndUID.Substring(2, 16));

                //ShutDown();
            }
        }
    }

private void SavePic(string text)
    {
        Cursor.Current = Cursors.WaitCursor;

        // Release any previous buffer
        if (m_ip != IntPtr.Zero)
        {
            Marshal.FreeCoTaskMem(m_ip);
            m_ip = IntPtr.Zero;
        }

        // here's where it crashes
        // capture image
        m_ip = cam.Click();
        Bitmap b = new Bitmap(cam.Width, cam.Height, cam.Stride, PixelFormat.Format24bppRgb, m_ip);
        cam.Dispose();

        // If the image is upsidedown
        b.RotateFlip(RotateFlipType.RotateNoneFlipY);
        pbPic.Image = b;

        Cursor.Current = Cursors.Default;
        MessageBox.Show("Here " + text);
    }


private void startCam()
    {
        const int VIDEODEVICE = 0; // zero based index of video capture device to use
        const int VIDEOWIDTH = 640; // Depends on video device caps
        const int VIDEOHEIGHT = 480; // Depends on video device caps
        const int VIDEOBITSPERPIXEL = 24; // BitsPerPixel values determined by device

        cam = new Capture(VIDEODEVICE, VIDEOWIDTH, VIDEOHEIGHT, VIDEOBITSPERPIXEL, pbPic);
    }


// method in capture class
 public IntPtr Click()
    {
        int hr;

        // get ready to wait for new image
        m_PictureReady.Reset();
        m_ipBuffer = Marshal.AllocCoTaskMem(Math.Abs(m_stride) * m_videoHeight);

        try
        {
            m_WantOne = true;

            // If we are using a still pin, ask for a picture
            if (m_VidControl != null)
            {
                // CRASHES HERE with : System.InvalidCastException was unhandled
                // Tell the camera to send an image
                hr = m_VidControl.SetMode(m_pinStill, VideoControlFlags.Trigger);
                DsError.ThrowExceptionForHR(hr);
            }

            // Start waiting
            if (!m_PictureReady.WaitOne(9000, false))
            {
                throw new Exception("Timeout waiting to get picture");
            }
        }
        catch
        {
            Marshal.FreeCoTaskMem(m_ipBuffer);
            m_ipBuffer = IntPtr.Zero;
            throw;
        }

        // Got one
        return m_ipBuffer;
    }

Pas de solution correcte

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top