下面是一段使用Google Chart Tools提供的生产二维码的例子代码:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
|
<?php
function createQR( $chlData , $chsWidth = 125, $chsHeight = 125, $choe = 'UTF-8' , $chld = array (
'L' ,
4
)) {
$base = 'https://chart.googleapis.com/chart?' ;
$cht = 'qr' ;
$get = $base .
'cht=' . $cht .
'&chs=' . $chsWidth . 'x' . $chsHeight .
'&chl=' . $chlData .
'&choe=' . $choe .
'&chld=' . $chld [0] . '|' . $chld [1];
return $get ;
}
$url = createQR( $chlData = urlencode( 'http://aiilive.blog.51cto.com' ));
echo '<img src="' . $url . '" alt="QR" width="125" height="125" />' ;
?>
|
生成的二维码如下图:
可以查看官方文档,下面就使用API过程中需要设置的参数做简单描述。
参数 |
必要性 |
说明 |
cht=qr |
true |
指定QR码 |
chs=<width>x<height> |
true |
二维码图片最大尺寸 |
chl=<data>
|
true |
数据进行URL编码
数字,字母,二进制,中文;二维码数据不能是混合数据类型
如何使用URL,则需要UTF-8编码,注意如果URL(减去其它url字符,其实就是指data)超过2k,则需要使用POST方式
|
choe=<output_encoding> |
false |
在QR码中数据的编码格式,主要可用的格式有:
UTF-8(默认),Shift-JIS,ISO-8858-1
|
chld=<error_correction_level>|<margin>
|
false |
error_correction_level:容错等级有四级,分别用:L,M,Q,H表示
margin:生成的二维码图片与边框的边距,不过这里不用像素来表示,而是行。默认4
|
我们在使用Google提供的API生成二维码并没有考虑QR码的信息量和版本,而这些工作是Google做了。这里提供两个连接,有关QR的详细信息和标准说明。
http://www.qrcode.com/zh/about/version.html
http://www.qrcode.com/en/codes/
下图来自OR code.com

Google 还提供了QR Code Reader的类库:Zerbar Cross Zxing. Github: https://github.com/zxing/zxing
我的另一篇文章关于使用Zxing生成二维码:http://aiilive.blog.51cto.com/1925756/1352004
本文转自 secondriver 51CTO博客,原文链接:http://blog.51cto.com/aiilive/1546924,如需转载请自行联系原作者