Java simple generation of two-dimensional code

Source: Internet
Author: User

Two-dimensional code introduction

Two-dimensional barcode/two-D code (2-dimensional bar Code,qrcode) is a certain geometric pattern in the plane (two-dimensional direction) distribution of black-and-white graphics to record data symbol information, the code is cleverly used to form the basis of the internal logic of the computer "0", The concept of "1" Bitstream uses several geometric shapes corresponding to the binary to represent literal numerical information, which is automatically read by the image input device or photoelectric scanning device to realize automatic information processing: It has some commonness of bar code technology: Each code system has its specific character set; Each character occupies a certain width , with a certain calibration function. At the same time, it also has the function of information automatic recognition for different lines and the change point of processing graphics rotation. ----from Baidu Encyclopedia Http://baike.baidu.com/link?url=9q1Mc6pObTbmDwvtEtungEjJutYmW-_EGKIn5J3FCa_ Yij3jei8zdzsmouvngnfdeifhygthpyf0pdo8qhxrga

Java Build QRCode

Zebra Crossing (ZXing) is an open source library that can be used to generate and parse QR codes on most platforms (Android, Javase, IPhone, RIM, Symbian). And Qrgen is developed on the basis of zxing, which makes it a piece of cake to generate QR codes using Java. It needs to rely on zxing, so you need zxing and Qrgen jar packages when you generate patterns.

Add QRCode dependencies in Pom.xml:

<dependency>
<groupId>net.glxn</groupId>
<artifactId>qrgen</artifactId>
<version>1.4</version>
</dependency>

The following 3 jar packages are then automatically downloaded in Maven dependencies:

Qrgen-1.4.jar

Javase-3.0.0.jar

Core-3.0.0.jar

The next step is to write a Java program to generate QRCode, roughly speaking, set up the contents of the QR code, and then call the Qrgen API to generate image/png format content, can be saved as a picture file, you can also display the QRCode directly on the Web page.

The Java code looks like this, and QRCode's content is a link to Baidu:

Package Com.test.qrcode;

Import Java.io.ByteArrayOutputStream;
Import Java.io.File;
Import java.io.FileNotFoundException;
Import Java.io.FileOutputStream;
Import java.io.IOException;

Import Net.glxn.qrgen.QRCode;
Import Net.glxn.qrgen.image.ImageType;

public class Qrcodeservice
{
public static void Main (string[] args)
{
String qrcodecontent = "https://www.baidu.com/";
Bytearrayoutputstream out = Qrcode.from (qrcodecontent). to (Imagetype.png). Stream ();

Try
{
FileOutputStream fout = new FileOutputStream (New File ("d:/download/qr_code.jpg"));
Fout.write (Out.tobytearray ());
Fout.flush ();
Fout.close ();
}
catch (FileNotFoundException e)
{
E.printstacktrace ();
}
catch (IOException E)
{
E.printstacktrace ();
}
}
}

Open the saved picture file, you can see the generated Baidu link of the QR code:

650) this.width=650; "src=" http://s3.51cto.com/wyfs02/M00/59/B7/wKioL1TdYH_ggvJ2AAAndGHke-g421.jpg "title=" qr_ Code.jpg "alt=" Wkiol1tdyh_ggvj2aaandghke-g421.jpg "/>

At this point, a simple Java generation QR code sample is finished, you can use the powerful features of Qrgen's API to achieve our specific needs, such as: in the Web page dynamically for the user to generate two-dimensional code.


This article is from the "fireworks easy to cool" blog, please be sure to keep this source http://yuanhuan.blog.51cto.com/3367116/1614228

Java simple generation of two-dimensional code

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.