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.