您的位置:首页 > 其它

ZXing生成二维码

2016-07-25 09:11 253 查看
package com.lutai.user.service;

import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.nio.file.FileSystems;
import java.nio.file.Path;
import java.util.HashMap;
import java.util.Map;

/**
* ZXingCodeService
*
* @author FD
* @date 2016/5/16
*/
public class ZXingCodeService {

private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;

private static BufferedImage logoImg;
enum FormatType{
JPG;
}

static {
try {
URL logoUrl = ZXingCodeService.class.getClassLoader().getResource("logo-min.png");
if (logoUrl != null) {
logoImg = ImageIO.read(logoUrl);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 生成二维码参数
*
* @return
*/
private Map<EncodeHintType, Object> getEncodeHintType() {
// 用于设置QR二维码参数
Map<EncodeHintType, Object> hints = new HashMap<>();
// 设置编码方式
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
//        // 设置QR二维码的纠错级别(H为最高级别)具体级别信息
//        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
//        hints.put(EncodeHintType.MARGIN, 0);
return hints;
}

/**
* 根据BitMatrix生成BufferedImage
* @param bitMatrix
* @return
*/
private BufferedImage getBufferedImage(BitMatrix bitMatrix) {
int width = bitMatrix.getWidth();
int height = bitMatrix.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, bitMatrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}

/**
* 将BufferedImage写到文件中
* @param bitMatrix
* @param format
* @param file
*/
public void write2File(BitMatrix bitMatrix, String format, File file) {
BufferedImage bufferedImage = getBufferedImage(bitMatrix);
try {
ImageIO.write(bufferedImage, format, file);
} catch (IOException e) {
e.printStackTrace();
}
}

/**
* 将BufferedImage写到流中
* @param bitMatrix
* @param format
* @param os
*/
public void write2Stream(BitMatrix bitMatrix, String format, OutputStream os) {
BufferedImage bufferedImage = getBufferedImage(bitMatrix);
try {
ImageIO.write(bufferedImage, format, os);
} catch (IOException e) {
e.printStackTrace();
}
}

public void addLogo() {

}

public String encode(String filePath, String fileName) {

Path path = FileSystems.getDefault().getPath(filePath, fileName);

String content = "www.baidu.com";
int width = 300, height = 300;

Map hints = getEncodeHintType();
try {
// 参数顺序分别为:编码内容,编码类型,生成图片宽度,生成图片高度,设置参数
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);
MatrixToImageWriter.writeToPath(bitMatrix, "JPG", path);
} catch (WriterException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return path.toString();
}

public String decode(String filePath) {

BufferedImage image = null;
try {
image = ImageIO.read(new File(filePath));
LuminanceSource source = new BufferedImageLuminanceSource(image);
Binarizer binarizer = new HybridBinarizer(source);
BinaryBitmap binaryBitmap = new BinaryBitmap(binarizer);
Map<DecodeHintType, Object> hints = new HashMap<>();
hints.put(DecodeHintType.CHARACTER_SET, "UTF-8");
Result result = new MultiFormatReader().decode(binaryBitmap, hints);
return result.getText();
} catch (IOException e) {
e.printStackTrace();
} catch (NotFoundException e) {
e.printStackTrace();
}
return "";
}

public static void main(String[] args) {
//        System.out.println(ZXingCodeService.class.getClassLoader().getResource(""));
//        System.out.println(ZXingCodeService.class.getClassLoader().getResource("jsonUtil.class"));
//        System.out.println(ZXingCodeService.class.getClassLoader().getResource("logback.xml"));
System.out.println(new ZXingCodeService().encode("D://fd", "fd.jpg"));
System.out.println(new ZXingCodeService().decode("D://fd/fd.jpg"));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: