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

标签 c# zxing aforge

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

到目前为止,我已经能够弄清楚如何从 URI 中解码 QRCode,但我想检测来自网络摄像头的 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;
        }
    }

关于c# - 如何在 C# 中使用 Aforge.NET 和 ZXing.NET 从 WebCamera 解码 QRCode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32634027/

相关文章:

c# - 基于 .Net WebApi 的类型和属性的自定义 Json 序列化

c# - 如何在 ASP.NET Core 2.2 中创建角色并将其分配给用户

ios - 无法更改从 zxingObjc 库为 ios 生成的条形码的颜色?

c# - 图像处理循环中的内存异常(需要比 GC.collect 更好的解决方案)

image-processing - 在 AForge.NET 中使用霍夫变换检测图像中的同心圆

c# - 之字形,IndexOutOfRangeException

c# - Silverlight C# 是否有将集合转换为文件以及相反的标准方法?

ios - 通常会在[ZXCapture dealloc]崩溃

android - ZXing找到二维码后如何重启相机

c# - 更新 PictureBox 时什么可能导致 ArgumentException?