public class QRCodeMax { // text display Private static Final int QRCOLOR = 0x201f1f; // dimensional code Color: Black Private static Final int BGWHITE = 0xFFFFFF; // dimensional code Background Color: White // set two-dimensional QR code parameter information Private static the Map <EncodeHintType, Object> = hints new new the HashMap <EncodeHintType, Object> () { Private static Final Long serialVersionUID = 1L ; { PUT (EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //QR two-dimensional code is provided an error correction level (H is the highest level) PUT (EncodeHintType.CHARACTER_SET, "UTF-. 8"); // set the encoding PUT (EncodeHintType.MARGIN, 0); // white border } }; / ** * + background image to generate a two-dimensional code described in the text + * * @param name fileName generated image * @param bgImgFile background picture address * @param wIDTH dimensional code width * @param hEIGHT height of the two-dimensional code * @param qrUrl two dimensional code identification address * @param ImageSX x-axis direction of two-dimensional code * @param ImageSY y-axis direction of two-dimensional code * / public static void CreatQRCode (File fileName, File bgImgFile, Integer WIDTH, HEIGHT Integer, String qrUrl, Integer ImageSX, Integer ImageSY) { the try { MultiFormatWriter multiFormatWriter = new new MultiFormatWriter (); // parameters are sequence: encoding the content, encoding type, generating image width, image height generated, setting parameters BitMatrix BM = multiFormatWriter.encode (qrUrl, BarcodeFormat.QR_CODE, wIDTH, hEIGHT, hints); the BufferedImage image = new new the BufferedImage (wIDTH, hEIGHT, BufferedImage.TYPE_INT_RGB); // start with diethyl dimensional code data creation Bitmap images are set to black (0xFFFFFFFF) white (0xFF000000) two color for( Int X = 0; X <WIDTH; X ++ ) { for ( int Y = 0; Y <HEIGHT; Y ++ ) { image.setRGB (X, Y, bm.get (X, Y) ? QRCOLOR: BGWHITE); } } / * * a background image * / the BufferedImage the backgroundImage = ImageIO.read (bgImgFile); int bgWidth = backgroundImage.getWidth (); int qrWidth = image.getWidth (); // distance x from the edge of the background image, centered int disx = (bgWidth - qrWidth) - imagesX; //距离y边距离 * * * * int disy = imagesY; Graphics2D rng = backgroundImage.createGraphics(); rng.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP)); rng.drawImage(image, disx, disy, WIDTH, HEIGHT, null); rng.dispose(); image = backgroundImage; image.flush(); ImageIO.write(image, "png", fileName); } catch (Exception e) { e.printStackTrace(); } } /** Test * * * @param args * / public static void main (String [] args) { File bgImgFile = new new File ( "D: / Project-the nums / Industry / Bud / Operation / the src / main / Resources / static / T. ; JPG ") // background picture File QrCodeFile = new new D (File": /data/img/6.jpg "); // generate Photos location String url =" https://blog.csdn.net/weixin_38407595 "; // two-dimensional code link String note = ""; // text description String TUI = ""; // text description // publicity generated two-dimensional code //FIG generating an address, the address background, two-dimensional code width, the height of the two-dimensional code, the two-dimensional address code recognition, text descriptions 1, 2 described in the text, text size, image x-axis direction, y-axis direction images, text 2xy 1 || axis direction CreatQRCode (QrCodeFile, bgImgFile, 200 is, 200 is, URL, 308, 123 ); } }
The above method, the test method used for two-dimensional code with the locally generated