After reading the QR code today, I felt good. I also collected a demo, saying that the demo was actually modified based on the basic functions.
1. Set up the environment. The required jar packages include
Here is the online reference demo.
Where
To make modifications based on the original and open-source packages.
2. QR code color modification introduction.
Public class myzxingencoderhandler {/*** @ Param contents: name of the image generated * @ Param width: width of the image generated * @ Param Height: height of the image generated * @ Param imgpath image path */Public void encode (string contents, int width, int height, string imgpath, string logopath) {hashtable <encodehinttype, Object> hints = new hashtable <encodehinttype, Object> (); // specify the error correction level hints. put (encodehinttype. error_correction, errorcorrectionlevel. l); // specify the hints encoding format. put (encodehinttype. character_set, "GBK"); try {// set the type of the generated QR code bitmatrix = new multiformatwriter (). encode (contents, barcodeformat. qr_code, width, height, hints); // 0xffe30022 font color, 0xfff4c2c2 background color mymatrixtoimagewriter. writetofile (bitmatrix, "PNG", imgpath, new mymatrixtoimageconfig (0xffe30022, 0xfff4c2c2), logopath);} catch (exception e) {e. printstacktrace () ;}} public static void main (string [] ARGs) {string imgpath = "zxing.png"; string logopath = "logo.png"; string contents = http: // localhost: 8080/demo; int width = 300, Height = 300; myzxingencoderhandler handler = new myzxingencoderhandler (); handler. encode (contents, width, height, imgpath, logopath); system. out. println ("Michael, you have finished zxing encode. ");}}
/*** Add a custom logo (key part) */public static void overlapimage (bufferedimage image, string imgpath, string logopath) {try {bufferedimage logo = ImageIO. read (new file (logopath); graphics2d G = image. creategraphics ();
// Logo width and height int width = image. getwidth ()/5; int Height = image. getheight ()/5;
// Start position of the logo, which is used to display int x = (image. getwidth ()-width)/2; int y = (image. getheight ()-height)/2; G. drawimage (logo, X, Y, width, height, null); G. dispose (); ImageIO. write (image, "PNG", new file (imgpath);} catch (exception e) {e. printstacktrace ();}}
DetailsCodeFor more information, see the mymatrixtoimagewriter. Java class.
Logo:
Finally:
The resource connections used in this article are:
Click Open Link