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.

[Java]
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: The image path generated
*/
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 encoding format
Hints. put (EncodeHintType. CHARACTER_SET, "GBK ");
Try {
// Set the type of the generated QR code
BitMatrix 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 = <A href = "http: // localhost: 8080/demo"> http: // localhost: 8080/demo </A>;
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 .");
}
}

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: The image path generated
*/
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 encoding format
Hints. put (EncodeHintType. CHARACTER_SET, "GBK ");
Try {
// Set the type of the generated QR code
BitMatrix 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 .");
}
} [Java]
/**
* Add a custom logo on the QR code (key part)
*/
Public static void overlapImage (BufferedImage image, String imgPath, String logoPath ){
Try {
BufferedImage logo = ImageIO. read (new File (logoPath ));
Graphics2D g = image. createGraphics ();

/**
* Add a custom logo on the QR code (key part)
*/
Public static void overlapImage (BufferedImage image, String imgPath, String logoPath ){
Try {
BufferedImage logo = ImageIO. read (new File (logoPath ));
Graphics2D g = image. createGraphics (); [java] view plaincopyprint? // Logo width and height
Int width = image. getWidth ()/5;
Int height = image. getHeight ()/5;

// Logo width and height
Int width = image. getWidth ()/5;
Int height = image. getHeight ()/5; [java] view plaincopyprint? // Start position of the logo, which is centered
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 ();
}
}

// Start position of the logo, which is centered
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 ();
}
} For detailed code, refer to the MyMatrixToImageWriter. java class.

 

Related Article

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.