在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
分享一例c#生成二维码的代码,直接引用ThoughtWorks.QRCode.dll 类生成二维码,有需要的朋友参考下。 方法1,直接引用ThoughtWorks.QRCode.dll 类,生成二维码。 ThoughtWorks.QRCode.Codec.QRCodeEncoder encoder = new QRCodeEncoder(); encoder.QRCodeEncodeMode = QRCodeEncoder.ENCODE_MODE.BYTE;//编码方法(注意:BYTE能支持中文,ALPHA_NUMERIC扫描出来的都是数字) encoder.QRCodeScale = 4;//大小 encoder.QRCodeVersion = 0;//版本(注意:设置为0主要是防止编码的字符串太长时发生错误) 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; //获得文件路径 string localFilePath = sf.FileName.ToString(); if (sf.FileName != "") { string fileNameExt = localFilePath.Substring(localFilePath.LastIndexOf("\\") + 1);//获取文件名,不带路径 // newFileName = fileNameExt+DateTime.Now.ToString("yyyyMMdd") ;//给文件名后加上时间 string 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; 方法2,引用ZXing类库。 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; 方法: string content = "二维码信息"; ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300); Bitmap bitmap = toBitmap(byteMatrix); pictureBox1.Image = bitmap; SaveFileDialog sFD = new SaveFileDialog(); sFD.Filter = "保存图片(*.png) |*.png|所有文件(*.*) |*.*"; sFD.DefaultExt = "*.png|*.png"; sFD.AddExtension = true; if (sFD.ShowDialog() == DialogResult.OK) { if (sFD.FileName != "") { writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName); } }
if (this.openFileDialog1.ShowDialog() != DialogResult.OK) { return; } Image img = Image.FromFile(this.openFileDialog1.FileName); Bitmap bmap; try { bmap = new Bitmap(img); } catch (System.IO.IOException ioe) { MessageBox.Show(ioe.ToString()); return; } if (bmap == null) { MessageBox.Show("Could not decode image"); return; } LuminanceSource source = new RGBLuminanceSource(bmap, bmap.Width, bmap.Height); com.google.zxing.BinaryBitmap bitmap1 = new com.google.zxing.BinaryBitmap(new HybridBinarizer(source)); Result result; try { result = new MultiFormatReader().decode(bitmap1); } catch (ReaderException re) { MessageBox.Show(re.ToString()); return; } MessageBox.Show(result.Text); 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; }
|
请发表评论