如何在C#中使用Aforge.NET和ZXing.NET从WebCamera解码QRCode

我正在尝试开发一个WindowsForm应用程序,它将使用webcamera来检测QRCode并解码消息.为此我使用AForge.NET和ZXing.NET.

到目前为止,我已经能够弄清楚如何从URI解码QRCode,但我想从webcamera中检测QRCode并对其进行解码.

以下是我的代码示例.

public String Decode(Uri uri)
{
    Bitmap image;
    try
    {
        image = (Bitmap)Bitmap.FromFile(uri.LocalPath);
    }
    catch (Exception ex)
    {
        throw new FileNotFoundException("Resource not found: " + uri);
    }

    using (image)
    {
        String text = "";
        LuminanceSource source = new BitmapLuminanceSource(image);

        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        Result result = new MultiFormatReader().decode(bitmap);

        if (result != null)
        {
            text = result.Text;
            return text;
        }

        text = "Provided QR couldn't be read.";
        return text;
    }
}

解决方法:

    private FilterInfoCollection videoDevices;
    private VideoCaptureDevice videoSource;
    private Bitmap capturedImage;
    private String message = "";

    public FormQRCodeScanner()
    {
        InitializeComponent();
    }

    private void FormQRCodeScanner_Load(object sender, EventArgs e)
    {
        videoDevices = new FilterInfoCollection(FilterCategory.VideoInputDevice);
        foreach (FilterInfo device in videoDevices)
        {
            comboBoxCameraSource.Items.Add(device.Name);
        }
        comboBoxCameraSource.SelectedIndex = 0;

        videoSource = new VideoCaptureDevice();

        buttonStartStop.Text = "Start";
        buttonCapture.Enabled = false;
        buttonDecode.Enabled = false;
    }

    private void FormQRCodeScanner_FormClosing(object sender, FormClosingEventArgs e)
    {
        if (videoSource.IsRunning)
        {
            videoSource.Stop();
        }
    }

    void videoSource_NewFrame(object sender, NewFrameEventArgs eventArgs)
    {
        Bitmap image = (Bitmap) eventArgs.Frame.Clone();
        pictureBoxSource.Image = image;
    }

    private void buttonStartStop_Click(object sender, EventArgs e)
    {
        if (videoSource.IsRunning)
        {
            videoSource.Stop();
            pictureBoxSource.Image = null;
            pictureBoxCaptured.Image = null;
            pictureBoxSource.Invalidate();
            pictureBoxCaptured.Invalidate();

            buttonStartStop.Text = "Start";
            buttonCapture.Enabled = false;
            buttonDecode.Enabled = false;
        }
        else
        {
            videoSource = new VideoCaptureDevice(videoDevices[comboBoxCameraSource.SelectedIndex].MonikerString);
            videoSource.NewFrame += new NewFrameEventHandler(videoSource_NewFrame);
            videoSource.Start();

            buttonStartStop.Text = "Stop";
            buttonCapture.Enabled = true;
            buttonDecode.Enabled = true;
        }
    }

    private void buttonCapture_Click(object sender, EventArgs e)
    {
        if (videoSource.IsRunning)
        {
            pictureBoxCaptured.Image = (Bitmap)pictureBoxSource.Image.Clone();
            capturedImage = (Bitmap)pictureBoxCaptured.Image;
        }
    }

    private void buttonDecode_Click(object sender, EventArgs e)
    {
        if (capturedImage != null)
        {
            ExtractQRCodeMessageFromImage(capturedImage);
            richTextBoxMessage.Text = message;
            richTextBoxMessage.ReadOnly = true;
        }
    }

    private string ExtractQRCodeMessageFromImage(Bitmap bitmap)
    {
        try
        {
            BarcodeReader reader = new BarcodeReader
                (null, newbitmap => new BitmapLuminanceSource(bitmap), luminance => new GlobalHistogramBinarizer(luminance));

            reader.AutoRotate = true;
            reader.TryInverted = true;
            reader.Options = new DecodingOptions { TryHarder = true };

            var result = reader.Decode(bitmap);

            if (result != null)
            {
                message = result.Text;
                return message;
            }
            else
            {
                message = "QRCode couldn't be decoded.";
                return message;
            }
        }
        catch (Exception ex)
        {
            message = "QRCode couldn't be detected.";
            return message;
        }
    }
上一篇:oracle的例程


下一篇:c# – 识别一个简单的字母