Java version QR code instance (non-Android version)

Source: Internet
Author: User

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

 

 

 

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.