This week, there is a project to generate two-dimensional code, study the use of Google's zxing to generate two-dimensional code, found that the effect can also be recorded here. If you need a more customized QR code, you can also connect to a third-party API service to generate a QR code.
Two-dimensional code generation:
@Component ("Qrcodehandler") Public classQrcodehandlerimplImplementsQrcodehandler {Private Static FinalLogger Logger = Loggerfactory.getlogger (Qrcodehandlerimpl.class); /**Two-dimensional code color*/ Private Static Final intFront_color = 0xff000000;//Front Color Black Private Static Final intBack_color = 0xFFFFFFFF;//White background color /**encoding Format*/ Private Static FinalString character_set = "UTF-8"; /**type of picture*/ Private Static FinalString FORMAT = "png"; /**Two-dimensional code size*/ Private Static Final intQrcode_size = 300; /**Logo Information*/ Private Static Final intLogo_size = 60; Private Static FinalHashmap<encodehinttype, object>hints; Static{hints=NewHashmap<encodehinttype, object>(); Hints.put (Encodehinttype.character_set, Character_set); Hints.put (Encodehinttype.error_correction, ERRORCORRECTIONLEVEL.M); Hints.put (Encodehinttype.margin,1); } /** * Generate a simple QR code * * @param contents The actual content of the QR code, including URL, text, files, etc., such as http://www.cnblogs.com/ark-blog/* @param d Estfile generated two-dimensional code file * / Public voidGeneratesimpleqrcode (String contents, File destfile) {Try { //1. Generating a bit matrixBitmatrix Bitmatrix =NewMultiformatwriter (). Encode (contents, Barcodeformat.qr_code, Qrcode_size, qrcode_size, hints); //2. Save the pictureMatrixtoimagewriter.writetopath (Bitmatrix, FORMAT, Destfile.topath ()); } Catch(Exception e) {logger.error ("#generateSimpleQrcode ()-Create QRCode failed, exception=[{}]", E); ThrowBusinessexception.newinstance ("$", "Create QRCode failed"); } } /** * Create a QR code with logo * * @param contents The actual content of the QR code, including URL, text, files, etc., such as http://www.cnblogs.com/ark-blog/* @para M logofile logo * @param destfile generated QR code file */ Public voidGeneratelogoqrcode (String contents, file logofile, file destfile) {Try { //1. Create a bit matrix diagramMultiformatwriter Multiformatwriter =NewMultiformatwriter (); Bitmatrix Bitmatrix=Multiformatwriter.encode (contents, Barcodeformat.qr_code, Qrcode_size, qrcode_size, hints); BufferedImage Qrcodeimage=Newbufferedimage (qrcode_size, Qrcode_size, Bufferedimage.type_int_rgb); for(intx = 0; x < qrcode_size; X + +) { for(inty = 0; Y < qrcode_size; y++) {Qrcodeimage.setrgb (x, Y, Bitmatrix.get (x, y)?Front_color:back_color); } } //2. Draw the logo on the matrix chartImage Logoimage =Imageio.read (Logofile); BufferedImage Logobufferedimage=Newbufferedimage (logo_size, Logo_size, Bufferedimage.type_int_rgb); Logobufferedimage.getgraphics (). DrawImage (Logoimage,0, 0,NULL); intPosition = (qrcode_size-logo_size)/2; Graphics2D graphics2d=Qrcodeimage.creategraphics (); Graphics2d.drawimage (logobufferedimage, position, position,NULL); Graphics2d.dispose (); //3. Save the pictureQrcodeimage.flush (); Imageio.write (Qrcodeimage, FORMAT, destfile); } Catch(Exception e) {logger.error ("#generateLogoQrcode ()-Create QRCode failed, exception=[{}]", E); ThrowBusinessexception.newinstance ("$", "Create QRCode failed"); } }}
Java generates two-dimensional code