qrcode是二维码的一种。qrcode可以存储最多4296个字母数字类型的任意文本。这些文本可以是任何内容,例如,网址、联系信息、电话号码(具体科查看二维码数据格式)。qr code存储的信息可以被安装有适当软件的光学设备读取。这种设备既可以是专用的qr code读取器也可以是手机。
通过调用 google chart tools / image charts 的 api ,我们可以很方便的生成qrcode。
调用方式也很简单,只要向 http://chart.apis.google.com/chart 传入适合的参数就可以了,参数如下:
1. cht=qr
这个是必需的,告诉 api ,你需要生成的是二维码。
2. chs=width>xheight>
这个同样是必需的,告诉 api ,你需要生成的二维码的尺寸。
3. chl=data>
这个还是必需的,用来告诉 api 二维码所包含的信息。可以是数字、字符数字、字符、二进制信息、汉字。不能混合数据类型。数据必须经过utf-8 url-encoded。如果需要传递的信息超过2k个字节,请使用post方式。
4. choe=output_encoding>
终于来了个不是必须的,这个是用来声明生成的二维码所包含信息的编码,默认是 utf-8 ;其他可选编码是 shift_jis 、 iso-8859-1
5. chld=error_correction_level>|margin>
可选 纠错等级。qr码支持四个等级的纠错,用来恢复丢失的、读错的、模糊的、数据。下面是可选的值:l-(默认)可以识别已损失7%的数据;m-可以识别已损失15%的数据;q-可以识别已损失25%的数据;h-可以识别已损失30%的数据。margin 是指生成的二维码离图片边框的距离。
qr码是方形的,有相同的长和宽。qr码的大小是固定的:从21到177的长/宽,每次递增4个像素点。每个配置被称为一个等级。长和宽越大,存储的信息就越多。下面是版本摘要:
等级为1的qr码长和宽分别为21个像素,最多可以存储25个字母数字和字符。
等级为2的qr码长和宽分别为25个像素,最多可以存储47个字母数字和字符。
…以此类推 。
chart api会根据你将存储的信息的大小来决定使用哪个等级的qr码。最棒的qr码阅读器可以读取等级为40的qr码中存储的信息。然而通常来说移动设备最多可以读取等级为4的qr码中存储的信息。
下面来介绍使用php调取google chart api 来生成二维码
data = $text; } //creating code with link mtadata public function link($url){ if (preg_match('/^http:\/\//', $url) || preg_match('/^https:\/\//', $url)) { $this->data = $url; } else { $this->data = http://.$url.; } } //creating code with bookmark metadata public function bookmark($title, $url){ $this->data = mebkm:title:.$title.;url:.$url.;;; } //creating code with email address metadatapublic function email_address($email){ $this->data= mailto:.$email.;} //creating code with email metadata public function email($email, $subject, $message){ $this->data = matmsg:to:.$email.;sub:.$subject.;body:.$message.;;; } //creating code with phone public function phone_number($phone){ $this->data = tel:.$phone; } //creating code with sms metadata public function sms($phone, $text){ $this->data = smsto:.$phone.:.$text; } //creating code with mms metadata public function mms($phone, $text){ $this->data = mmsto:.$phone.:.$text; } //creating code with mecard metadata public function contact_info($name, $address, $phone, $email){ $this->data = mecard:n:.$name.;adr:.$address.;tel:.$phone.;email:.$email.;;; } //creating code with geo location metadata public function geo($lat, $lon, $height){ $this->data = geo:.$lat.,.$lon.,.$height; } //creating code with wifi configuration metadata public function wifi($type, $ssid, $pass){ $this->data = wifi:t:.$type.;s.$ssid.;.$pass.;;; } //creating code with i-appli activating meta data public function iappli($adf, $cmd, $param){ $cur = current($param); $next = next($param); $param_str = ; foreach($cur as $key => $val) { $param_str .= param:.$val.,.$next[$key].;; } $this->data = lapl:adfurl:.$adf.;cmd:.$cmd.;.$param_str.;; } //creating code with gif or jpg image, or smf, mfi or toruca files as content public function content($type, $size, $content){ $this->data = cnts:type:.$type.;lng:.$size.;body:.$content.;;; } //getting image public function get_image($size = 150, $ec_level = 'l', $margin = '0'){ $ch = curl_init(); $this->data = urlencode($this->data); curl_setopt($ch, curlopt_url, 'http://chart.apis.google.com/chart'); curl_setopt($ch, curlopt_post, true); curl_setopt($ch, curlopt_postfields, 'chs='.$size.'x'.$size.'&cht=qr&chld='.$ec_level.'|'.$margin.'&chl='.$this->data); curl_setopt($ch, curlopt_returntransfer, true); curl_setopt($ch, curlopt_header, false); curl_setopt($ch, curlopt_timeout, 30); $response = curl_exec($ch); curl_close($ch); return $response; } //getting link for image public function get_link($size = 150, $ec_level = 'l', $margin = '0'){ $this->data = urlencode($this->data); return 'http://chart.apis.google.com/chart?chs='.$size.'x'.$size.'&cht=qr&chld='.$ec_level.'|'.$margin.'&chl='.$this->data; } //forsing image download public function download_image($file){ header('content-description: file transfer'); header('content-type: image/png'); header('content-disposition: attachment; filename=qrcode.png'); header('content-transfer-encoding: binary'); header('expires: 0'); header('cache-control: must-revalidate, post-check=0, pre-check=0'); header('pragma: public'); header('content-length: ' . filesize($file)); ob_clean(); flush(); echo $file; }}?>
使用上述二维码 php类的方法
bookmark($title,$url); //获取二维码图片urlecho get_link().%22>; //here is the way to output image//header(content-type:image/png);//echo $qr->get_image(); //and here is the way to force image download//$file = $qr->get_image();//$qr->download_image($file) ?>
