C#实现 读取二维码与生成二维码存储到文件



using Spire.Barcode;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using Zen.Barcode;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;

namespace _2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

string filepath;
private void button1_Click(object sender, EventArgs e)
{
//QRCodeVersion = 0;
if (textBox1.Text.Trim() != "")
{
string enCodeString = textBox1.Text;
QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
qrCodeEncoder.QRCodeScale = 4;
qrCodeEncoder.QRCodeVersion = 0;
qrCodeEncoder.QRCodeForegroundColor = Color.Green;
pictureBox1.Image = qrCodeEncoder.Encode(enCodeString, Encoding.GetEncoding("utf-8"));
}
}

private void button2_Click(object sender, EventArgs e)
{
if (pictureBox1.Image != null)
{
SaveFileDialog s = new SaveFileDialog();
s.Title = "保存二维码图片";
s.Filter = "图片文件(*.jpg)|*.jpg";
if (s.ShowDialog() == DialogResult.OK)
try
{
pictureBox1.Image.Save(s.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
MessageBox.Show("保存成功");
}
catch { MessageBox.Show("保存失败"); }
}
}

private void button3_Click(object sender, EventArgs e)
{
OpenFileDialog p = new OpenFileDialog();
p.Title = "请选择二维码文件";
p.Filter = "图片文件(*.jpg)|*.jpg";
p.Multiselect = false;
if (p.ShowDialog() == DialogResult.OK)
{
filepath = p.FileName;
textBox2.Text = filepath;
System.Threading.Thread t = new System.Threading.Thread(ss);
t.IsBackground = true;
t.Start();

}
}
private void ss()
{
if (filepath != "")
{
string tt = "";
try
{
Invoke((EventHandler)delegate
{
button3.Enabled = false;
button3.Text = "识别中···";
pictureBox2.Image = new Bitmap(filepath);
});
//pictureBox1.Size = new Size(new Bitmap(filepath).Size.Width, new Bitmap(filepath).Size.Height);
QRCodeDecoder qrDecoder = new QRCodeDecoder();
string txtMsg = qrDecoder.decode(new QRCodeBitmapImage(new Bitmap(pictureBox2.Image)), Encoding.UTF8);
tt = txtMsg;
}
catch { tt = "识别失败"; }
Invoke((EventHandler)delegate
{
textBox3.Text = tt;
button3.Enabled = true;
button3.Text = "从文件中读取";
});
}
System.Threading.Thread.CurrentThread.Abort();
}

private void button4_Click(object sender, EventArgs e)
{
#region 不同方法 生成条形码
//if (textBox4.Text == string.Empty)
//{
// MessageBox.Show("输入内容不能为空!");
// return;
//}
//EncodingOptions encodeOption = new EncodingOptions();
设置宽和高
//encodeOption.Height = 130;
//encodeOption.Width = 240;
//BarcodeWriter wr = new BarcodeWriter();
//wr.Options = encodeOption;
条形码:根据自己的需要选择条形码格式
//wr.Format = BarcodeFormat.EAN_13;
生成条形码
//Bitmap image = wr.Write(textBox1.Text);
显示
//pictureBox1.Image = image;

//BarcodeSettings.ApplyKey("------");
//BarcodeSettings bs = new BarcodeSettings()
//{
// Data = textBox4.Text,
// ShowCheckSumChar = false,
// TopTextColor = Color.Red,
// ShowTopText = false,
// ShowTextOnBottom = true
//};

Generate the barcode based on the this.barCodeControl1
//BarCodeGenerator generator = new BarCodeGenerator(bs);
//Image barcode = generator.GenerateImage();
//pictureBox3.Image = barcode;
save the barcode as an image
//barcode.Save("barcode.jpg");
#endregion
Code128BarcodeDraw barcode128 = BarcodeDrawFactory.Code128WithChecksum;
Image img = barcode128.Draw(textBox4.Text, 40);
pictureBox3.Image = img;

}

private void button5_Click(object sender, EventArgs e)
{
if (pictureBox3.Image != null)
{
SaveFileDialog s = new SaveFileDialog();
s.Title = "保存条形码图片";
s.Filter = "图片文件(*.jpg)|*.jpg";
if (s.ShowDialog() == DialogResult.OK)
try
{
pictureBox3.Image.Save(s.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
MessageBox.Show("保存成功");
}
catch { MessageBox.Show("保存失败"); }
}
}

private void button6_Click(object sender, EventArgs e)
{
OpenFileDialog p = new OpenFileDialog();
p.Title = "请选择条形码文件";
p.Filter = "图片文件(*.jpg)|*.jpg";
p.Multiselect = false;
if (p.ShowDialog() == DialogResult.OK)
{
filepath = p.FileName;
textBox5.Text = filepath;
}
//pictureBox4.Image = new Bitmap(filepath);
//Bitmap bmp = new Bitmap(filepath);
//BarcodeReader br = new BarcodeReader();

//Result result = br.Decode(bmp);
//if (result == null)
//{
// MessageBox.Show("读取失败");
//}
//else
//{
// //读取成功
// textBox6.Text = result.Text;
//}
IBarcodeReader reader = new BarcodeReader();
// load a bitmap
var barcodeBitmap = new Bitmap(filepath);
// detect and decode the barcode inside the bitmap
var result = reader.Decode(barcodeBitmap);
//MessageBox.Show(result.Text.ToString());
// do something with the result
if (result != null)
{
// txtDecoderType.Text = result.BarcodeFormat.ToString();
//txtDecoderContent.Text = result.Text;
}



}
}
}
using Spire.Barcode;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using ThoughtWorks.QRCode.Codec;
using ThoughtWorks.QRCode.Codec.Data;
using Zen.Barcode;
using ZXing;
using ZXing.Common;
using ZXing.QrCode;

namespace _2
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        string filepath;
        private void button1_Click(object sender, EventArgs e)
        {
            //QRCodeVersion = 0;
            if (textBox1.Text.Trim() != "")
            {
                string enCodeString = textBox1.Text;
                QRCodeEncoder qrCodeEncoder = new QRCodeEncoder();
                qrCodeEncoder.QRCodeScale = 4;
                qrCodeEncoder.QRCodeVersion = 0;
                qrCodeEncoder.QRCodeForegroundColor = Color.Green;
                pictureBox1.Image = qrCodeEncoder.Encode(enCodeString, Encoding.GetEncoding("utf-8"));
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (pictureBox1.Image != null)
            {
                SaveFileDialog s = new SaveFileDialog();
                s.Title = "保存二维码图片";
                s.Filter = "图片文件(*.jpg)|*.jpg";
                if (s.ShowDialog() == DialogResult.OK)
                    try
                    {
                        pictureBox1.Image.Save(s.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                        MessageBox.Show("保存成功");
                    }
                    catch { MessageBox.Show("保存失败"); }
            }
        }

        private void button3_Click(object sender, EventArgs e)
        {
            OpenFileDialog p = new OpenFileDialog();
            p.Title = "请选择二维码文件";
            p.Filter = "图片文件(*.jpg)|*.jpg";
            p.Multiselect = false;
            if (p.ShowDialog() == DialogResult.OK)
            {
                filepath = p.FileName;
                textBox2.Text = filepath;
                System.Threading.Thread t = new System.Threading.Thread(ss);
                t.IsBackground = true;
                t.Start();

            }
        }
        private void ss()
        {
            if (filepath != "")
            {
                string tt = "";
                try
                {
                    Invoke((EventHandler)delegate
                    {
                        button3.Enabled = false;
                        button3.Text = "识别中···";
                        pictureBox2.Image = new Bitmap(filepath);
                    });
                    //pictureBox1.Size = new Size(new Bitmap(filepath).Size.Width, new Bitmap(filepath).Size.Height); 
                    QRCodeDecoder qrDecoder = new QRCodeDecoder();
                    string txtMsg = qrDecoder.decode(new QRCodeBitmapImage(new Bitmap(pictureBox2.Image)), Encoding.UTF8);
                    tt = txtMsg;
                }
                catch { tt = "识别失败"; }
                Invoke((EventHandler)delegate
               {
                   textBox3.Text = tt;
                   button3.Enabled = true;
                   button3.Text = "从文件中读取";
               });
            }
            System.Threading.Thread.CurrentThread.Abort();
        }

        private void button4_Click(object sender, EventArgs e)
        {
            #region 不同方法 生成条形码
            //if (textBox4.Text == string.Empty)
            //{
            //    MessageBox.Show("输入内容不能为空!");
            //    return;
            //}
            //EncodingOptions encodeOption = new EncodingOptions();
            设置宽和高 
            //encodeOption.Height = 130;
            //encodeOption.Width = 240;
            //BarcodeWriter wr = new BarcodeWriter();
            //wr.Options = encodeOption;
            条形码:根据自己的需要选择条形码格式 
            //wr.Format = BarcodeFormat.EAN_13;
            生成条形码 
            //Bitmap image = wr.Write(textBox1.Text);
            显示 
            //pictureBox1.Image = image;

            //BarcodeSettings.ApplyKey("------");
            //BarcodeSettings bs = new BarcodeSettings()
            //{
            //    Data = textBox4.Text,
            //    ShowCheckSumChar = false,
            //    TopTextColor = Color.Red,
            //    ShowTopText = false,
            //    ShowTextOnBottom = true
            //};

            Generate the barcode based on the this.barCodeControl1
            //BarCodeGenerator generator = new BarCodeGenerator(bs);
            //Image barcode = generator.GenerateImage();
            //pictureBox3.Image = barcode;
            save the barcode as an image
            //barcode.Save("barcode.jpg");
            #endregion
            Code128BarcodeDraw barcode128 = BarcodeDrawFactory.Code128WithChecksum;
            Image img = barcode128.Draw(textBox4.Text, 40);
            pictureBox3.Image = img;

        }

        private void button5_Click(object sender, EventArgs e)
        {
            if (pictureBox3.Image != null)
            {
                SaveFileDialog s = new SaveFileDialog();
                s.Title = "保存条形码图片";
                s.Filter = "图片文件(*.jpg)|*.jpg";
                if (s.ShowDialog() == DialogResult.OK)
                    try
                    {
                        pictureBox3.Image.Save(s.FileName, System.Drawing.Imaging.ImageFormat.Jpeg);
                        MessageBox.Show("保存成功");
                    }
                    catch { MessageBox.Show("保存失败"); }
            }
        }

        private void button6_Click(object sender, EventArgs e)
        {
            OpenFileDialog p = new OpenFileDialog();
            p.Title = "请选择条形码文件";
            p.Filter = "图片文件(*.jpg)|*.jpg";
            p.Multiselect = false;
            if (p.ShowDialog() == DialogResult.OK)
            {
                filepath = p.FileName;
                textBox5.Text = filepath;
            }
            //pictureBox4.Image = new Bitmap(filepath);
            //Bitmap bmp = new Bitmap(filepath);
            //BarcodeReader br = new BarcodeReader();

            //Result result = br.Decode(bmp);
            //if (result == null)
            //{
            //    MessageBox.Show("读取失败");
            //}
            //else
            //{
            //    //读取成功 
            //    textBox6.Text = result.Text;
            //}
            IBarcodeReader reader = new BarcodeReader();
            // load a bitmap
            var barcodeBitmap  = new Bitmap(filepath);
            // detect and decode the barcode inside the bitmap
            var result = reader.Decode(barcodeBitmap);
            //MessageBox.Show(result.Text.ToString());
            // do something with the result
            if (result != null)
            {
               // txtDecoderType.Text = result.BarcodeFormat.ToString();
                //txtDecoderContent.Text = result.Text;
            }


        }
    }
}

你可能感兴趣的:(C#实现 读取二维码与生成二维码存储到文件)