【问题标题】:Generating QR codes生成二维码
【发布时间】:2011-08-20 20:15:11
【问题描述】:

我正在编写一个将生成二维码的应用程序。

大部分的编程逻辑都实现了。

该过程的下一步是生成二维码图像。

最简单的二维码是基于一个 21x21 的网格,我必须在其中制作一个黑色/白色的瓷砖 (1x1)。

欲了解更多信息:http://www.thonky.com/qr-code-tutorial/part-3-mask-pattern/

最好的方法是什么。

我需要:

  1. 在应用程序中显示代码预览

  2. 让用户可以选择将二维码保存为图片(我认为是.jpg)。

即如何制作可以像上面那样构建的图像以及如何保存?

【问题讨论】:

标签: vb.net image qr-code


【解决方案1】:

我个人会尝试使用 Google 图表服务来生成二维码图像。简单易行。这是来自 Google 网站的示例图片。

https://chart.googleapis.com/chart?chs=150x150&cht=qr&chl=Hello%20world&choe=UTF-8

在此处查看文档: http://code.google.com/apis/chart/infographics/docs/qr_codes.html

【讨论】:

  • 不是我的问题的真正答案。下一步是什么?建议使用正则表达式/jQuery? ;)
  • 根据链接.. 这个api被this弃用了
【解决方案2】:

为了创建二维码图像,您需要在应用程序中生成位图。执行此操作的示例代码是:

'Create a new QR bitmap image  
Dim bmp As New Bitmap(21, 21)

'Get the graphics object to manipulate the bitmap
Dim gr As Graphics = Graphics.FromImage(bmp)

'Set the background of the bitmap to white
gr.FillRectangle(Brushes.White, 0, 0, 21, 21)

'Draw position detection patterns
'Top Left
gr.DrawRectangle(Pens.Black, 0, 0, 6, 6)
gr.FillRectangle(Brushes.Black, 2, 2, 3, 3)

'Top Right
gr.DrawRectangle(Pens.Black, 14, 0, 6, 6)
gr.FillRectangle(Brushes.Black, 2, 16, 3, 3)

'Bottom Left
gr.DrawRectangle(Pens.Black, 0, 14, 6, 6)
gr.FillRectangle(Brushes.Black, 16, 2, 3, 3)


'*** Drawing pixels is done off the bitmap object, not the graphics object

'Arbitrary black pixel
bmp.SetPixel(8, 14, Color.Black)

'Top timing pattern
bmp.SetPixel(8, 6, Color.Black)
bmp.SetPixel(10, 6, Color.Black)
bmp.SetPixel(12, 6, Color.Black)

'Left timing pattern
bmp.SetPixel(6, 8, Color.Black)
bmp.SetPixel(6, 10, Color.Black)
bmp.SetPixel(6, 12, Color.Black)

'Add code here to set the rest of the pixels as needed

为了向最终用户显示图像,您可以使用 PictureBox 控件:

Me.PictureBox1.Image = bmp

最后,为了保存位图,您可以在其上调用 save 函数:

bmp.Save("C:\QR.jpg", Drawing.Imaging.ImageFormat.Jpeg)

【讨论】: