为什么我的StartCapture()方法只触发一次?

时间:2012-05-16 21:17:34

标签: bitmap system.drawing rfid motorola handheld

这是我第一次使用RFID motorola手持设备。此应用程序的目的是能够使用设备拍摄照片并保存每张照片,并触发“触发器”。出于某种原因,我只能触发一次StartCapture()方法,因此只保存一张图片,并且该方法将不会再次触发应用程序正在运行。任何具有Symbol.Imaging2类库经验的人的建议都会很棒。

    using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Forms;
using System.Drawing;
using System.Diagnostics;
using Symbol;
using Symbol.ResourceCoordination;
using Symbol.RFID3;
using Symbol.Imaging2;


namespace TagReaderMobile
{
    public partial class Form1 : Form
    {
       // private RFIDReader _reader = null;
        private Trigger _trigger = null;
        private Imaging2 Imager = new Imaging2(); //initializes available camera
        private int fileNumber = 0;
        //private TabPage _openTagPage = null;

        public Form1()
        {
            InitializeComponent();
        }

        public Trigger Trigger
        {
            get
            {
                if (_trigger == null)
                {
                    _trigger =
                        new Trigger(new TriggerDevice(TriggerID.ALL_TRIGGERS, new ArrayList()));
                }

                return _trigger;
            }
        }

        private void InitializeAllTriggers()
        {
            if (!Trigger.IsStage2InUse)
            {
                Trigger.Stage2Notify += (_trigger_Stage2Notify);
            }
        }

        private void DisconnectTriggers()
        {
            if (_trigger != null)
            {
                _trigger.Dispose();
                _trigger = null;
            }
        }

        private void _trigger_Stage2Notify(object sender, TriggerEventArgs e)
        {
            if (e.NewState == TriggerState.STAGE2)
            {
                UpdateTriggerState(true);
            }
            else
            {
                UpdateTriggerState(false);
            }
        }

        private void UpdateTriggerState(bool triggerPulled)
        {
            var workToDo = new Func<bool, bool>(x =>
            {
                if (x)
                {
                    picTrigger1.Image = Properties.Resources.go;
                    picTrigger2.Image = Properties.Resources.go;
                }
                else
                {
                    picTrigger1.Image = Properties.Resources.stop;
                    picTrigger2.Image = Properties.Resources.stop;
                }
                return true;
            });

            if (InvokeRequired)
            {
                Invoke(workToDo, new object[] { triggerPulled });
            }
            else
            {
                workToDo(triggerPulled);
            }
        }

        private void Form1_Activated(object sender, EventArgs e)
        {
            UpdateTriggerState(false);
            //InitializeAllTriggers();

            ConfigureImager();
        }

        private void Form1_Deactivate(object sender, EventArgs e)
        {
            DisconnectTriggers();

        }

        private void ConfigureImager() // Image Capture Method
        {
            if (Imager != null)
            {
                Imager.Enable();
                Imager.StartAcquisition(pictureBox1);
                Imager.OnStatus += new Imaging2.OnStatusHandler(Image_OnStatus);
                Imager.OnCapture += new Imaging2.OnCaptureHandler(Image_OnCapture);

                StartCapture();

            }
        }

        private void StartCapture()
        {
            Imager.Config.Activators.FreezeTimeout = 3000;
            Imager.Config.Activators.Triggers = new[] { Triggers.ALLTRIGGERS };
            IMGResults result = Imager.CaptureImage();
        }

        void Image_OnCapture(ImageData imageData)
        {
            Imager.Enable();
            Debug.WriteLine("Path=" + Program.AppPath);

            System.Drawing.Image bitmap = imageData.GetBitmap();
            bitmap.Save(@"\My Documents\My Pictures\Image" + fileNumber + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            try
            {
                bitmap.Save(@"\My Documents\My Pictures\Image" + fileNumber + ".jpg", System.Drawing.Imaging.ImageFormat.Jpeg);
            }
            catch(Exception e)
            {
                Debug.WriteLine(e.Message); 
                Debug.WriteLine(e.StackTrace);
            }
            fileNumber++;
            Debug.WriteLine("OnCapture: " + imageData.Results.ToString());
        }

        private void Image_OnStatus(StatusData statusData)
        {
            Debug.WriteLine("Status: " + statusData.EventType);
        }
    }
}

1 个答案:

答案 0 :(得分:1)

因为触发触发器时会调用ConfigureImager。第一次创建Imager对象并调用StartCapture。第二次,Imager已经存在,因此未创建,但由于StartCapture阻止,也未调用if