小男孩‘自慰网亚洲一区二区,亚洲一级在线播放毛片,亚洲中文字幕av每天更新,黄aⅴ永久免费无码,91成人午夜在线精品,色网站免费在线观看,亚洲欧洲wwwww在线观看

分享

C# 生成二維碼和zxing類庫(kù)

 務(wù)實(shí)耐久 2014-01-19
    在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|所有文件(*.*) |*.*";
            //設(shè)置默認(rèn)文件類型顯示順序
            sf.FilterIndex = 1;
            //保存對(duì)話框是否記憶上次打開的目錄
            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")  ;//給文件名后加上時(shí)間
                    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類庫(kù)。
  ZXing是一個(gè)開源Java類庫(kù)用于解析多種格式的1D/2D條形碼。目標(biāo)是能夠?qū)R編碼、Data Matrix、UPC的1D條形碼進(jìn)行解碼。于此同時(shí),它同樣提供 cpp,ActionScript,android,iPhone,rim,j2me,j2se,jruby,C#等方式的類庫(kù)。zxing類庫(kù)的作用主 要是解碼,是目前開源類庫(kù)中解碼能力比較強(qiáng)的(商業(yè)的另說,不過對(duì)于動(dòng)輒成千上萬(wàn)的類庫(kù)授權(quán)費(fèi)用,的確很值)。

到谷歌code下載相應(yīng)的代碼
1.下載zxing最新的包
到zxing的主頁(yè): http://code.google.com/p/zxing/
找到其中的CSharp文件夾,在vs中打開并編譯,將obj下debug中的zxing.dll復(fù)制并粘帖到你的項(xiàng)目中的bin文件目錄下,

右擊添加項(xiàng)目引用。將zxing.dll引用到項(xiàng)目中,就可以在需要的地方使用了。
源代碼中有兩處UTF-8的問題,會(huì)導(dǎo)致中文出現(xiàn)亂碼(編譯.dll之前修改)
其一: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";
應(yīng)將UTF8改為UTF-8

生成代碼:
//引用
 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;
    }

    本站是提供個(gè)人知識(shí)管理的網(wǎng)絡(luò)存儲(chǔ)空間,所有內(nèi)容均由用戶發(fā)布,不代表本站觀點(diǎn)。請(qǐng)注意甄別內(nèi)容中的聯(lián)系方式、誘導(dǎo)購(gòu)買等信息,謹(jǐn)防詐騙。如發(fā)現(xiàn)有害或侵權(quán)內(nèi)容,請(qǐng)點(diǎn)擊一鍵舉報(bào)。
    轉(zhuǎn)藏 分享 獻(xiàn)花(0

    0條評(píng)論

    發(fā)表

    請(qǐng)遵守用戶 評(píng)論公約

    類似文章 更多