如果就是想要普通的二维码生成,google apis已经做好了。
我们甚至都不需要去编码,直接url方式引用即可。
效果如下:
http://chart.apis.google.com/chart?cht=qr&&chs=550x500&chl=%E5%8C%85%E5%95%86%E9%93%B6%E8%A1%8C
You can create a QR code on the fly with a URL GET request.
QR codes are a popular type of two-dimensional barcode. They are also known as hardlinks or physical world hyperlinks. QR Codes store up to 4,296 alphanumeric characters of arbitrary text. This text can be anything, for example URL, contact information, a telephone number, even a poem! QR codes can be read by an optical device with the appropriate software. Such devices range from dedicated QR code readers to mobile phones.
Root URL: https://chart.googleapis.com/chart?
QR code requests support the following URL query parameters after the ? in the root URL:
Parameter | Required or Optional | Description |
---|---|---|
cht=qr |
Required | Specifies a QR code. |
chs=<width>x<height> |
Required | Image size. |
chl=<data> |
Required | The data to encode. Data can be digits (0-9), alphanumeric characters, binary bytes of data, or Kanji. You cannot mix data types within a QR code. The data must be UTF-8 URL-encoded. Note that URLs have a 2K maximum length, so if you want to encode more than 2K bytes (minus the other URL characters), you will have to send your data using POST. |
choe=<output_encoding> |
Optional | How to encode the data in the QR code. Here are the available values:
|
chld=<error_correction_level>|<margin> |
Optional |
|
Example:
cht=qr
chl=Hello+world
choe=UTF-8
Here is a little more about how QR codes work; you don't necessarily need to know this to be able to generate a QR code.
QR codes are squares, with an equal number of rows and columns. There are a fixed set of QR code sizes: from 21 to 177 rows/columns, increasing in steps of four. Each configuration is called a version. The more rows/columns, the more data the code can store. Here is a summary of the versions:
Don't confuse the number of rows and columns with size of the QR code image. The pixel size of the code is determined using chs
as usual.
The API will determine which version to use, based on the amount of data you provide.
The appropriate QR code version will be returned depending on the number of characters you provide. For example, if you provide 55 alphanumeric characters, you will get a Version 3 QR code, although this can change if you specify an error correction (EC) level explicitly using the chld
parameter.
Before generating your QR code, consider what kind of device is used to read your code: the best QR code readers are able to read Version 40 codes; mobile devices might only be able to read up to Version 4.
The following table summarizes the characteristics of a few different versions:
Version | Rows x Columns | EC level | Maximum characters by EC level and character type | |||
---|---|---|---|---|---|---|
Digits: 0 to 9 | Alphanumeric: 0 to 9, A to Z, space, $ % * + - . / : |
Binary | Kanji | |||
1 | 21x21 | L | 41 | 25 | 17 | 10 |
M | 34 | 20 | 14 | 8 | ||
Q | 27 | 16 | 11 | 7 | ||
H | 17 | 10 | 7 | 4 | ||
2 | 25x25 | L | 77 | 47 | 32 | 20 |
M | 63 | 38 | 26 | 16 | ||
Q | 48 | 29 | 20 | 12 | ||
H | 34 | 20 | 14 | 8 | ||
3 | 29x29 | L | 127 | 77 | 53 | 32 |
M | 101 | 61 | 42 | 26 | ||
Q | 77 | 47 | 32 | 20 | ||
H | 58 | 35 | 24 | 15 | ||
4 | 33x33 | L | 187 | 114 | 78 | 48 |
M | 149 | 90 | 62 | 38 | ||
Q | 111 | 67 | 46 | 28 | ||
H | 82 | 50 | 34 | 21 | ||
10 | 57x57 | L | 652 | 395 | 271 | 167 |
M | 513 | 311 | 213 | 131 | ||
Q | 364 | 221 | 151 | 93 | ||
H | 288 | 174 | 119 | 74 | ||
40 | 177x177 | L | 7,089 | 4,296 | 2,953 | 1,817 |
M | 5,596 | 3,391 | 2,331 | 1,435 | ||
Q | 3,993 | 2,420 | 1,663 | 1,024 | ||
H | 3,057 | 1,852 | 1,273 | 784 |
The QR code standard is trademarked by Denso Wave, Inc.
The ISO sells the English language specification on their site. The Japanese version is free.
QR code standards are approved as:
QR code reader software is available from many sources. Google offers a QR Code reader library, Zebra Crossing (ZXing), for free. See http://code.google.com/p/zxing/ for details.
See Barcode Contents for a rough guide to standard encoding of information in barcodes