Involves two aspects of knowledge: 1.QR code basics, see Daniel works http://coolshell.cn/articles/10590.html 2. the basic idea of the java graphics library image and Graphics2D is to first use the QR code image generated by zxing, then read the image, insert the icon in it, and then output the entire image. Another way is to create a QR code image and directly overwrite the icon on the QR code. This will cause loss of some data. However, due to the error correction capability of the QR code, the scanner can retrieve the lost information. Without talking nonsense, go directly to the Code:
/*****/Package barcode; import java. awt. color; import java. awt. graphics2D; import java. awt. image; import java. awt. geom. affineTransform; import java. awt. image. affineTransformOp; import java. awt. image. bufferedImage; import java. io. file; import java. io. IOException; import java. util. hashMap; import java. util. map; import javax. imageio. imageIO; import com. google. zxing. barcodeFormat; import com. google. z Xing. encodeHintType; import com. google. zxing. multiFormatWriter; import com. google. zxing. writerException; import com. google. zxing. common. bitMatrix; import com. google. zxing. qrcode. decoder. errorCorrectionLevel;/*** @ author lichunxi **/public class BarcodeFactory {// General private static final int IMAGE_WIDTH = 80; private static final int IMAGE_HEIGHT = 80; private static final int IMAGE_HALF _ WIDTH = IMAGE_WIDTH/2; private static final int FRAME_WIDTH = 2; // two-dimensional code: private static MultiFormatWriter mutiWriter = new MultiFormatWriter (); public static void encode (String content, int width, int height, String srcImagePath, String destImagePath) {try {ImageIO. write (genBarcode (content, width, height, srcImagePath), "jpg", new File (destImagePath);} catch (IOException e) {e. printStackT Race ();} catch (WriterException e) {e. printStackTrace () ;}} private static BufferedImage genBarcode (String content, int width, int height, String srcImagePath) throws WriterException, IOException {// read source image BufferedImage scaleImage = scale (srcImagePath, IMAGE_WIDTH, IMAGE_HEIGHT, true); int [] [] srcPixels = new int [IMAGE_WIDTH] [IMAGE_HEIGHT]; for (int I = 0; I <scaleImage. getWidth (); I ++) {(Int j = 0; j <scaleImage. getHeight (); j ++) {srcPixels [I] [j] = scaleImage. getRGB (I, j) ;}} Map <EncodeHintType, Object> hint = new HashMap <EncodeHintType, Object> (); hint. put (EncodeHintType. CHARACTER_SET, "UTF-8"); hint. put (EncodeHintType. ERROR_CORRECTION, ErrorCorrectionLevel. h); // generate the QR code BitMatrix matrix = mutiWriter. encode (content, BarcodeFormat. QR_CODE, width, height, hint); // convert a two-dimensional matrix to a one-dimensional pixel array Int halfW = matrix. getWidth ()/2; int halfH = matrix. getHeight ()/2; int [] pixels = new int [width * height]; for (int y = 0; y <matrix. getHeight (); y ++) {for (int x = 0; x <matrix. getWidth (); x ++) {// read image if (x> halfW-IMAGE_HALF_WIDTH & x