標籤:
Google API快速產生QR二維碼
現在來說產生二維碼最簡單的方法是使用Google Chart API來實現,再次膜拜Google大神~
Google Chart API是一套可以讓你線上產生報表圖的系統,通過URL你可以得到各種圖表。
舉個例子:你在瀏覽器中輸入
https://chart.googleapis.com/chart?cht=qr&chs=200×200&choe=UTF-8&chld=L|4&chl=http://Codeup.org
你將得到一個內容為本站網址的QR二維圖。
回過頭來我們再介紹一下二維碼……好像這話放的位置不對,你們湊活著看吧……
一、什麼是二維碼
隨著科技的發展,智能手機越來越普及,QR碼(二維碼)的使用也越來越多了。利用手機的拍照功能,再加上一些QR碼識別軟體,可以通過二維碼來記錄一些比較枯燥並不好記的資訊,比如說長長的網址。現在很多手機軟體下載網站都提供了可視化的下載路徑,就是將下載路徑做成QR碼,讓手機使用者快速讀取QR碼中的下載連結轉到下載頁面。
具體QR碼的定義可以查看維基百科的介紹。
二、怎樣利用Google API產生二維碼
現在來說,最簡單直接的產生QR碼的方法就是通過Google Chart API來實現。先給出個執行個體,就拿最上面曾列出來那個說吧:
https://chart.googleapis.com/chart?cht=qr&chs=200×200&choe=UTF-8&chld=L|4&chl=http://Codeup.org
我們來分析一下這個連結中的參數:
- https://chart.googleapis.com/chart? 這是Google Chart API的頭部,直接照抄就好了~
- &cht=qr 這是說圖表類型為qr也就是二維碼。
- &chs=200×200 這是說產生圖片尺寸為200×200,是寬x高。這並不是產生圖片的真實尺寸,應該是最大尺寸吧。
- &choe=UTF-8 這是說內容的編碼格式為UTF-8,此值預設為UTF-8.其他的編碼格式請參考Google API文檔。
- &chld=L|4 L代表預設錯誤修正水平; 4代表二維碼邊界空白大小,可自行調節。具體參數請參考Google API文檔。
- &chl=XXXX 這是QR內容,也就是解碼後看到的資訊。包含中文時請使用UTF-8編碼漢字,否則將出現問題。
鑒於Google API的文檔可能會變化,我把其中關鍵區段摘錄於下:
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:
UTF-8 [Default]
Shift_JIS
ISO-8859-1
|
chld=<error_correction_level>|<margin> |
Optional |
- error_correction_level - QR codes support four levels of error correction to enable recovery of missing, misread, or obscured data. Greater redundancy is achieved at the cost of being able to store less data. See the appendix for details. Here are the supported values:
L - [Default] Allows recovery of up to 7% data loss
M - Allows recovery of up to 15% data loss
Q - Allows recovery of up to 25% data loss
H - Allows recovery of up to 30% data loss
- margin - The width of the white border around the data portion of the code. This is in rows, not in pixels. (See below to learn what rows are in a QR code.) The default value is 4.
|
三、我的QR二維碼產生器
昨天晚上花了點功夫自己做了一個可視化的QR二維碼產生器,內容比較簡單,介面比較簡陋,純粹是為了自己做著玩的。如果你想體驗一把產生QR的話,可以來試一試。
QR產生器連結:http://test.codeup.org/CreateQR.html
現在我把CreateQR現在內嵌在這裡,原地址在上面↑↑↑↑如果不能顯示,請訪問上面的連結。
如果你有什麼建議的話請聯絡我。
四、為WordPress中每篇文章QR碼
可以為WordPress中的每篇文章地址都產生一個QR碼放在旁邊,具體代碼如下,作為圖片插入到文章中就OK了~
| 1 |
<img src="https://chart.googleapis.com/chart?cht=qr&chs=150x150&choe=UTF-8&chld=L|4&chl=<?php the_permalink(); ?>" width="150" height="150" alt="QR 代碼" /> |
聲明:未作說明,則本文為代碼至上原創。轉載務必註明出處。
注意:轉載須保留全文,如需修改請 聯絡作者。
本文永久地址:http://codeup.org/archives/241
Google API快速產生QR二維碼