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.
		
		
		
		
			
				
					
					
						
							141 lines
						
					
					
						
							5.8 KiB
						
					
					
				
			
		
		
	
	
							141 lines
						
					
					
						
							5.8 KiB
						
					
					
				package com.ynxbd.common.helper.common;
 | 
						|
 | 
						|
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 org.apache.commons.codec.binary.Base64;
 | 
						|
 | 
						|
import javax.imageio.ImageIO;
 | 
						|
import java.awt.image.BufferedImage;
 | 
						|
import java.io.ByteArrayOutputStream;
 | 
						|
import java.io.File;
 | 
						|
import java.util.Hashtable;
 | 
						|
import java.util.Map;
 | 
						|
 | 
						|
 | 
						|
public class QRCodeHelper {
 | 
						|
 | 
						|
    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;
 | 
						|
    }
 | 
						|
 | 
						|
    /**
 | 
						|
     * 将内容生成长宽均为width的图片,图片路径由imgPath指定
 | 
						|
     */
 | 
						|
    public static File getQRCodeImage(String contents, int size, String imgPath) {
 | 
						|
        return getQRCodeImage(contents, size, size, imgPath);
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    public static void main(String[] args) {
 | 
						|
        getQRCodeImage("http://lcey.natapp1.cc/wx/my-info.html", 1024, "C:\\Users\\Administrator\\Desktop\\22.png");
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 将内容生成长为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);
 | 
						|
            BufferedImage image = toBufferedImage(bitMatrix);
 | 
						|
            ImageIO.write(image, "png", imageFile);
 | 
						|
            return imageFile;
 | 
						|
        } catch (Exception e) {
 | 
						|
            e.printStackTrace();
 | 
						|
            return null;
 | 
						|
        }
 | 
						|
    }
 | 
						|
 | 
						|
    public static String encodeToBase64(String content) {
 | 
						|
        return encodeToBase64(content, null, null);
 | 
						|
    }
 | 
						|
 | 
						|
 | 
						|
    /**
 | 
						|
     * 内容转图片base64
 | 
						|
     *
 | 
						|
     * @param content 内容
 | 
						|
     * @param width   二维码宽度
 | 
						|
     * @param height  二维码高度
 | 
						|
     * @return 图片base64
 | 
						|
     */
 | 
						|
    public static String encodeToBase64(String content, Integer width, Integer height) {
 | 
						|
        MultiFormatWriter multiFormatWriter = new MultiFormatWriter();
 | 
						|
        Map<EncodeHintType, Object> hints = new Hashtable<>();
 | 
						|
        hints.put(EncodeHintType.CHARACTER_SET, "UTF-8"); //设置字符集编码类型
 | 
						|
        try {
 | 
						|
            if (width == null) width = 100;
 | 
						|
            if (height == null) height = 100;
 | 
						|
 | 
						|
            BitMatrix bitMatrix = multiFormatWriter.encode(content, BarcodeFormat.QR_CODE, width, height, hints);
 | 
						|
            BufferedImage image = toBufferedImage(bitMatrix);
 | 
						|
            ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
 | 
						|
            //输出二维码图片流
 | 
						|
            ImageIO.write(image, "png", outputStream);
 | 
						|
            return "data:image/png;base64," + Base64.encodeBase64String(outputStream.toByteArray());
 | 
						|
 | 
						|
        } catch (Exception e) {
 | 
						|
            e.printStackTrace();
 | 
						|
        }
 | 
						|
        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;
 | 
						|
////        url = "http://lhxrmyy1.natapp1.cc/wx/q?p=10000005";
 | 
						|
////        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);
 | 
						|
//        }
 | 
						|
//    }
 | 
						|
 | 
						|
 | 
						|
}
 | 
						|
 |