基本信息
源码名称:三种方式 识别条码 例子源码(zxing/zbar/thoughtworkqrcode)
源码大小:8.09M
文件格式:.rar
开发语言:C#
更新时间:2017-12-04
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):78630559

本次赞助数额为: 1 元 
   源码介绍

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Imaging;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using ZBar;
using ZXing;
using Image = System.Drawing.Image;

namespace 条码识别
{
    public partial class MainForm : Form
    {
        public MainForm()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Zbar条码识别
        /// </summary>
        private void ScanBarCodeZbar(string fileName)
        {
            Image primaryImage = Image.FromFile(fileName);
           // Bitmap pImg = MakeGrayscale3((Bitmap)primaryImage);
            using (ZBar.ImageScanner scanner = new ZBar.ImageScanner())
            {
                scanner.SetConfiguration(ZBar.SymbolType.QRCODE, ZBar.Config.Enable, 1);
                List<ZBar.Symbol> symbols = new List<ZBar.Symbol>();
                System.Diagnostics.Stopwatch watch = new Stopwatch();
                watch.Start();
                symbols = scanner.Scan((Image)primaryImage);
                watch.Stop();
                TimeSpan timeSpan = watch.Elapsed;

                if (symbols != null && symbols.Count > 0)
                {
                    string result = "扫描执行时间:"   timeSpan.TotalMilliseconds.ToString();
                    symbols.ForEach(s => result  = ",条码内容:"   s.Data   " 条码质量:"   s.Type   Environment.NewLine);
                    MessageBox.Show(result);
                }
            }
        }

        /// <summary>
        /// Zxing条码识别
        /// </summary>
        private void ScanBarCodeZxing(string fileName)
        {
            Image primaryImage = Image.FromFile(fileName);
            Bitmap pImg = MakeGrayscale3((Bitmap)primaryImage);
            BarcodeReader reader = new BarcodeReader();
            System.Diagnostics.Stopwatch watch = new Stopwatch();
            watch.Start();
            Result result = reader.Decode((Bitmap)pImg); //通过reader解码  
            watch.Stop();
            TimeSpan timeSpan = watch.Elapsed;
            MessageBox.Show("二维码值:" result ",扫描执行时间:"   timeSpan.TotalMilliseconds.ToString());

        }

        /// <summary>
        ///thoughtworkqrcode条码识别
        /// </summary>
        private void ScanBarCodeTWQR(string fileName)
        {
            Image primaryImage = Image.FromFile(fileName);

            Bitmap pImg = (Bitmap)primaryImage;// MakeGrayscale3((Bitmap)primaryImage);
            try
            {
                System.Diagnostics.Stopwatch watch = new Stopwatch();
                watch.Start();
                string decodedString = new QRCodeDecoder().decode(new QRCodeBitmapImage(pImg), Encoding.UTF8);
                watch.Stop();
                TimeSpan timeSpan = watch.Elapsed;
                MessageBox.Show("二维码值:"   decodedString   ",扫描执行时间:"   timeSpan.TotalMilliseconds.ToString());
            }
            catch (Exception ex)
            {
                MessageBox.Show("好难过");
            }
        }

        /// <summary>
        /// 处理图片灰度
        /// </summary>
        /// <param name="original"></param>
        /// <returns></returns>
        public static Bitmap MakeGrayscale3(Bitmap original)
        {
            //create a blank bitmap the same size as original
            Bitmap newBitmap = new Bitmap(original.Width, original.Height);

            //get a graphics object from the new image
            Graphics g = Graphics.FromImage(newBitmap);

            //create the grayscale ColorMatrix
            System.Drawing.Imaging.ColorMatrix colorMatrix = new System.Drawing.Imaging.ColorMatrix(
               new float[][] 
              {
                 new float[] {.3f, .3f, .3f, 0, 0},
                 new float[] {.59f, .59f, .59f, 0, 0},
                 new float[] {.11f, .11f, .11f, 0, 0},
                 new float[] {0, 0, 0, 1, 0},
                 new float[] {0, 0, 0, 0, 1}
              });

            //create some image attributes
            ImageAttributes attributes = new ImageAttributes();

            //set the color matrix attribute
            attributes.SetColorMatrix(colorMatrix);

            //draw the original image on the new image
            //using the grayscale color matrix
            g.DrawImage(original, new Rectangle(0, 0, original.Width, original.Height),
               0, 0, original.Width, original.Height, GraphicsUnit.Pixel, attributes);

            //dispose the Graphics object
            g.Dispose();
            return newBitmap;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            OpenFileDialog fileDialog = new OpenFileDialog();
            DialogResult dr = fileDialog.ShowDialog();

            if (dr == DialogResult.OK)
            {
                switch (comboBox1.Text.Trim().ToLower())
                {
                    case   "zbar":
                    {
                        ScanBarCodeZbar(fileDialog.FileName);
                        break;
                    }
                    case "zxing":
                    {
                        ScanBarCodeZxing(fileDialog.FileName);
                        break;
                    }
                    case "thoughtworkqrcode":
                    {
                        ScanBarCodeTWQR(fileDialog.FileName);
                        break;
                    }
                }
                
            }
        }
    }
}