第一种比较直接点
C#中直接引用ThoughtWorks.QRCode.dll 类,
            ThoughtWorks.QRCode.Codec.QRCodeEncoder encoder = new QRCodeEncoder();
              encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.ALPHA_NUMERIC;//编码方法
              encoder.QRCodeScale = 4;//大小
                encoder.QRCodeVersion = 4;//版本
                encoder.QRCodeErrorCorrect = QRCodeEncoder.ERROR_CORRECTION.M;
  String qrdata = "二维码信息";
System.Drawing.Bitmap bp = encoder.Encode(qrdata.ToString(), Encoding.GetEncoding("GB2312"));
                      Image image = bp;
                      Object oMissing = System.Reflection.Missing.Value;
                      pictureBox1.Image = bp;
保存二维码图片:
SaveFileDialog sf = new SaveFileDialog();
                      sf.Title = "选择保存文件位置";
                      sf.Filter = "保存图片(*.jpg) |*.jpg|所有文件(*.*) |*.*";
                      //设置默认文件类型显示顺序
                      sf.FilterIndex = 1;
                      //保存对话框是否记忆上次打开的目录
                      sf.RestoreDirectory = true;
                      if (sf.ShowDialog() == DialogResult.OK)
                      {
                              Image im = this.pictureBox1.Image;
                              //获得文件路径
                              localFilePath = sf.FileName.ToString();
                              if (sf.FileName != "")
                              {
                                      fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);//获取文件名,不带路径
                                    // newFileName = fileNameExt+DateTime.Now.ToString("yyyyMMdd")  ;//给文件名后加上时间
                                      FilePath = localFilePath.Substring(0, localFilePath.LastIndexOf("."));  //获取文件路径,带文件名,不带后缀
                                      string fn = sf.FileName;
                                      pictureBox1.Image.Save(FilePath +"-"+ DateTime.Now.ToString("yyyyMMdd") + ".jpg");
                                     
                              }
}
  //解析二维码信息
  // QRCodeDecoder decoder = new QRCodeDecoder();
  //  String decodedString = decoder.decode(new QRCodeBitmapImage(new Bitmap(pictureBox1.Image)));
    //this.label3.Text = decodedString; 

第二种,反正我是没有下载成功,网站总进不去

ZXing是一个开源Java类库用于解析多种格式的1D/2D条形码。目标是能够对QR编码、Data Matrix、UPC的1D条形码进行解码。 其提供了多种平台下的客户端包括:J2ME、J2SE和Android。

首先,在其官网http://code.google.com/p/zxing/上去下载源码

编译在其csharp目录下打开zxing.csproj文件,新建一个工程。在编译之前修改两个错误:

源代码中有两处UTF-8的问题,会导致乱码,

其一:com.google.zxing.qrcode.encoder.encoder类中的

internal const System.String DEFAULT_BYTE_MODE_ENCODING = "ISO-8859-1";

此处,将ISO-8859-1改为UTF-8

其二:com.google.zxing.qrcode.decoder.DecodedBitStreamParser类的成员

private const System.String UTF8 = "UTF8";

应将UTF8改为UTF-8


修改完之后,点重新生成编译出新的dll文件。

将zxing.dll考入到你自己所需要的工程中,并添加引用。

这样自己就可以试用了,主要是二维码的生成和识别两个用处:


二维码的生成:

  1. using com.google.zxing.qrcode;  
  2. using com.google.zxing;  
  3. using com.google.zxing.common;  
  4. using ByteMatrix = com.google.zxing.common.ByteMatrix;  
  5. using EAN13Writer = com.google.zxing.oned.EAN13Writer;  
  6. using EAN8Writer = com.google.zxing.oned.EAN8Writer;  
  7. using MultiFormatWriter = com.google.zxing.MultiFormatWriter;  
  8. private void button1_Click(object sender, EventArgs e)  
  9. {  
  10.   
  11. string content = textBox1.Text;  
  12. ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200);  
  13. Bitmap bitmap = toBitmap(byteMatrix);  
  14. pictureBox1.Image = bitmap;  
  15. //writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);  
  16. //SaveFileDialog sFD = new SaveFileDialog();  
  17. //sFD.DefaultExt = "*.png|*.png";  
  18. //sFD.AddExtension = true;  
  19. //try  
  20. //{  
  21. // if (sFD.ShowDialog() == DialogResult.OK)  
  22. // {  
  23. // }  
  24. //}  
  25. //catch (Exception ex)  
  26. //{  
  27. // MessageBox.Show(ex.Message);  
  28. //}  
  29.   
  30. }  
  31. public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)  
  32. {  
  33. Bitmap bmap = toBitmap(matrix);  
  34. bmap.Save(file, format);  
  35. }  
  36. public static Bitmap toBitmap(ByteMatrix matrix)   
  37. {   
  38. int width = matrix.Width;   
  39. int height = matrix.Height;   
  40. Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb);   
  41. for (int x = 0; x < width; x++)   
  42. {   
  43. for (int y = 0; y < height; y++)   
  44. {   
  45. bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF"));   
  46. }   
  47. }   
  48. return bmap;   
  49. }  
using com.google.zxing.qrcode;
using com.google.zxing;
using com.google.zxing.common;
using ByteMatrix = com.google.zxing.common.ByteMatrix;
using EAN13Writer = com.google.zxing.oned.EAN13Writer;
using EAN8Writer = com.google.zxing.oned.EAN8Writer;
using MultiFormatWriter = com.google.zxing.MultiFormatWriter;
private void button1_Click(object sender, EventArgs e)
{

string content = textBox1.Text;
ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 200, 200);
Bitmap bitmap = toBitmap(byteMatrix);
pictureBox1.Image = bitmap;
//writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);
//SaveFileDialog sFD = new SaveFileDialog();
//sFD.DefaultExt = "*.png|*.png";
//sFD.AddExtension = true;
//try
//{
// if (sFD.ShowDialog() == DialogResult.OK)
// {
// }
//}
//catch (Exception ex)
//{
// MessageBox.Show(ex.Message);
//}

}
public static void writeToFile(ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)
{
Bitmap bmap = toBitmap(matrix);
bmap.Save(file, format);
}
public static Bitmap toBitmap(ByteMatrix matrix) 
{ 
int width = matrix.Width; 
int height = matrix.Height; 
Bitmap bmap = new Bitmap(width, height, System.Drawing.Imaging.PixelFormat.Format32bppArgb); 
for (int x = 0; x < width; x++) 
{ 
for (int y = 0; y < height; y++) 
{ 
bmap.SetPixel(x, y, matrix.get_Renamed(x, y) != -1 ? ColorTranslator.FromHtml("0xFF000000") : ColorTranslator.FromHtml("0xFFFFFFFF")); 
} 
} 
return bmap; 
}


二维码的读取识别:

  1. private void button1_Click(object sender, EventArgs e)   
  2. {   
  3. if (this.openFileDialog1.ShowDialog() != DialogResult.OK)   
  4. {   
  5. return;   
  6. }   
  7. Image img = Image.FromFile(this.openFileDialog1.FileName);   
  8. Bitmap bmap;   
  9. try   
  10. {   
  11. bmap = new Bitmap(img);   
  12. }   
  13. catch (System.IO.IOException ioe)   
  14. {   
  15. MessageBox.Show(ioe.ToString());   
  16. return;   
  17. }   
  18. if (bmap == null)   
  19. {   
  20. MessageBox.Show("Could not decode image");   
  21. return;   
  22. }   
  23. LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height);   
  24. com.google.zxing.BinaryBitmap bitmap = new com.google.zxing.BinaryBitmap(new COMMON.HybridBinarizer(source));   
  25. Result result;   
  26. try   
  27. {   
  28. result = new MultiFormatReader().decode(bitmap);   
  29. }   
  30. catch(ReaderException re)   
  31. {   
  32. MessageBox.Show(re.ToString());   
  33. return;   
  34. }   
  35.   
  36. MessageBox.Show(result.Text);   
  37. }  


Logo

旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐