zoukankan      html  css  js  c++  java
  • 引用ZXing生成二维码

    1、生成二维码

    ZXing是一个开放源码的,用Java实现的多种格式的1D/2D条码图像处理库,它包含了联系到其他语言的端口。

    Zxing可以实现使用手机的内置的摄像头完成条形码的扫描及解码。本文引用zxing.dll,生成二维码。

    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;
    using System.IO;
    using System.Collections;
    
    
    private void btnGenerate_Click(object sender, EventArgs e)
            {
                ByteMatrix byteMatrix;
                string content = this.textBox1.Text;
    
                if (!string.IsNullOrEmpty(content))
                {
                    byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 300, 300);
                    bitmap = ToBitmap(byteMatrix);
                }
    
                this.pictureBox1.Image = bitmap;
                mapCreate = bitmap;
            }
    
            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;
            }
    View Code

    2、加图片水印

    //图片水印处理方法
            private Bitmap ImageWatermark(Bitmap map, string waterpath)
            {
                Image waterimg = Image.FromFile(waterpath);
    
                //添加水印
                Graphics g = Graphics.FromImage(map);
    
                //获取水印位置设置
                ArrayList loca = new ArrayList();
                int x = 0;
                int y = 0;
                x = map.Width / 2 - waterimg.Width / 2;
                y = map.Height / 2 - waterimg.Height / 2;
                loca.Add(x);
                loca.Add(y);
    
                //g.DrawImage(waterimg, new Rectangle(int.Parse(loca[0].ToString()), int.Parse(loca[1].ToString()), waterimg.Width, waterimg.Height));
                g.DrawImage(waterimg, new Rectangle(int.Parse(loca[0].ToString()), int.Parse(loca[1].ToString()), 75, 75));
    
                return map;
            }
    View Code

    3、导出

    //先添加 saveFileDialog控件 
    
    private void btnOut_Click(object sender, EventArgs e)
            {
                try
                {
                    saveFileDialog1.ShowDialog();
                    string fileName = saveFileDialog1.FileName;
    
                    if (fileName != null)
                    {
                        mapCreate.Save(fileName);//mapCreate是bitmap格式的图片
                    }
                }
                catch (Exception ex)
                {                
                    throw;
                }
            }
    View Code
  • 相关阅读:
    推荐阅读20100603
    [ASP.NET4之旅]Circular file references are not allowed
    满园尽是503,记曾经的一次IIS 7性能考验
    VS2010小Bug:找不到System.Web.Extensions.dll引用
    VS2010奇异Bug:三个中文符号在CSS文件中轻松让VS2010崩溃
    推荐阅读20100528
    Windows平台网站文件同步备份解决方案——cwRsyn
    推荐阅读20100803
    在IE8中使用建行企业网银的解决方法
    博客园已经用上NorthScale Memcached Server
  • 原文地址:https://www.cnblogs.com/yuan-jun/p/6628621.html
Copyright © 2011-2022 走看看