package com.neusoft.saas.contactcentre.controller.qrCode;
import java.io.File;
import java.io.IOException;
import java.util.Date;
import java.util.Hashtable;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
public class QRCodeFactory {
public static String EncodeQRCODE(String contents, int width, int height,
String iconImagePath, String codePath,String ImageName)
throws IOException, WriterException{
try{
if(width == 0){
width = 300; // 二维码图片宽度430
}
if(height == 0){
height = 300; // 二维码图片高度430
}
File testFile = new File(codePath);
if(!testFile .exists()){
testFile.mkdir();//创建目录
}
testFile = new File(codePath,ImageName);
if(!testFile .exists()){
testFile.createNewFile();//创建文件
}
String format = "png";// 二维码的图片格式png
Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
// 指定纠错等级,纠错级别(L 7%、M 15%、Q 25%、H 30%)
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
// 内容所使用字符集编码
hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
// hints.put(EncodeHintType.MAX_SIZE, 350);//设置图片的最大值
// hints.put(EncodeHintType.MIN_SIZE, 200);//设置图片的最小值
hints.put(EncodeHintType.MARGIN, 0); //设置白边
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents,//要编码的内容
//编码类型,目前zxing支持:Aztec 2D,CODABAR 1D format,Code 39 1D,Code 93 1D ,Code 128 1D,
//Data Matrix 2D , EAN-8 1D,EAN-13 1D,ITF (Interleaved Two of Five) 1D,
//MaxiCode 2D barcode,PDF417,QR Code 2D,RSS 14,RSS EXPANDED,UPC-A 1D,UPC-E 1D,UPC/EAN extension,UPC_EAN_EXTENSION
BarcodeFormat.QR_CODE,
width, //条形码的宽度
height, //条形码的高度
hints);//生成条形码时的一些配置,此项可选
// 生成二维码
// File outputFile = new File(codePath,new Date().getTime()+".png");//指定输出路径
// bitMatrix = deleteWhite(bitMatrix);
MatrixToImageWriter.writeToFile(bitMatrix, format, testFile,iconImagePath);
return codePath+ImageName;
} catch (IOException e) {
System.err.println("图片读取异常 : "+e.getMessage());
} catch (WriterException e) {
System.err.println("图片输出异常 :"+e.getMessage());
}
return null;
}
public static BitMatrix deleteWhite(BitMatrix matrix){
int[] rec = matrix.getEnclosingRectangle();
int resWidth = rec[2] + 2;
int resHeight = rec[3] + 2;
BitMatrix resMatrix = new BitMatrix(resWidth, resHeight);
resMatrix.clear();
for (int i = 0; i < resWidth; i++) {
for (int j = 0; j < resHeight; j++) {
if (matrix.get(i + rec[0], j + rec[1]))
resMatrix.set(i+1, j+1);
// if (matrix.get(i + rec[0], j + rec[1]))
// resMatrix.set(i, j);
}
}
return resMatrix;
}
public static void main(String[] args) throws Exception {
try {
String contents = "https://blog.csdn.net/u013173247/article/details/41676495";
String codePath = System.getProperty("user.dir")+"/testQrcode/";
String pngStr = new Date().getTime()+".png";
String qrcodeImagePath = codePath + pngStr;
EncodeQRCODE(contents,280, 280, codeIconPath, codePath,pngStr);
if(qrcodeImagePath != null){
File file = new File(qrcodeImagePath);
String base64 = ImageUtil.encodeImgageToBase64(file);
base64 = base64.replaceAll("\r|\n", "");
// if (!file.exists()) {
// System.out.println("删除文件失败:" + qrcodeImagePath + "刚生成的file不存在!");
// } else {
// if (file.isFile()){
// file.delete();
// }
// }
System.out.println("base64");
System.out.println(base64);
}else{
System.out.println("报错了!");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}