java 生成二维码有很多开发的jar包如zxing,qrcode.q前者是谷歌开发的后者则是小日本开发的,
这里我们使用zxing的开发包来弄
1、先下载zxing开发包,这里用到的只是core那个jar包 (core-3.1.0.jar)
下载地址:http://download.youkuaiyun.com/download/u014733374/8212455
2、使用zxing开发还需要一个类,代码如下
package org.lxh;
import com.google.zxing.common.BitMatrix;
import javax.imageio.ImageIO;
import java.io.File;
import java.io.OutputStream;
import java.io.IOException;
import java.awt.image.BufferedImage;
public final class MatrixToImageWriter {
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
private MatrixToImageWriter() {}
//生成二维码图片方法
public static BufferedImage toBufferedImage(BitMatrix matrix) {
int width = matrix.getWidth(); //图片宽度
int height = matrix.getHeight(); //图片高度
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
//将图片写到文件中(format:传入的图片格式;file:保存二维码图片的路径)
public static void writeToFile(BitMatrix matrix, String format, File file)
throws IOException {
BufferedImage image = toBufferedImage(matrix); //调用生成图片的方法,返回二维码图片
if (!ImageIO.write(image, format, file)) { //将图片以传入的图片格式存入指定的路径下
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
//将图片写到一个啊二进制流中
public static void writeToStream(BitMatrix matrix, String format, OutputStream stream) throws IOException {
BufferedImage image = toBufferedImage(matrix); //调用生成图片的方法,返回二维码图片
if (!ImageIO.write(image, format, stream)) { //将图片以传入的图片格式存入指定的流中
throw new IOException("Could not write an image of format " + format);
}
}
}
3、借助上面的类生成二维码图片
//获得动态的二维码
public String getQRCode(){
String text= "http://192.168.2.129:8081/ps2/mobile/register";//二维码中包含的请求路径 内容
int width = 300; //二维码宽度
int height = 300; //二维码高度
//二维码的图片格式 ,这里可以根据调用方穿过来标识做判断生成什么格式
String format = "jpg";
Hashtable hints = new Hashtable();
//内容所使用编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//将二维码的宽度、高度、编码格式和内容封装到BitMatrix 类中
BitMatrix bitMatrix = new MultiFormatWriter().encode(text,
BarcodeFormat.QR_CODE, width, height, hints);
//生成二维码 ,并且将二维码放到本地文件夹下(如果部署正式项目,就将这张图片部署到正式环境的目录下)
String saveUrl = FacesUtil.getRealPath(File.separator+"upload") + File.separator + userId +".jpg";
File outputFile = new File(saveUrl);
MatrixToImageWriterUtil.writeToFile(bitMatrix, format, outputFile);//将生成的图片写到路劲下
//findPicture是访问服务器的地址,将这个地址传回到调用方,即可获得生成的二维码图片
String findPicture = "http://m.aicailang.com:7001/upload/" + userId +".jpg";
return findPicture ;
}
4、上面是二维码的编码,即将内容编码成二维码图片格式,下面是将编码后的图片进行解码,将内容解析出来
public String decode(String imgPath) {//将图片路径传进来
BufferedImage image = null;
Result result = null;
try {
image = ImageIO.read(new File(imgPath));//读二维码图片
if (image == null) {
System.out.println("the decode image may be not exit.");
}
LuminanceSource source = new BufferedImageLuminanceSource(image);
BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
Hashtable<Object, Object> hints = new Hashtable<Object, Object>();
hints.put(DecodeHintType.CHARACTER_SET, "utf-8");
result = new MultiFormatReader().decode(bitmap, hints);
return result.getText();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* @param args
*/
public static void main(String[] args) {
String imgPath = "d:/1.png";//二维码图片路径
ZxingDecoderHandler handler = new ZxingDecoderHandler();
String decodeContent = handler.decode(imgPath);//解析二维码返回二维码内容
System.out.println("解码内容如下:");
System.out.println(decodeContent);
}