Creating QR codes using Google's Web API

Creating QR codes using Google's Web API

Google Charts can be queried via POST (see here for details: https://developers.google.com/chart/image/docs/post_requests?csw=1), so we have to: a) query the remote server, specifically the POST parameters (described later), b) get the server's response (a PNG image), c) use it for us, that is, draw it (the image) how we want.

So, open a project in Visual Studio and add a new User Control. Set the BorderStyle property to Fixed3D and DoubleBuffered to True (to avoid flickering when the control refreshes itself).

Using the Code

The standard URL we will query is as follows: http://chart.googleapis.com/chart?chs={WIDTH}x{HEIGHT}&cht=qr&chl={DATA} (the parameters in curly brackets will be replaced by the actual parameters). chs is determined by the specific QR code (width x height) and chl contains the data represented by the barcode. The parameters related to the size of the barcode are easy to derive from the properties of our control (the standard control naturally has width and height), but we also need to create a new variable to store a fixed-length text, which represents the data displayed by our QR code.

In the UserControl, we define the standard URI as a constant, define the Data Property, and an internal variable to store the local context data:

  1. Const _GOOGLE_URL As String = "http://chart.googleapis.com/chart?chs= [This link is external to TechNet Wiki. It will open in a new window.] {WIDTH}x{HEIGHT}&cht=qr&chl={DATA}"  
  2. Dim_DATA As String = String.Empty
  3.    
  4. Property Data As String
  5. Get
  6. Return _DATA
  7. End Get
  8. Set(value As String)
  9. _DATA = value
  10. End Set
  11. End Property

When we use Control, Data Property is available in both code view and design view:

Now we can construct a URL URI with the request parameters, which requires assembling the data by encoding it before the page request. We need to make sure that there are no special characters that will interfere with our query. I wrote a private method to do this. Calling it can get a parameter-encoded URI (thanks to the WebUtility.UrlEncode function).

  1. Private Function getQRURI() As String
  2. Dim _qrAddr As String = _GOOGLE_URL.Replace( "{WIDTH}" , Me.Width.ToString).Replace( "{HEIGHT}" , Me.Height.ToString)
  3. _qrAddr = _qrAddr.Replace( "{DATA}" , WebUtility.UrlEncode(_DATA))
  4.    
  5. Return _qrAddr
  6. End Function

Once the data parameters need to be included, we will replace the two parameters {WIDTH} and {HEIGHT} in the code with our size (for WebUtility.UrlEncode, see here.

<

Now you can get the QR code image from the remote server, because we have used the QRCode control to generate a QR code image cache on the server waiting for your request. Since I want to use the standard OnPaint drawing method directly (which can better use PaintEventArgs), I will overload it and add my own code:

  1. Protected Overrides Sub OnPaint(e As PaintEventArgs)
  2. MyBase.OnPaint(e)
  3. If _DATA Is Nothing Then Exit Sub
  4.    
  5. Dim client As New WebClient()
  6. Dim bytes() As Byte = client.DownloadData(getQRURI())
  7. client.Dispose()
  8.    
  9. Dim memStream As New IO.MemoryStream(bytes)
  10. Dim bmp As Bitmap = Bitmap.FromStream(memStream)
  11. memStream.Dispose()
  12.    
  13. e.Graphics.DrawImage(bmp, 0 , 0 )
  14. End Sub

Call the standard drawing operation. Next, we check if there is a data request (there is another method), and we use a new WebClient instance to handle the remote request. By calling the download data processed by the format URI method, we fill a bytes array and then build a QR Code image in PNG format.

Variables of type Picture can be initialized by reading from a stream (just like when we open a local picture, there will be a copy of the local stream). Now that we have our bytes in memory, we can declare an array-based MemoryStream and use it as the source of the bitmap. At this point , to achieve a fully working bitmap, we can use the variable e, which the OnPaint event can access itself, to draw the image at the position [0;0] that we control.

After compiling our project, the QRBox will appear in the Toolbox, ready to be used on the Form.

Using it is very simple, you only need to set its data and properties, as well as the callback to control the refresh.

The following simple Form example will show how it works. I have added a QrBox, a standard TextBox and Button to my Form.

When the user presses the "Make" button, we will read the TextBox text, transfer it to the QrBox Data Property, and trigger the refresh method. To start the remote query for Google Charts, the code generated by pressing the button will be as simple as this:

  1. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  2. QrBox1.Data = TextBox1.Text
  3. QrBox1.Refresh()
  4. End Sub

The complete code of UserControl

The complete code of QrBox UserControl is as follows:

  1. Imports System.Net
  2.    
  3. Public Class QRBox
  4. Const _GOOGLE_URL As String = "http://chart.googleapis.com/chart?chs={WIDTH}x{HEIGHT}&cht=qr&chl={DATA}"  
  5. Dim_DATA As String = String.Empty
  6.    
  7. Property Data As String
  8. Get
  9. Return _DATA
  10. End Get
  11. Set(value As String)
  12. _DATA = value
  13. End Set
  14. End Property
  15.    
  16. Private Function getQRURI() As String
  17. Dim _qrAddr As String = _GOOGLE_URL.Replace( "{WIDTH}" , Me.Width.ToString).Replace( "{HEIGHT}" , Me.Height.ToString)
  18. _qrAddr = _qrAddr.Replace( "{DATA}" , WebUtility.UrlEncode(_DATA))
  19.    
  20. Return _qrAddr
  21. End Function
  22.    
  23. Protected Overrides Sub OnPaint(e As PaintEventArgs)
  24. MyBase.OnPaint(e)
  25. If _DATA Is Nothing Then Exit Sub
  26.    
  27. Dim client As New WebClient()
  28. Dim bytes() As Byte = client.DownloadData(getQRURI())
  29. client.Dispose()
  30.    
  31. Dim memStream As New IO.MemoryStream(bytes)
  32. Dim bmp As Bitmap = Bitmap.FromStream(memStream)
  33. memStream.Dispose()
  34.    
  35. e.Graphics.DrawImage(bmp, 0 , 0 )
  36. End Sub
  37.    
  38. Public Sub New()
  39. InitializeComponent()
  40. End Sub
  41. End Class

<<:  How to develop cross-platform mobile applications on Linux?

>>:  Why Lei Jun rejected Zuckerberg's olive branch

Recommend

Is ESA ready to develop a commercial version of "Courier Boy"?

Recently, ESA announced the launch of a commercia...

New version of Microsoft Launcher released: improved UI and battery life

Microsoft Launcher 6.2.201202.9334 for Android wa...

8 key points for Weibo operations!

I haven't been here for a long time. How are ...

Radio signals in a storm: riding the wave or hanging by a thread?

With thunder and lightning, howling winds, and po...

LeTV Box U4 Pro turns old TV into super TV instantly

Among Internet TV brands, LeTV, which has sold 10 ...

Primary school students’ “Buddhist style of skewers”, do you get it?

Review expert: Zhu Guangsi, member of Beijing Sci...

Old Secretary: "Teaching You to Become a Master of Social Interactions"

Training course content: Do you have such confusi...

How to design a suitable TV game?

I first entered the TV game industry half a year ...

How scary are these cute animals behind the scenes?

One minute with the doctor, the postures are cons...

Android 12 detail adjustments: notification system count is more prominent

Every major version of Android will optimize the ...

Paid community operation strategy and conversion!

The community needs to be hot-started, not cold-s...