Java Generate business Card-type QR code source sharing

Source: Internet
Author: User
Tags string format

25% of the world's people have procrastination--but I think it's definitely less, at least I'm a patient with procrastination. Always want to "Java generated business card (with background image, user network picture, user nickname) of the two-dimensional code" this blog share, but has been dragged and dragged, dragged to the present, really should be the Scottish proverb-" when you can do things, often do not do." ”

Zero

    1. The top left corner is the avatar.
    2. The silence of the King two is the nickname of words.
    3. Two-dimensional code with URL Http://blog.csdn.net/qing_gee
    4. There is also the specified background map.

Usage scenarios:

Point to the public number menu "My QR code" and then show a card-type QR code to the user.

First, the source code download

Https://github.com/qinggee/qrcode-utils can be downloaded directly from GitHub.

Ii. Introduction of source code

You must have seen a lot of Java generation of QR code with logo in the network, these are the primary application of generating QR code. Relatively speaking, the generation of "business card (with background image, user network picture, user name of the QR code image) of the QR Code" may be more advanced, but the intrinsic principle is actually similar-in a specified Picture object graphics2d using the DrawImage () method to draw the upper image, Draw text with drawstring.

2.1 Using the interface

File location:/qrcode-utils/src/test/qrcodeutilstest.java

matrixtobgimageconfig config = new Matrixtobgimageconfig (); //network Avatar Address Config.setheadimgurl ("https://avatars2.githubusercontent.com/u/6011374?v=4& Amp;u=7672049c1213f7663b79583d727e95ee739010ec&s=400 "); //two-dimensional code address, scan QR code to jump address Config.setqrcode_url ( "/http" Blog.csdn.net/qing_gee "); //two-dimensional code name on the business card config.setrealname ( "Silence King II"); byte[] bytes = qrcodeutils.createqrcode (config) via Qrcodeutils.createqrcode () ; //two-dimensional code generation path Paths Path = Files.createtempfile ( "Qrcode_with_bg_", //write to File files.write (path, bytes);         
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16

If you download the source from github, you can import the project into your work library directly through Eclipse and run/qrcode-utils/src/test/qrcodeutilstest.java to generate the QR code.

2.2 Description of the catalog file

    1. The Core class is Qrcodeutils.java (used to generate the QR code)
    2. Parameter class Matrixtobgimageconfig.java of business card type QR code
    3. Test Case Qrcodeutilstest.java
    4. RES resource bundle has two pictures, bg.jpg for the specified background, default_headimg.jpg for the default avatar image
    5. /qrcode-utils/lib for the required jar package
2.3 qrcodeutils.java2.3.1 Get background

Note the first line of code in the following code.

InputStream inputStream = Thread.currentThread().getContextClassLoader()                    .getResourceAsStream(config.getBgFile());File bgFile = Files.createTempFile("bg_", ".jpg").toFile();FileUtils.copyInputStreamToFile(inputStream, bgFile);
    • 1
    • 2
    • 3
    • 4
    • 1
    • 2
    • 3
    • 4
2.3.2 Get Avatar

Get the Avatar by creating a httpget request.

Closeablehttpclient httpclient = Httpclientbuilder.create (). build (); HttpGet HttpGet =New HttpGet (Config.getheadimgurl ()); Httpget.addheader ("Content-type","Text/html;charset=utf-8");Configure the Request timeout setting Requestconfig Requestconfig = Requestconfig.custom (). Setconnectionrequesttimeout (). Setconnecttimeout (). SetSocketTimeout (Build (); Httpget.setconfig (Requestconfig);Try (closeablehttpresponse response = Httpclient.execute (HttpGet); InputStream Headimgstream = handleresponse (response);) {header[] Contenttypeheader = Response.getheaders ("Content-type");if (contenttypeheader! =null && contenttypeheader.length > 0) {if (Contenttypeheader[0].getvalue (). StartsWith (Contenttype.application_ Json.getmimetype ())) {//application/json; encoding=utf-8 Download media file error String responsecontent = Handleutf8response (response); Logger.warn ( "Download network avatar Error {}", responsecontent);}} Headimgfile = Createtmpfile (Headimgstream,  "Headimg_" + uuid.randomuuid (), " jpg "); catch (Exception e) {logger.error (E.getmessage (), E); throw new Exception ( "Avatar file read wrong! ", e);} finally {httpget.releaseconnection ();}        
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29

Download the image locally using the Createtmpfile method.

Publicstatic File createtmpfile (InputStream InputStream, String name, String ext) throws ioexception {File tmpfile = file.createtempfile (name, " + ext); Tmpfile.deleteonexit (); try (fileoutputstream fos = new FileOutputStream (TmpFile)) { Span class= "Hljs-keyword" >int read = 0; byte[] bytes = new byte[1024 * 100]; while ((read = Inputstream.read (bytes))! =-1) {fos.write (bytes , 0, read); } fos.flush (); return tmpfile;} } 
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
2.3.3 Drawing a QR code, avatar, nickname on a background map
PrivateStaticvoidIncreasingimage (bufferedimage image, string format, String ImagePath, File bgfile, Matrixtobgimageconfig config, Fi Le Headimgfile)Throws Exception {try {bufferedimage bg = imageio.read (bgfile); Graphics2D g = bg.creategraphics ();How the height and width of the QR code are definedint width = config.getqrcode_height ();int height = config.getqrcode_height ();Logo start position, this purpose is to center the logo displayint x = config.getqrcode_x ();int y = config.getqrcode_y ();Draw Diagram G.drawimage (image, X, y, width, height,NULL); BufferedImage headimg = Imageio.read (headimgfile);int headimg_width = Config.getheadimg_height ();int headimg_height = Config.getheadimg_height ();int headimg_x = config.getheadimg_x (); int headimg_y = config.getheadimg_y (); //drawing avatar G.drawimage (headimg, headimg_x, headimg_y, Headimg_width, Headimg_height, null); //Draw text G.setcolor (Color.gray); //text color font font = new font ( "Arial", Font.Bold, 28); G.setfont (font); g.DrawString (Config.getrealname (), config.getrealname_x (), config.getrealname_y ()); G.dispose (); //write QR code to BG picture imageio.write (BG, format, new File (ImagePath));} catch (Exception e) {throw new Exception ( "two-dimensional code added BG when an exception occurred! ", e);}               
      1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • Ten
    • one
    • 2
    • (
    • )
    • +
    • +
    • /
    • 0
    • +
    • all
    • +
    • +
    • +
    • -
    • 29
    • +
    • +
    • all
    • +
    • +
    • PNS
    • up
    • i>39
    • (
    • )
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21st
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42

Well, the source is introduced here first.

http://blog.csdn.net/qing_gee/article/details/77341821

Java Generate business Card-type QR code source sharing

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.