Use zxing batch to generate a QR code at the specified position of the prepared card background image and place the specified text content (link address, text, etc.) and place it in that position, and finally add the card number.
step:
1). Make a background picture as shown below:
2). Generate the QR code BufferedImage object. The code is as follows:
/** * * @Title: toBufferedImage * @Description: Convert text into QR code image object* @param text * QR code content* @param width * QR code height* @param height * Two-digit width* @param * @param Exception * Set file* @return BufferedImage Return type* @throws */ public static BufferedImage toBufferedImage(String text, int width, int height) throws Exception { int BLACK = 0xFF000000; int WHITE = 0xFFFFFFFF; Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // The character set encoding used by the content is hints.put(EncodeHintType.MARGIN, 1); BitMatrix matrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); 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; }3). Generate a QR code at the specified position of the card background image, the code is as follows:
/** * * @Title: markImageByCode * @Description: Add QR code to the specified position of the image* @param img * QR code image object* @param srcImgPath * Background image* @param targetPath * Target map* @param positionWidth * Position horizontal coordinate* @param positionHeight * Position vertical coordinate* @return void Return type* @throws */ public static void markImageByCode(Image img, String srcImgPath, String targetPath, int positionWidth, int positionHeight) { OutputStream os = null; try { Image srcImg = ImageIO.read(new File(srcImgPath)); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); // 1. Get the brush object Graphics2D g = buffImg.createGraphics(); // 2. Set the jagged edge processing of the line segment g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage( srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 3. QR code location g.drawImage(img, positionWidth, positionHeight, null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); // 4. Release the resource g.dispose(); // 5. Generate image (it is recommended to generate PNG, jpg will be distorted) os = new FileOutputStream(targerPath); ImageIO.write(buffImg, "PNG", os); System.out.println("QR code image generation successful"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != os) os.close(); } catch (Exception e) { e.printStackTrace(); } } } 4). Add a card number to the card
/** * * @Title: pressText * @Description: Add text to the specified position of the image* @param pressText * Text content* @param srcImageFile * Original image* @param destImageFile * Target image* @param x * horizontal coordinate* @param y * vertical coordinate* @param alpha * Transparency* @return void Return type* @throws */ public final static void pressText(String pressText, String srcImageFile, String destImageFile, int x, int y, float alpha) { try { File img = new File(srcImageFile); Image src = ImageIO.read(img); int width = src.getWidth(null); int height = src.getHeight(null); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); // Open text anti-aliasing to remove text glitch g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g.drawImage(src, 0, 0, width, height, null); // Set color g.setColor(new Color(89, 87, 87)); // Set Font g.setFont(new Font("Fangzheng Lanting Black_GBK", Font.BOLD, 14)); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // The first parameter -> set content, the next two parameters -> coordinate position of text on the picture (x,y). g.drawString(pressText, x, y); g.dispose(); ImageIO.write((BufferedImage) image, "PNG", new File(destImageFile));// Output to file stream} catch (Exception e) { e.printStackTrace(); } } Example:
Code:
Test code
public class codeTest { public static void main(String[] args) throws Exception { String text = "http://www.xxx.com/"; // QR code content// Generate QR code// Generate image QR code to store directory String targetPath = "f:/qrcode/targetimg/" + Utils.toStr(); //Create directory Utils.makeDirs(targetPath); int begin = 100;//code start number int end = 101;//code end number for (int i = begin; i <= end; i++) { //Generate 16-digit numbers with dates, such as 20161214000001 String code = Utils.toStr() + Utils.formateNumber(i); //Get the QR code object BufferedImage image = Utils.toBufferedImage(text + "?payCode=" + code,240,240); //Generate a picture of a standing card with a background image + QR code Utils.markImageByCode(image, "f:/qrcode/srcimg/src.png", targetPath + "/" + code + ".png", 340, 160); // Add the code number of the standing card to the figure Utils.pressText(code, targetPath + "/" + code + ".png", targetPath + "/" + code + ".png", 390, 417, 0.5f); } // Generate QR code} } Effect:
The batch generated image renderings are as follows
Batch Picture:
utils code:
package cn.utils.code; import java.awt.AlphaComposite; import java.awt.Color; import java.awt.Font; import java.awt.Graphics2D; import java.awt.Image; import java.awt.RenderingHints; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileOutputStream; import java.io.OutputStream; import java.text.DecimalFormat; import java.text.SimpleDateFormat; import java.util.Date; import java.util.Hashtable; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix; /** Tool class. */ public abstract class Utils { /** Date format: yyyy-MM-dd HH:mm:ss */ public static String DF_DATETIME = "yyyyMMdd"; private static float alpha = 1f; /** * * @Title: toBufferedImage * @Description: Convert text into QR code image object* @param text * QR code content* @param width * QR code height* @param height * Two-digit width* @param * @param Exception * Set file* @return BufferedImage Return type* @throws */ public static BufferedImage toBufferedImage(String text, int width, int height) throws Exception { int BLACK = 0xFF000000; int WHITE = 0xFFFFFFFF; Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.CHARACTER_SET, "utf-8"); // The character set encoding used by the content is hints.put(EncodeHintType.MARGIN, 1); BitMatrix matrix = new MultiFormatWriter().encode(text, BarcodeFormat.QR_CODE, width, height, hints); 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; } /** * * @Title: markImageByCode * @Description: Add QR code to the specified position of the image* @param img * QR code image object* @param srcImgPath * Background image* @param targetPath * Target map* @param positionWidth * Position horizontal coordinate* @param positionHeight * Position vertical coordinate* @return void Return type* @throws */ public static void markImageByCode(Image img, String srcImgPath, String targetPath, int positionWidth, int positionHeight) { OutputStream os = null; try { Image srcImg = ImageIO.read(new File(srcImgPath)); BufferedImage buffImg = new BufferedImage(srcImg.getWidth(null), srcImg.getHeight(null), BufferedImage.TYPE_INT_RGB); // 1. Get the brush object Graphics2D g = buffImg.createGraphics(); // 2. Set the jagged edge processing of the line segment g.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR); g.drawImage( srcImg.getScaledInstance(srcImg.getWidth(null), srcImg.getHeight(null), Image.SCALE_SMOOTH), 0, 0, null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // 3. QR code location g.drawImage(img, positionWidth, positionHeight, null); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_OVER)); // 4. Release the resource g.dispose(); // 5. Generate an image (it is recommended to generate PNG, jpg will be distorted) os = new FileOutputStream(targerPath); ImageIO.write(buffImg, "PNG", os); System.out.println("QR code image generation successful"); } catch (Exception e) { e.printStackTrace(); } finally { try { if (null != os) os.close(); } catch (Exception e) { e.printStackTrace(); } } } /** * * @Title: pressText * @Description: Add text to the specified position of the image* @param pressText * Text content* @param srcImageFile * Original image* @param destImageFile * Target image* @param x * horizontal coordinate* @param y * vertical coordinate* @param alpha * Transparency* @return void Return type* @throws */ public final static void pressText(String pressText, String srcImageFile, String destImageFile, int x, int y, float alpha) { try { File img = new File(srcImageFile); Image src = ImageIO.read(img); int width = src.getWidth(null); int height = src.getHeight(null); BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); Graphics2D g = image.createGraphics(); // Open text anti-aliasing to remove text glitch g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); g.drawImage(src, 0, 0, width, height, null); // Set color g.setColor(new Color(89, 87, 87)); // Set Font g.setFont(new Font("Fangzheng Lanting Black_GBK", Font.BOLD, 14)); g.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha)); // The first parameter -> the set content, the next two parameters -> the coordinate position of the text on the image (x,y) . g.drawString(pressText, x, y); g.dispose(); ImageIO.write((BufferedImage) image, "PNG", new File(destImageFile)); // Output to file stream} catch (Exception e) { e.printStackTrace(); } } // Date to string/** Format the date as String, the default format is yyyy-MM-dd HH:mm:ss, the default date is the current date. */ public static String toStr() { return toStr(DF_DATETIME); } /** Format the date as String, the format is specified by the parameter format, the default date is the current date, the format value can be used in constants of this class or customized. */ public static String toStr(String format) { return toStr(format, new Date()); } /** Format the date as String, the default format is yyyy-MM-dd HH:mm:ss, the date is specified by the parameter date. */ public static String toStr(Date date) { return toStr(DF_DATETIME, date); } /** Format the date as String, the format is specified by the parameter format, and the date is specified by the parameter date. The format value can be used in constants of this class or customized. */ public static String toStr(String format, Date date) { return new SimpleDateFormat(format).format(date); } public static String formatNumber(int num) { DecimalFormat df = new DecimalFormat("000000"); String str2 = df.format(num); return str2; } public static String formatNumber(int num) { DecimalFormat df = new DecimalFormat("000000"); String str2 = df.format(num); return str2; } public static boolean makeDirs(String filePath) { File folder = new File(filePath); return (folder.exists() && folder.isDirectory()) ? true : folder .mkdirs(); } } Techniques used:
1. Use the zxing tool to generate QR code.
1) Download address: http://repo1.maven.org/maven2/com/google/zxing/javase/3.1.0/
2).maven configuration
<dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>2.2</version> </dependency>
The above is all the content of this article. I hope it will be helpful to everyone's learning and I hope everyone will support Wulin.com more.