因为项目中要实现扫描二维码并实现登录,但本人开发的模块是服务器,跟前台传输用到的主要是json对象。所以不能直接传输图片,必须把图片加密成base64压码的形式。
首先介绍二维码生成的代码,二维码生成我用到的第三方架包是google的zxing。首先下载所需要的包。本来下载一个包zxing-core-2.0.jar,但发现代码有些类找不到具体包,后来查询才发现还少一个包。此包名为:zxing-1.7-javase.jar,稍后会把包上传。
代码如下所示:
- import java.awt.Color;
- import java.awt.Graphics;
- import java.io.ByteArrayOutputStream;
- import java.io.File;
- import java.io.FileNotFoundException;
- import java.io.FileOutputStream;
- import java.io.IOException;
- import java.util.Hashtable;
- import sun.misc.BASE64Decoder;
- import sun.misc.BASE64Encoder;
- import com.google.zxing.BarcodeFormat;
- import com.google.zxing.EncodeHintType;
- import com.google.zxing.MultiFormatWriter;
- import com.google.zxing.common.BitMatrix;
- import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
- import com.google.zxing.client.j2se.MatrixToImageWriter;
- public class EncoderHandler {
- /**
- * 编码
- * @param contents
- * @param width
- * @param height
- * @param imgPath
- */
- public void encode(String contents, int width, int height, String imgPath) {
- Hashtable<Object, Object> hints = new Hashtable<Object, Object>();
- // 指定纠错等级
- hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
- // 指定编码格式
- try {
- BitMatrix byteMatrix;
- byteMatrix = new MultiFormatWriter().encode(new String(contents.getBytes("UTF-8"),"iso-8859-1"),BarcodeFormat.QR_CODE, width, height);
- ByteArrayOutputStream bao = new ByteArrayOutputStream();
- MatrixToImageWriter.writeToStream(byteMatrix, "png", bao);
- System.out.println(bao.toByteArray());
- String ok = Base64Code(bao.toByteArray());
- System.out.println("-----------------------------------------------------------");
- System.out.println(ok);
- System.out.println("--------------------------------------------------------------");
- createImage(ok);
- } catch (Exception e) {
- e.printStackTrace();
- }
- }
- public String Base64Code(byte[] b) {
- BASE64Encoder encoder = new BASE64Encoder();
- String codeBase64 = "";
- StringBuilder pictureBuffer = new StringBuilder();
- pictureBuffer.append(encoder.encode(b));
- System.out.println(pictureBuffer.toString());
- codeBase64 = pictureBuffer.toString();
- return codeBase64;
- }
- public void createImage(String Base64){
- BASE64Decoder decoder = new BASE64Decoder();
- FileOutputStream write;
- try {
- write = new FileOutputStream(new File(
- "c:/ok.png"));
- byte[] decoderBytes = decoder
- .decodeBuffer(Base64);
- write.write(decoderBytes);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- System.out.println("Decoding the picture Success");
- }
- /**
- * @param args
- */
- public static void main(String[] args) {
- String imgPath = "e:/zxing.png";
- String contents = "http://www.baidu.com";
- int width = 300, height = 300;
- EncoderHandler handler = new EncoderHandler();
- handler.encode(contents, width, height, imgPath);
- System.out.println("successful");
- }
- }