In order to study the unified intersection management of Alipay and WeChat payment (a QR code supports WeChat and Alipay scan code payment at the same time); this article will not explain the payment development of Alipay and WeChat, mainly the generation of QR code and parsing;
steps:
1. Prepare jar package, two jar packages are used in java; one is used to generate QR code, and the other is used to parse QR code;
a. Generate QR code jar package path: http:// www.swetake.com/qr/java/qr_java.html ( Note: Download qrcode_java0.50beta10.tar.gz, after decompression, the qrcode.jar in the lib directory is the library used to generate the QR code )
b. Analysis 2 QR code jar package path: http://sourceforge.jp/projects/qrcode/releases/ ( Note: After decompressing the qrcode package, the qrcode.jar package in the lib directory should be renamed to avoid conflicts with the generated QR code jar package
2. java code a , TwoDimensionCodeImage
class: QR code image object
package com.ppfuns.aaa.entity; import jp.sourceforge.qrcode.data.QRCodeImage; import java.awt.image.BufferedImage; /** * Created with IntelliJ IDEA. * User: jony * Date: 2017/9/26 * Time: 9:53 * To change this template use File | Settings | File Templates. */ public class TwoDimensionCodeImage implements QRCodeImage { BufferedImage bufImg; public TwoDimensionCodeImage(BufferedImage bufImg) { this.bufImg = bufImg; } @Override public int getHeight() { return bufImg.getHeight(); } @Override public int getPixel(int x, int y) { return bufImg.getRGB(x, y); } @Override public int getWidth() { return bufImg.getWidth(); } }
b. CommonUtils class: the core class of QR code operation
package com.ppfuns.aaa.common; import com.ppfuns.aaa.entity.TwoDimensionCodeImage; import com.swetake.util.Qrcode; import jp.sourceforge.qrcode.QRCodeDecoder; import jp.sourceforge.qrcode.exception.DecodingFailedException; import javax.imageio.ImageIO; import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; /** * Created with IntelliJ IDEA. * User: Jian Dequn * Date: 2017/9/26 * Time: 9:54 * To change this template use File | Settings | File Templates. */ public class CommonUtils { /** * Generate QRCode image * @param content store the content * @param imgPath image path */ public static void encoderQRCode(String content, String imgPath) { encoderQRCode(content, imgPath, "png", 7); } /** * Generate QRCode image * @param content store the content * @param output output stream */ public static void encoderQRCode(String content, OutputStream output) { encoderQRCode(content, output, "png", 7); } /** * Generate QRCode image * @param content store the content * @param imgPath image path * @param imgType image type */ public static void encoderQRCode(String content, String imgPath, String imgType) { encoderQRCode(content, imgPath, imgType, 7); } /** * Generate QRCode image * @param content store the content * @param output output stream * @param imgType image type */ public static void encoderQRCode(String content, OutputStream output, String imgType) { encoderQRCode(content, output, imgType, 7); } /** * Generate QRCode image * @param content store the content * @param imgPath image path * @param imgType image type * @param size QR code size */ public static void encoderQRCode(String content, String imgPath, String imgType, int size) { try { BufferedImage bufImg = qRCodeCommon(content, imgType, size); File imgFile = new File(imgPath); // Generate QRCode image of QR code ImageIO.write(bufImg, imgType, imgFile); } catch (Exception e) { e.printStackTrace (); } } /** * Generate QRCode image * @param content store the content * @param output output stream * @param imgType image type * @param size QR code size */ public static void encoderQRCode(String content, OutputStream output, String imgType, int size) { try { BufferedImage bufImg = qRCodeCommon(content, imgType, size); // Generate QRCode image of QR code ImageIO.write(bufImg, imgType, output); } catch (Exception e) { e.printStackTrace (); } } /** * A public method for generating a QRCode image * @param content store the content * @param imgType image type * @param size QR code size * @return */ private static BufferedImage qRCodeCommon(String content, String imgType, int size) { BufferedImage bufImg = null; try { Qrcode qrcodeHandler = new Qrcode(); // Set the error rate of QR code, you can choose L(7%), M(15%), Q(25%), H(30%). The higher the error rate, the less information can be stored, but The smaller the requirement for the clarity of the QR code qrcodeHandler.setQrcodeErrorCorrect('M'); qrcodeHandler.setQrcodeEncodeMode('B'); // Set the size of the QR code, the value range is 1-40. The larger the value, the larger the size and the larger the information that can be stored. qrcodeHandler.setQrcodeVersion(size); // Get the byte array of the content, set the encoding format byte[] contentBytes = content.getBytes("utf-8"); // size of the picture int imgSize = 67 + 12 * (size - 1); bufImg = new BufferedImage(imgSize, imgSize, BufferedImage.TYPE_INT_RGB); Graphics2D gs = bufImg.createGraphics(); // set background color gs.setBackground(Color.WHITE); gs.clearRect(0, 0, imgSize, imgSize); // set image color > BLACK gs.setColor(Color.BLACK); // Set the offset, not setting it may cause parsing errors int pixoff = 2; // output content > QR code if (contentBytes.length > 0 && contentBytes.length < 800) { boolean[][] codeOut = qrcodeHandler.calQrcode(contentBytes); for (int i = 0; i < codeOut.length; i++) { for (int j = 0; j < codeOut.length; j++) { if (codeOut[j][i]) { gs.fillRect(j * 3 + pixoff, i * 3 + pixoff, 3, 3); } } } } else { throw new Exception("QRCode content bytes length = " + contentBytes.length + " not in [0, 800]."); } gs.dispose(); bufImg.flush(); } catch (Exception e) { e.printStackTrace (); } return bufImg; } /** * Parse QR Code (QRCode) * @param imgPath image path * @return */ public static String decoderQRCode(String imgPath) { // QRCode QR code image file File imageFile = new File(imgPath); BufferedImage bufImg = null; String content = null; try { bufImg = ImageIO.read(imageFile); QRCodeDecoder decoder = new QRCodeDecoder(); content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8"); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); e.printStackTrace (); } catch (DecodingFailedException dfe) { System.out.println("Error: " + dfe.getMessage()); dfe.printStackTrace(); } return content; } /** * Parse QR Code (QRCode) * @param input input stream * @return */ public String decoderQRCode(InputStream input) { BufferedImage bufImg = null; String content = null; try { bufImg = ImageIO.read(input); QRCodeDecoder decoder = new QRCodeDecoder(); content = new String(decoder.decode(new TwoDimensionCodeImage(bufImg)), "utf-8"); } catch (IOException e) { System.out.println("Error: " + e.getMessage()); e.printStackTrace (); } catch (DecodingFailedException dfe) { System.out.println("Error: " + dfe.getMessage()); dfe.printStackTrace(); } return content; } public static void main(String[] args) { String content = "https://k.87.re/2014/07/150724ix4.jpg"; //The picture requirement is an existing QR code picture, the generated QR code here is just an update of the content in the QR code picture encoderQRCode(content,"C:\\Users\\lenovo\\Desktop\\121.jpg"); String path = "C:\\Users\\lenovo\\Desktop\\121.jpg"; String content1 = decoderQRCode(path); System.out.println(content1); // } }