Use zxing batch in the designated position of a good standing card background map, the specified text content (link address, text, etc.) generated two-dimensional code and placed in the position, and finally add the sign number.
Steps:
1). Do the background map, the following figure:
2. Generate two-dimensional code BufferedImage objects. The code is as follows:
/** * * @Title: Tobufferedimage * @Description: Convert text into two-dimensional code picture Object * @param text * Two-dimensional code content * @param width
* Two-dimensional code Height * @param Height * Two-bit 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 content uses character set encoding 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 two-dimensional code at the specified position on the background map, the code is as follows:
/** * * @Title: Markimagebycode * @Description: Add two D code to the picture specified position * @param img * Two-dimensional code Image Object * @param srcim Gpath * Background image * @param targerpath * Target map * @param positionwidth * Position Horizontal axis * @param positionheight * Position ordinate * @return void return type * @throws */public static void Markimagebycode (Image img, String srcimgpath, S
Tring Targerpath, 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 of the line to handle G.setrenderinghint (Renderinghints.key_interpolation, Renderinghints.value_interpolation_bilin
EAR); 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, two-dimensional code position g.drawimage (IMG, positionwidth, positionheight, NULL);
G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_over));
4, Release resources g.dispose ();
5, generate pictures (recommended to generate PNG, JPG will be distorted) OS = new FileOutputStream (Targerpath);
Imageio.write (buffimg, "PNG", OS);
System.out.println ("Two-dimensional code picture Generation Success");
catch (Exception e) {e.printstacktrace ();
Finally {try {if (null!= OS) os.close ();
catch (Exception e) {e.printstacktrace ();
}
}
}
4). Add a standing card number to the sign
/** * * @Title: Presstext * @Description: Add text to the picture at the specified position * @param presstext * Text content * @param srcimagefile * Original picture * @param destimagefile * Target picture * @param x * Horizontal Axis * @param y * ordinate * @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 go to Text Burr G.setrenderinghint (renderinghints.key_text_antialiasing, renderinghints.value_text_antialias_on)
;
G.drawimage (SRC, 0, 0, width, height, null);
Sets the color G.setcolor (new color (89, 87, 87)); Set font g.setfont (new font ("Founder Blue Pavilion in Black _gbk", FonT.bold, 14));
G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_atop, Alpha));
The first parameter-> the contents of the set, followed by two parameters-> the coordinates of the 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.printstack
Trace ();
}
}
Example:
Code:
Test code
public class Codetest {public
static void Main (string[] args) throws Exception {
String text = ' http://www.xxx.co m/"; Two-dimensional code content
//Generate two-dimensional code
//Generate picture two-dimensional code storage directory
String targetpath = "f:/qrcode/targetimg/" + utils.tostr ();
Create a directory
utils.makedirs (TargetPath);
int begin = 100;//code start number
int end = 101;//code ending number
for (int i = begin; I <= end; i++) {
//Generate 16 digits with date as 2 0161214000001
String code = UTILS.TOSTR () + utils.formatenumber (i);
Gets the two-dimensional code object
bufferedimage image = Utils.tobufferedimage (text
+ "? paycode=" + code,240,240);
Generate a utils.markimagebycode with a background image + two-dimensional code
(image, "F:/qrcode/srcimg/src.png",
TargetPath + "/" + code + ". png") , 160, etc.);
The picture of the card plus the code number
Utils.presstext (code, TargetPath + "/" + code + ". png", TargetPath
+ "/" + code + ". png", 390, 41 7, 0.5f);
Generate two-dimensional code
}
}
Effect:
Batch-generated picture effects are shown below
Batch diagram:
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 two-dimensional code picture Object * @param text * Two-dimensional code content * @param width * Two-dimensional code Height * @param Height * Two-bit width * @param * @param ExCeption * Set File * @return bufferedimage return type * @throws/public static bufferedimage Tobufferedimage (Strin
g 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 content uses character set encoding 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 two D code to the picture specified position * @param img * Two-dimensional code Image Object * @param Srcimgpath * Background image * @param targerpath * Target map * @param POSitionwidth * Position Horizontal coordinate * @param positionheight * position ordinate * @return void return type * @throws/public static void Markimagebycode (Image img, string Srcimgpath, string targerpath, int positionwidth, int positionheight) {out
Putstream 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 of the line to handle G.setrenderinghint (Renderinghints.key_interpolation, Renderinghints.value_interpolation_bilin
EAR); 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, two-dimensional code position g.drawimage (IMG, positionwidth, positionheight, NULL); G.setcomposite (Alphacomposite. getinstance (Alphacomposite.src_over));
4, Release resources g.dispose ();
5, generate pictures (recommended to generate PNG, JPG will be distorted) OS = new FileOutputStream (Targerpath);
Imageio.write (buffimg, "PNG", OS);
System.out.println ("Two-dimensional code picture Generation Success");
catch (Exception e) {e.printstacktrace ();
Finally {try {if (null!= OS) os.close ();
catch (Exception e) {e.printstacktrace (); }}/** * * @Title: Presstext * @Description: Add text to the picture at the specified position * @param presstext * Text content * @param Srcimagefile * Original picture * @param destimagefile * Target picture * @param x * Horizontal Axis * @param y * ordinate * @par AM Alpha * Transparency * @return void return type * @throws/public final static void Presstext (String presstext, Strin
G 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 go to Text Burr G.setrenderinghint (renderinghints.key_text_antialiasing, renderinghints.value_text_antialias_on)
;
G.drawimage (SRC, 0, 0, width, height, null);
Sets the color G.setcolor (new color (89, 87, 87));
Set font g.setfont (new font ("founder Orchid Pavilion in Black _gbk", Font.Bold, 14));
G.setcomposite (Alphacomposite.getinstance (alphacomposite.src_atop, Alpha));
The first parameter-> the contents of the set, followed by two parameters-> the coordinates of the 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.printstack
Trace (); }//Date-/** The date is formatted as String, the default is Yyyy-mm-dd HH:mm:ss, and the default date is the current date.
*/public static String Tostr () {return tostr (df_datetime); /** formats the date as string, the format is specified by the parameter format, the default date is the current date, and the format value can use this class of constants or customizations. */public static String ToSTR (String format) {return TOSTR (format, New Date ()); /** formats the date as string, the default format is Yyyy-mm-dd HH:mm:ss, and the date is specified by the parameter date.
*/public static String tostr (date date) {return tostr (df_datetime, date); /** formats the date as string, the format is specified by the parameter format, the date is specified by the parameter date, and the format value can use this class of constants or customizations.
*/public static string Tostr (String format, date date) {return new SimpleDateFormat (format). format (date);
public static String formatenumber (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 ();
}
}
Technology to use:
1. Use the zxing to generate a two-dimensional code tool.
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>
The above is the entire content of this article, I hope to help you learn, but also hope that we support the cloud habitat community.