QRコードを生成する画像コード+画像透かし+エクスポート

8003 ワード

小さなプロジェクトをして、いくつかの基本的な情報に基づいて、QRコードのピクチャーを生成して、そしてQRコードのピクチャーのために1枚のピクチャーの透かしを加えて、それから導出します.
  
1、QRコードを生成する
ZXingは、Javaで実装された複数のフォーマットのオープンソースの1 D/2 Dバーコード画像処理ライブラリであり、他の言語に接続されたポートを含む.Zxingは、携帯電話の内蔵カメラを使ってバーコードのスキャンと復号を行うことができます.このプロジェクトで実現できるバーコードの符号化と復号化.QR符号化、Data Matrix、UPCの1 Dバーコードを復号できることが目標です.J 2 ME、J 2 SE、Androidなど、さまざまなプラットフォームのクライアントを提供しています.---QRcodeを復号するために使用されます.本文はzxingを引用する.dll、QRコードを生成します.
名前空間を参照:
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;

  
QRコードの生成:
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;
        }

 
画像透かし:
// 
        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;
        }

 
エクスポート:
まず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;
            }
        }