上周五,閑逛Google Code的時候,查看了一下Google參與的開源項目,
在code.google.com上點擊"開源計划"然后點擊使用 Google 的代碼
即可查看Google所有的開源項目列表
翻了幾頁,發現一個zxing以前沒聽說過(孤陋寡聞了)
原來是個二維碼的識別程序庫,剛好前幾個月還困惑火車票上的防偽碼是怎么做的(才知道那種碼叫QRcode),
於是把代碼下載了下來,順便說一下,這個庫的示例數據是圖片,所以體積較大,大概130M,
我用tortoise SVN, 由於網速太慢,下了三個小時,
順便在網上也查了查相關資料,編譯了java版本的試了一下
效果不錯,可以使用,於是又把其.net版的工程編譯了一下,是一個dll,debug版的212K
參照javase中的GUIRunner.java代碼(很短的幾句代碼),在C#中實現了一個二維碼的讀取識別(QRCode)
參照javase中的MatrixToImageWriter.java(代碼也很短),實現了二維碼圖片的生成(QRCode)
貼一下代碼,這是識別的:
- using System;
- using System.Drawing;
- using System.Windows.Forms;
- using com.google.zxing;
- using COMMON = com.google.zxing.common;
- private void button1_Click(object sender, EventArgs e)
- {
- 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 bitmap = new com.google.zxing.BinaryBitmap(new COMMON.HybridBinarizer(source));
- Result result;
- try
- {
- result = new MultiFormatReader().decode(bitmap);
- }
- catch(ReaderException re)
- {
- MessageBox.Show(re.ToString());
- return;
- }
- MessageBox.Show(result.Text);
- }
生成圖片的代碼:
- private void button2_Click(object sender, EventArgs e)
- {
- SaveFileDialog sFD = new SaveFileDialog();
- sFD.DefaultExt = "*.png|*.png";
- sFD.AddExtension = true;
- try
- {
- if (sFD.ShowDialog() == DialogResult.OK)
- {
- string content = "我的電話號碼:110119112118;手機型號:Blackberry 8100";
- COMMON.ByteMatrix byteMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, 350, 350);
- writeToFile(byteMatrix, System.Drawing.Imaging.ImageFormat.Png, sFD.FileName);
- }
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- }
- public static void writeToFile(COMMON.ByteMatrix matrix, System.Drawing.Imaging.ImageFormat format, string file)
- {
- Bitmap bmap = toBitmap(matrix);
- bmap.Save(file, format);
- }
- public static Bitmap toBitmap(COMMON.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;
- }
源代碼中有兩處UTF-8的問題,會導致亂碼,
其一: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";
應將UTF8改為UTF-8
手機像素,理論上30萬的就可以,不過可能得有自動對焦的才行,
我的Blackberry 8100,130萬的像素,拍出來的是無法識別,有rim的示例程序,
但是j2me的還沒編譯成功,所以還沒辦法嘗試,提示找不到javax的一堆symbol(可能是wtk2.1版本有點舊)
生成的QRCode圖:
這張圖片里面的信息如下:
=======================分割線2012-06-27分割線======================================
很抱歉不能給留言的同學一一回復,參考代碼和編譯好的dll我放到github上了,
需要的請移步下載 https://github.com/F-Sidney/ZXingGUI
參考資料: