Introduced
The main use of the Goole zxing
package, the following given the sample code, very convenient for everyone's understanding and learning, the code is a preliminary framework, the function has to be based on the actual use of perfect optimization.
First step, MAVEN import zxing
<dependency>
<groupId>com.google.zxing</groupId>
<artifactid>core</ artifactid>
<version>3.2.1</version>
</dependency>
Step two, start generating two-dimensional code:
private static final int black = 0xff000000;
private static final int white = 0xFFFFFFFF; /** * Deposit The generated two-dimensional code into the picture * @param Matrix zxing Package of two-dimensional code class * @return/public static bufferedimage tobufferedimage (Bitmatrix ma
Trix) {int width = matrix.getwidth ();
int height = matrix.getheight ();
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; /** * Generate two-dimensional code and write to file * @param content Scan two-dimensional code contents * @param format picture format jpg * @param file * @throws Exception/Publi c static void WriteToFile (string content, string format, file file) throws Exception {Multiformatwriter Multiformatwri
ter = new Multiformatwriter ();
@SuppressWarnings ("Rawtypes") Map hints = new HashMap ();
Set UTF-8 to prevent Chinese garbled hints.put (encodehinttype.character_set, "UTF-8");
Set the size of the white area around the two-dimensional code hints.put (encodehinttype.margin,1); To set the fault tolerance of two dimensional codes
Hints.put (Encodehinttype.error_correction, ErrorCorrectionLevel.H);
Draw two-dimensional code Bitmatrix Bitmatrix = Multiformatwriter.encode (content, Barcodeformat.qr_code, WIDTH, HEIGHT, hints);
BufferedImage image = Tobufferedimage (Bitmatrix); if (! Imageio.write (image, format, file)) {throw new IOException ("Could not write a image of format" + format + "to" + fi
Le); }
}
Third step, writing text to two-dimensional code picture:
/** * to the two-dimensional code picture plus text * @param presstext text * @param qrfile two-dimensional code file * @param fontstyle * @param color * @param fontsize */public static void Presstext (String presstext, File qrfile, int fontstyle, color color, int fontsize) throws Exception
{presstext = new String (Presstext.getbytes (), "utf-8");
Image src = imageio.read (qrfile);
int imagew = Src.getwidth (null);
int imageh = Src.getheight (null);
BufferedImage image = New BufferedImage (Imagew, Imageh, Bufferedimage.type_int_rgb);
Graphics g = image.creategraphics ();
G.drawimage (SRC, 0, 0, Imagew, imageh, NULL);
Sets the color g.setcolor the brush;
To set fonts font font = new Font ("XXFarEastFont-Arial", FontStyle, FontSize);
FontMetrics metrics = g.getfontmetrics (font);
Text in the coordinates of the picture is set here in the middle int startx = (width-metrics.stringwidth (presstext))/2;
int starty = HEIGHT/2;
G.setfont (font);
g.DrawString (Presstext, StartX, starty);
G.dispose ();
FileOutputStream out = new FileOutputStream (qrfile);
Imageio.write (Image, "JPEG", out);
Out.close (); SysTem.out.println ("Image press success"); }
The fourth step, in the Main method test, a middle with text two-dimensional code is generated
public static void Main (string[] args) throws Exception {
file Qrcfile = new file ("/users/deweixu/", "google.jpg");
writetofile ("www.google.com.hk", "jpg", qrcfile);
Presstext ("Google", Qrcfile, 5, color.red,);
Summarize
The above is the entire content of this article, I hope the content of this article for everyone's study or work can bring certain help, if you have questions you can message exchange.