You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
139 lines
5.1 KiB
139 lines
5.1 KiB
2 years ago
|
package com.ynxbd.wx.utils;
|
||
|
|
||
|
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 javax.imageio.ImageIO;
|
||
|
import java.awt.image.BufferedImage;
|
||
|
import java.io.File;
|
||
|
import java.io.IOException;
|
||
|
import java.util.Hashtable;
|
||
|
import java.util.Map;
|
||
|
|
||
|
/**
|
||
|
* @author 张剑峰
|
||
|
* @date 2018年6月4日下午12:38:39
|
||
|
* @version v1.0.0
|
||
|
* @Copyright: 2018云南新八达科技有限公司 All rights reserved.
|
||
|
*/
|
||
|
public class QRCodeUtil {
|
||
|
|
||
|
private static final int BLACK = 0xFF000000;
|
||
|
private static final int WHITE = 0xFFFFFFFF;
|
||
|
private static final int GREEN = 0xFFAAAAAA;
|
||
|
|
||
|
private 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;
|
||
|
}
|
||
|
|
||
|
private 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);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 将内容contents生成长宽均为width的图片,图片路径由imgPath指定
|
||
|
*/
|
||
|
public static File getQRCodeImage(String contents, int size, String imgPath) {
|
||
|
return getQRCodeImage(contents, size, size, imgPath);
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 将内容contents生成长为width,宽为width的图片,图片路径由imgPath指定
|
||
|
*/
|
||
|
public static File getQRCodeImage(String contents, int width, int height, String imgPath) {
|
||
|
try {
|
||
|
Map<EncodeHintType, Object> hints = new Hashtable<>();
|
||
|
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
|
||
|
hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
|
||
|
|
||
|
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
|
||
|
|
||
|
File imageFile = new File(imgPath);
|
||
|
writeToFile(bitMatrix, "png", imageFile);
|
||
|
return imageFile;
|
||
|
} catch (Exception e) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
/**
|
||
|
* 将内容contents生成长为width,宽为width的图片,图片路径由imgPath指定
|
||
|
*/
|
||
|
public static String getQRCodeImgBase64(String contents, int width, int height) {
|
||
|
String resultBase64 = null;
|
||
|
try {
|
||
|
Map<EncodeHintType, Object> hints = new Hashtable<>();
|
||
|
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);
|
||
|
hints.put(EncodeHintType.CHARACTER_SET, "UTF8");
|
||
|
|
||
|
BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);
|
||
|
|
||
|
// File imageFile = new File(imgPath);
|
||
|
// writeToFile(bitMatrix, "svg", imageFile);
|
||
|
return resultBase64;
|
||
|
} catch (Exception e) {
|
||
|
return null;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
public static void main(String[] args) {
|
||
|
|
||
|
// String product_id = "999981XBD100XBD200XBD0.01XBD1";
|
||
|
// // product_id = MD5Util.MD5Encode(product_id, "UTF-8");
|
||
|
// String sec = Long.toString(System.currentTimeMillis());
|
||
|
// String timeStamp = Long.toString(System.currentTimeMillis() / 1000);
|
||
|
// SimpleDateFormat formatter = new SimpleDateFormat("yyyy年-MM月dd日-HH时mm分ss秒");
|
||
|
// Date date = new Date(System.currentTimeMillis());
|
||
|
// String s = formatter.format(date);
|
||
|
// String nonceStr = Long.toString(System.currentTimeMillis());
|
||
|
// Map<String, String> packageParams = new HashMap<>();
|
||
|
// packageParams.put("appid", WeChatConfig.getAppId());
|
||
|
// packageParams.put("mch_id", WeChatConfig.getMchId());
|
||
|
// packageParams.put("product_id", product_id);
|
||
|
// packageParams.put("time_stamp", timeStamp);
|
||
|
// packageParams.put("nonce_str", nonceStr);
|
||
|
// String sign = SignatureUtil.generateSign(packageParams, WeChatConfig.getMchKey());
|
||
|
// String url = "weixin://wxpay/bizpayurl?sign=" + sign + "&appid=" +
|
||
|
// WxConfig.getInstance().getAppId()
|
||
|
// + "&mch_id=" + WxConfig.getInstance().getMchId() + "&product_id=" +
|
||
|
// product_id + "&time_stamp="
|
||
|
// + timeStamp + "&nonce_str=" + nonceStr;
|
||
|
//// String url = "http://lhxrmyy1.natapp1.cc/wx/q?p=10000005";
|
||
|
// // String url =
|
||
|
// // "weixin:/pay/bizpayurl?sign=38128468EEFC56C19EDEAD077E8BD38B&appid=wx940ec0975be0daf4&mchid=1354195702&product_id=123815|814994|2074382|0.90|1&time_stamp=1533309475&nonce_str=1533309475421&";
|
||
|
//
|
||
|
// // String[] array = product_id.split("\\|");
|
||
|
// // String patientId = array[0];
|
||
|
// // String mzNum = array[1];
|
||
|
// // String recipeId = array[2];
|
||
|
// // String payMoney = array[3];
|
||
|
// // if (patientId.equals("") || mzNum.equals("") || recipeId.equals("")
|
||
|
// // || payMoney.equals("")) {
|
||
|
// // System.out.println("扫码支付:参数中有空值,不允许支付!product_id=" + product_id);
|
||
|
// // return;
|
||
|
// // }
|
||
|
|
||
|
getQRCodeImage("http://www.wsxrmyy.cn/wx/qr-questionnaire.html", 300, "C:\\Users\\Administrator\\Desktop\\qr_questionnaire");
|
||
|
}
|
||
|
|
||
|
|
||
|
|
||
|
|
||
|
}
|