1. <! - generating a two-dimensional code according to the link ->
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.2.1</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.3.3</version> </dependency> </dependencies>
2.util
package com.xinlianpu.util; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.WriterException; import com.google.zxing.client.j2se.MatrixToImageWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import javax.imageio.ImageIO; import javax.imageio.stream.ImageOutputStream; import java.awt.*; import java.awt.image.BufferedImage; import java.io.*; import java.net.URL; import java.util.HashMap; import java.util.Map; import java.util.Objects; /** * Created by Mafy on 2019/5/9. */ public class ZxingUtils { public static BufferedImage enQRCode(String contents, int width, int height) throws WriterException { //定义二维码参数 final Map<EncodeHintType, Object> hints = new HashMap(8) { { //编码 put(EncodeHintType.CHARACTER_SET, "UTF-8"); //容错级别 put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); //边距 put(EncodeHintType.MARGIN, 0); } }; return enQRCode(contents, width, height, hints); } /** Generating a two-dimensional code * BitMatrix bitMatrix = new MultiFormatWriter () encode (contents, BarcodeFormat.QR_CODE, width,. height,hints); * * @Param Contents QR code content * @param width image width * @param height Image Height * @param hints dimensional code parameters * @return BufferedImage objects * Error @throws WriterException coding * @throws IOException Error writing to file * / public static enQRCode the BufferedImage (String Contents, int width, int height, the Map hints) throws WriterException { .. // UUID.randomUUID = UUID String () toString () Replace ( "-", ""); // local full path // String pathname = path + "/" + + + uuid format. ""; // generate two-dimensional code // File path = new new File (pathname) .toPath (); // save the two-dimensional code to under path MatrixToImageWriter.writeToPa // (bitMatrix, the format, File); // return pathname; return MatrixToImageWriter.toBufferedImage (bitMatrix); } / ** * will be drawn on the background image in FIG. * * @Param backgroundPath background path * @param zxingImage image * x @param x-axis origin on the background picture of the drawing of FIG. * @param y background image drawn on the y-axis of FIG starting * @return * / public static the BufferedImage the drawImage (String backgroundPath, the BufferedImage zxingImage, X int, int y ) throws IOException { // read the background image stream BufferedImage backgroundImage; // Try-with-resources resources automatically shut down, automatically call close () method to close resources, limited to achieve Closeable or AutoCloseable interface type the URL of the URL of imgUrl = new new (backgroundPath); // the try (InputStream ImageIN = new new FileInputStream (backgroundPath )) { // the backgroundImage = ImageIO.read (ImageIN); //} the try (= the InputStream ImageIN new new BufferedInputStream (imgurl.openStream ())) { the backgroundImage = ImageIO.read (ImageIN); } return the drawImage (the backgroundImage, zxingImage, X, y); } / ** * will be drawn on the background image in FIG. * * @param the backgroundImage background * @param zxingImage image * @param x background image drawn on x-axis starting point * @param y pictures drawn on the y-axis origin background @Return * * @throws IOException * / public static the drawImage the BufferedImage (the backgroundImage the BufferedImage, the BufferedImage zxingImage, int X, Y int) throws IOException { Objects.requireNonNull (the backgroundImage, "background >>>>> not empty"); Objects .requireNonNull (zxingImage, ">>>>> two-dimensional code is not empty"); // + x width two-dimensional code may not exceed the width of the background, the length Similarly if ((zxingImage.getWidth () + x ) > backgroundImage.getWidth () || (zxingImage.getHeight () + Y)> backgroundImage.getHeight ()) { the throw new new IOException ( "+ x width >>>>> dimensional code may not exceed the width of the background, the length Similarly "); } // combined image Graphics2D g = backgroundImage.createGraphics (); G.drawImage (zxingImage, X, Y, zxingImage.getWidth () zxingImage.getHeight (), NULL); return the backgroundImage; } / ** * draw text on the background of FIG. * * @param the backgroundImage background * x @param x-axis origin on a text drawn background * @param y the y-axis is plotted on the starting point of the text background * @return * @throws IOException * / public static the drawString the BufferedImage (the BufferedImage the backgroundImage, String text, int X, int y, the font font, Color Color) { // draw text Graphics2D g = backgroundImage.createGraphics (); // set the color g.setColor (color); // eliminate jaggies g.setRenderingHint (RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB); // set the font g.setFont(font); //绘制文字 g.drawString(text, x, y); return backgroundImage; } public static InputStream bufferedImageToInputStream(BufferedImage backgroundImage) throws IOException { return bufferedImageToInputStream(backgroundImage, "png"); } /** * backgroundImage 转换为输出流 * * @param backgroundImage * @param format * @return * @throws IOException */ public static InputStream bufferedImageToInputStream(BufferedImage backgroundImage, String format) throws IOException { ByteArrayOutputStream bs = new ByteArrayOutputStream(); try ( ImageOutputStream imOut = ImageIO.createImageOutputStream(bs)) { ImageIO.write(backgroundImage, format, imOut); InputStream is = new ByteArrayInputStream(bs.toByteArray()); return is; } } /** * 保存为文件 * * @param is * @param fileName * @throws IOException */ public static void saveFile(InputStream is, String fileName) throws IOException { the try (in = new new BufferedInputStream BufferedInputStream (IS); imgwrong = to true; BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName))) { int len; byte [] B = new new byte [1024]; the while ((len = in.read (B)) = -1!) { out.write (B, 0, len); } } } // reading the remote image url , width and height to obtain public static int [] returnImgWH (URL the URL) { int [] = A new new int [2]; the BufferedImage BI = null; Boolean = imgwrong to false; the try { // read the image bi = javax.imageio.ImageIO .read (URL); the try { // file determines whether the normal image display, some pictures incorrectly encoded int I = bi.getType (); imgwrong = to false; } the catch} (Exception E) { String contcent = "http://192.168.4.162:8802/portal/courseActivity/detail?id=aed2fabfde70409ea0611d4eaf05dd6e:3b8b77dd-a63b-4555-abc0-2c0033af35be"; The catch} (IOException EX) { ex.printStackTrace (); } IF (imgwrong) { A [0] = bi.getWidth (); // get the width of a [1] = bi.getHeight () ; // highly } {the else A = null; } return A; } public static void main (String [] args) { // dimensional code width int width = 293; // height of the two-dimensional code int height = 293; // two-dimensional code content = null zxingImage the BufferedImage; the try { // D barcode image stream zxingImage = ZxingUtils.enQRCode (contcent, width, height); The catch} (WriterException E) { e.printStackTrace (); } // background picture address String backgroundPath = "http://test-dfs.enterfaces.com/M00/00/51/CgYBa1zRe4iAM5e9AAEUUh_Ua8g311.jpg"; the InputStream = null inputStream ; the try { the URL = new new URL the URL (backgroundPath); int [] = returnImgWH BB (URL); // two-dimensional code and the background synthesis BufferedImage image = ZxingUtils.drawImage (backgroundPath, zxingImage , bb [0] -width, bb [. 1] -height); // String text = ""; // = ZxingUtils.drawString Image (Image, text, 375, 647, font, new Color (244,254,189 )); // -------------------------------- // --------- --------------------------------------- draw text -------------- // Font font = new Font ( "Microsoft elegant black", Font.BOLD, 35); // // ----------------------------------------- draw text --- -------------------------------------------------- // image transfer inputStream inputStream = ZxingUtils.bufferedImageToInputStream (image); } the catch (IOException E) { e.printStackTrace (); } // save the image path String originalFileName = "D: //99999.png"; the try { / / save local image ZxingUtils.saveFile (inputStream, originalFileName); } the catch (IOException E) { e.printStackTrace (); } } }