使用zxing类生成一维码、二维码实例
发布时间:2020-12-15 17:58:37 所属栏目:百科 来源:网络整理
导读:今天PHP站长网 52php.cn把收集自互联网的代码分享给大家,仅供参考。 ? 条形码在生活中使用的已经相当广泛了,不管是去书店买书,还是去超市买商品,都会用到条码,而且每一个条码中的信息都不尽相同,每一类的商品都有统
以下代码由PHP站长网 52php.cn收集自互联网 现在PHP站长网小编把它分享给大家,仅供参考
? 条形码在生活中使用的已经相当广泛了,不管是去书店买书,还是去超市买商品,都会用到条码,而且每一个条码中的信息都不尽相同,每一类的商品都有统一的条 码,当然条码的类型也有不同,比如有标准的UPC条码,也有Code39,Code128,EAN8等等好多好多。这些根据不同的需要而被广泛使用。
#region 引用程序集 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 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; #endregion namespace qr { public partial class Form1 : Form { public Form1() { // 初始化组件 InitializeComponent(); } // public static void writeToFile(ByteMatrix matrix,System.Drawing.Imaging.ImageFormat format,string file) //{ // Bitmap bmap = toBitmap(matrix); // bmap.Save(file,format); //} /// <summary> /// 绘制一维码、二维码位图 /// </summary> /// <param name="matrix"></param> /// <returns></returns> 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; } private void button1_Click_1(object sender,EventArgs e) { string content = textBox1.Text; int codeWidth = Convert.ToInt16(txtWidth.Text.Trim().ToString()); int codeHeight = Convert.ToInt16(txtHeight.Text.Trim().ToString()); // 生成二维码 ByteMatrix byteMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.QR_CODE,codeWidth,codeHeight); 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); //} } private void button2_Click(object sender,EventArgs e) { string content = textBox1.Text; // 生成一维码 ByteMatrix byteMatrix = new MultiFormatWriter().encode(content,BarcodeFormat.EAN_8,200,100); Bitmap bitmap = toBitmap(byteMatrix); pictureBox1.Image = bitmap; //Bitmap twobitmap = new Bitmap(bitmap.Width,bitmap.Height); //Graphics g = Graphics.FromImage(bitmap); //g.DrawString(content,Font,Brushes.Black,pictureBox1.); //pictureBox1.Image = bitmap; } } } 来自:http://blog.csdn.net/happylee6688/article/details/8446203 以上内容由PHP站长网【52php.cn】收集整理供大家参考研究 如果以上内容对您有帮助,欢迎收藏、点赞、推荐、分享。 (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
推荐文章
站长推荐
- Xcode 7 beta 4构建错误 – com.apple.CoreSimul
- 【XML】XML+DTD+SCHEMA基础整理(一)
- PostgreSQL 在视频、图片去重,图像搜索业务中的
- Vue-router 类似Vuex实现组件化开发的示例
- 官方的正则表达式组件 RegularExpressions (2) :
- react-native – 有没有办法强制WebView链接点击
- cocos2dx[3.2](20)——拖尾渐隐效果MotionStreak
- ruby-on-rails – 如何在ruby slim中提供空白
- 快速搭建vue2.0+boostrap项目的方法
- DOJO中国-DOJOCRM 源码发布啦
热点阅读