how to load VideoCaptureDevice Form within aForge demo client

May 15, 2014 at 12:53 PM
Hi,
I am trying to call the VideoCaptureDevice form within the aForge demo but I am unable to do it. I seem not be able to get it up and running.

I need a way to change resolution or could you please suggest some other way of doing it. I also tried the resolution function but this is giving me some trouble too.

Any suggestions, please?

Thanks in advance
vancho
Coordinator
May 15, 2014 at 6:49 PM
I need more details. Something like a stacktrace of an exception, a screenshot with an error, some source code if you changed something within the demo.
Without it I have to look into a crystal ball...
May 15, 2014 at 7:53 PM
Hi,

Here is the code I am trying to use. Generally it is the same as in the demo. I am just having trouble adjusting the resolution as I want with the demo win form app. Please let me know what you think. Thanks in advance.

using System;
using System.Drawing;
using System.Threading;
using System.Windows.Forms;

using AForge.Video;
using ZXing;
using ZXing.Client;
using AForge.Video.DirectShow;
using System.Collections.Generic;
using ZXing.Common;
using ZXing.Client.Result;

namespace PhaseBarCodeScanner
{
public partial class frm_BarCodeScanner : Form
{
    private struct Device
    {
        public int Index;
        public string Name;
        public override string ToString()
        {
            return Name;
        }
    }

    private readonly CameraDevices camDevices;
    private Bitmap currentBitmapForDecoding;
    private readonly Thread decodingThread;
    private Result currentResult;
    private readonly Pen resultRectPen;

    public frm_BarCodeScanner()
    {
        InitializeComponent();

        camDevices = new CameraDevices();

        decodingThread = new Thread(DecodeBarcode);
        decodingThread.Start();

        pictureBox1.Paint += pictureBox1_Paint;
        resultRectPen = new Pen(Color.Green, 10);
    }

    void pictureBox1_Paint(object sender, PaintEventArgs e)
    {
        //Commented by Vancho on 05.14.2014
        //Performance issue. We do not needed.

        //try
        //{
        //    if (currentResult == null)
        //        return;

        //    if (currentResult.ResultPoints != null && currentResult.ResultPoints.Length > 0)
        //    {
        //        var resultPoints = currentResult.ResultPoints;
        //        var rect = new Rectangle((int)resultPoints[0].X, (int)resultPoints[0].Y, 1, 1);
        //        foreach (var point in resultPoints)
        //        {
        //            if (point.X < rect.Left)
        //                rect = new Rectangle((int)point.X, rect.Y, rect.Width + rect.X - (int)point.X, rect.Height);
        //            if (point.X > rect.Right)
        //                rect = new Rectangle(rect.X, rect.Y, rect.Width + (int)point.X - rect.X, rect.Height);
        //            if (point.Y < rect.Top)
        //                rect = new Rectangle(rect.X, (int)point.Y, rect.Width, rect.Height + rect.Y - (int)point.Y);
        //            if (point.Y > rect.Bottom)
        //                rect = new Rectangle(rect.X, rect.Y, rect.Width, rect.Height + (int)point.Y - rect.Y);
        //        }
        //        using (var g = pictureBox1.CreateGraphics())
        //        {
        //            g.DrawRectangle(resultRectPen, rect);
        //        }
        //    }
        //}
        //catch (Exception err)
        //{ err.Message.ToString(); }
    }

    protected override void OnLoad(EventArgs e)
    {
        base.OnLoad(e);
        LoadDevicesToCombobox();
    }

    protected override void OnClosing(System.ComponentModel.CancelEventArgs e)
    {
        base.OnClosing(e);
        if (!e.Cancel)
        {
            decodingThread.Abort();
            if (camDevices.Current != null)
            {
                camDevices.Current.NewFrame -= Current_NewFrame;
                if (camDevices.Current.IsRunning)
                {
                    camDevices.Current.SignalToStop();
                }
            }
        }
    }

    private void LoadDevicesToCombobox()
    {
        cmbDevice.Items.Clear();
        for (var index = 0; index < camDevices.Devices.Count; index++)
        {
            cmbDevice.Items.Add(new Device { Index = index, Name = camDevices.Devices[index].Name });
        }

        //select first device or web camera
        cmbDevice.SelectedIndex = 0;
    }

    private void cmbDevice_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (camDevices.Current != null)
        {
            camDevices.Current.NewFrame -= Current_NewFrame;
            if (camDevices.Current.IsRunning)
            {
                camDevices.Current.SignalToStop();
            }
        }

        camDevices.SelectCamera(((Device)(cmbDevice.SelectedItem)).Index);
        camDevices.Current.NewFrame += Current_NewFrame;
        camDevices.Current.Start();
    }

    private void Current_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        if (IsDisposed)
        {
            return;
        }

        try
        {
            if (currentBitmapForDecoding == null)
            {
                currentBitmapForDecoding = (Bitmap)eventArgs.Frame.Clone();
            }
            Invoke(new Action<Bitmap>(ShowFrame), eventArgs.Frame.Clone());
        }
        catch (ObjectDisposedException)
        {
            // not sure, why....
        }
    }

    private void ShowFrame(Bitmap frame)
    {
        if (pictureBox1.Width < frame.Width)
        {
            pictureBox1.Width = frame.Width;
        }
        if (pictureBox1.Height < frame.Height)
        {
            pictureBox1.Height = frame.Height;
        }
        pictureBox1.Image = frame;
    }

    private readonly IList<ResultPoint> resultPoints;
    private readonly IList<Result> lastResults;
    private EncodingOptions EncodingOptions { get; set; }
    private Type Renderer { get; set; }
    private bool TryMultipleBarcodes { get; set; }
    private bool TryOnlyMultipleQRCodes { get; set; }

    BarcodeReader reader = new BarcodeReader();
    private void DecodeBarcode()
    {
        while (true)
        {
            if (currentBitmapForDecoding != null)
            {
                var result = reader.Decode(currentBitmapForDecoding);
                if (result != null)
                {
                    Invoke(new Action<Result>(ShowResult), result);
                }
                currentBitmapForDecoding.Dispose();
                currentBitmapForDecoding = null;
            }
            Thread.Sleep(200);
        }
    }

    private void ShowResult(Result result)
    {
        currentResult = result;
        txtTypeWebCam.Text = result.BarcodeFormat.ToString();

        txtContentWebCam.Text = result.Text;
    }

    private void AForgeDemoForm_Load(object sender, EventArgs e)
    {

    }

    private void frm_BarCodeScanner_Resize(object sender, EventArgs e)
    {
        notifyIcon1.BalloonTipTitle = "PhaseGroup BarCode Scanner App, minimized to tray";
        notifyIcon1.BalloonTipText = "Minimized";

        if (FormWindowState.Minimized == this.WindowState)
        {
            notifyIcon1.Visible = true;
            notifyIcon1.ShowBalloonTip(500);
            this.Hide();
        }
        else if (FormWindowState.Normal == this.WindowState)
        {
            notifyIcon1.Visible = false;
        }
    }

    private void notifyIcon2_MouseDoubleClick(object sender, MouseEventArgs e)
    {
        this.Show();
        this.WindowState = FormWindowState.Normal;
    }

    private void btnStart_Click(object sender, EventArgs e)
    {
        camDevices.SelectCamera(((Device)(cmbDevice.SelectedItem)).Index);
        camDevices.Current.NewFrame += Current_NewFrame;
        camDevices.Current.Start();
    }

    private void btnStop_Click(object sender, EventArgs e)
    {
        camDevices.Current.SignalToStop();
        pictureBox1.Image = null;
    }

    private void btnQualityControl_Click(object sender, EventArgs e)
    {
        VideoCaptureDevice obj;
        FilterInfoCollection VideoCaptureDevices;

        VideoCaptureDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        obj = new VideoCaptureDevice(VideoCaptureDevices[0].MonikerString);
        obj.DisplayPropertyPage(IntPtr.Zero); //This will display a form with camera controls
    }

    private readonly BarcodeReader barcodeReader;
    private VideoCaptureDevice videoDevice;
    //private VideoCaptureDevice videoDevice;
    private VideoCapabilities[] videoCapabilities;
    private VideoCapabilities[] snapshotCapabilities;
    private void btnSettings_Click_1(object sender, EventArgs e)
    {
        //resolution
        if (videoDevice != null)
        {
            if ((videoCapabilities != null) && (videoCapabilities.Length != 0))
            {
                videoDevice.VideoResolution = videoCapabilities[3];
            }
        }
    }
}
}
May 15, 2014 at 8:11 PM
Not sure if someone received a notification but thanks.
Coordinator
May 16, 2014 at 7:40 PM
Edited May 16, 2014 at 7:41 PM
If you want to set the VideoResolution of the camera you have to restart the device.
AForge.Net documentation: VideoResolution property
    private void btnSettings_Click_1(object sender, EventArgs e)
    {
        //resolution
        if (videoDevice != null)
        {
            if ((videoCapabilities != null) && (videoCapabilities.Length != 0))
            {
                videoDevice.Stop();
                videoDevice.VideoResolution = videoCapabilities[3];
                videoDevice.Start();
            }
        }
    }