Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use java to generate QR Code

2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

This article mainly shows you "how to use java to generate QR code", the content is easy to understand, clear, hope to help you solve doubts, the following let Xiaobian lead you to study and learn "how to use java to generate QR code" this article.

QR code is a kind of bar code picture which uses black and white plane geometry to record text, picture, web address and other information through the corresponding coding algorithm.

The characteristics of QR code:

1. High density coding, large information capacity

It can hold up to 1850 uppercase letters or 2710 numbers or 1108 bytes, or more than 500 Chinese characters, which is about dozens of times higher than the information capacity of ordinary bar codes.

two。 Wide coding range

The bar code can encode the digital information such as picture, sound, text, signature, fingerprint and so on, which can be expressed by bar code; it can represent multiple languages; it can represent image data.

3. Strong fault tolerance and error correction function

This makes the two-dimensional bar code can still be correctly read when it is partially damaged by piercing, defacing, etc., and the information can still be recovered when the damaged area reaches 50%.

4. High decoding reliability

Its decoding error rate is much lower than that of ordinary bar code 2/1000000, and the bit error rate is less than 1/10000000.

5. Encryption measures can be introduced

Good confidentiality and anti-counterfeiting.

6. Low cost, easy to make, durable

Because of these characteristics, QR codes are becoming more and more popular and widely used (see Baidu encyclopedia for details, but the introduction is not the focus of this article), so knowing how to develop QR codes is a very good knowledge reserve. so this blog post will show you how to generate and parse QR codes.

Required jar package: QRCode.jar

Download address; http://download.csdn.net/detail/lidew521/5542191

a. New java Project: RCodeTest

b. Add jar packages supported by QR codes:

-

c. Execute the following class to generate a QR code picture:

/ RCodeTest/src/com/credream/rcode/TwoDimensionCode.java

Package com.credream.rcode

Import java.awt.Color

Import java.awt.Graphics2D

Import java.awt.image.BufferedImage

Import java.io.File

Import java.io.IOException

Import java.io.InputStream

Import java.io.OutputStream

Import javax.imageio.ImageIO

Import jp.sourceforge.qrcode.QRCodeDecoder

Import jp.sourceforge.qrcode.exception.DecodingFailedException

Import com.swetake.util.Qrcode

Public class TwoDimensionCode {

/ * *

* generate a QRCode image

* @ param content stores content

* @ param imgPath Picture path

, /

Public void encoderQRCode (String content, String imgPath) {

This.encoderQRCode (content, imgPath, "png", 7)

}

/ * *

* generate a QRCode image

* @ param content stores content

* @ param output output stream

, /

Public void encoderQRCode (String content, OutputStream output) {

This.encoderQRCode (content, output, "png", 7)

}

/ * *

* generate a QRCode image

* @ param content stores content

* @ param imgPath Picture path

* @ param imgType Picture Type

, /

Public void encoderQRCode (String content, String imgPath, String imgType) {

This.encoderQRCode (content, imgPath, imgType, 7)

}

/ * *

* generate a QRCode image

* @ param content stores content

* @ param output output stream

* @ param imgType Picture Type

, /

Public void encoderQRCode (String content, OutputStream output, String imgType) {

This.encoderQRCode (content, output, imgType, 7)

}

/ * *

* generate a QRCode image

* @ param content stores content

* @ param imgPath Picture path

* @ param imgType Picture Type

* @ param size QR code size

, /

Public void encoderQRCode (String content, String imgPath, String imgType, int size) {

Try {

BufferedImage bufImg = this.qRCodeCommon (content, imgType, size)

File imgFile = new File (imgPath)

/ / generate QR code QRCode picture

ImageIO.write (bufImg, imgType, imgFile)

} catch (Exception e) {

E.printStackTrace ()

}

}

/ * *

* generate a QRCode image

* @ param content stores content

* @ param output output stream

* @ param imgType Picture Type

* @ param size QR code size

, /

Public void encoderQRCode (String content, OutputStream output, String imgType, int size) {

Try {

BufferedImage bufImg = this.qRCodeCommon (content, imgType, size)

/ / generate QR code QRCode picture

ImageIO.write (bufImg, imgType, output)

} catch (Exception e) {

E.printStackTrace ()

}

}

/ * *

* A common method for generating QRCode images

* @ param content stores content

* @ param imgType Picture Type

* @ param size QR code size

* @ return

, /

Private BufferedImage qRCodeCommon (String content, String imgType, int size) {

BufferedImage bufImg = null

Try {

Qrcode qrcodeHandler = new Qrcode ()

/ / set the error rate of QR code. You can choose L (7%), M (15%), Q (25%), H (30%). The higher the error rate, the less information can be stored, but the less required for the clarity of QR code.

QrcodeHandler.setQrcodeErrorCorrect ('M')

QrcodeHandler.setQrcodeEncodeMode ('B')

/ / set the QR code size. The value ranges from 1 to 40. The larger the value, the larger the size, the greater the information that can be stored.

QrcodeHandler.setQrcodeVersion (size)

/ / get the byte array of the content and set the encoding format

Byte [] contentBytes = content.getBytes ("utf-8")

/ / Image size

Int imgSize = 67 + 12 * (size-1)

BufImg = new BufferedImage (imgSize, imgSize, BufferedImage.TYPE_INT_RGB)

Graphics2D gs = bufImg.createGraphics ()

/ / set background color

Gs.setBackground (Color.WHITE)

Gs.clearRect (0,0, imgSize, imgSize)

/ / set image color > BLACK

Gs.setColor (Color.BLACK)

/ / set offset. Failure to set may result in parsing error.

Int pixoff = 2

/ / output > QR code

If (contentBytes.length > 0 & & contentBytes.length < 800) {

Boolean [] codeOut = qrcodeHandler.calQrcode (contentBytes)

For (int I = 0; I < codeOut.length; iTunes +) {

For (int j = 0; j < codeOut.length; jacks +) {

If (codeOut [j] [I]) {

Gs.fillRect (j * 3 + pixoff, I * 3 + pixoff, 3,3)

}

}

}

} else {

Throw new Exception ("QRCode content bytes length =" + contentBytes.length + "not in [0800].")

}

Gs.dispose ()

BufImg.flush ()

} catch (Exception e) {

E.printStackTrace ()

}

Return bufImg

}

/ * *

* parsing QR Code (QRCode)

* @ param imgPath Picture path

* @ return

, /

Public String decoderQRCode (String imgPath) {

/ / QRCode QR code picture file

File imageFile = new File (imgPath)

BufferedImage bufImg = null

String content = null

Try {

BufImg = ImageIO.read (imageFile)

QRCodeDecoder decoder = new QRCodeDecoder ()

Content = new String (decoder.decode (new TwoDimensionCodeImage (bufImg)), "utf-8")

} catch (IOException e) {

System.out.println ("Error:" + e.getMessage ())

E.printStackTrace ()

} catch (DecodingFailedException dfe) {

System.out.println ("Error:" + dfe.getMessage ())

Dfe.printStackTrace ()

}

Return content

}

/ * *

* parsing QR Code (QRCode)

* @ param input input stream

* @ return

, /

Public String decoderQRCode (InputStream input) {

BufferedImage bufImg = null

String content = null

Try {

BufImg = ImageIO.read (input)

QRCodeDecoder decoder = new QRCodeDecoder ()

Content = new String (decoder.decode (new TwoDimensionCodeImage (bufImg)), "utf-8")

} catch (IOException e) {

System.out.println ("Error:" + e.getMessage ())

E.printStackTrace ()

} catch (DecodingFailedException dfe) {

System.out.println ("Error:" + dfe.getMessage ())

Dfe.printStackTrace ()

}

Return content

}

Public static void main (String [] args) {

String imgPath = "G:/TDDOWNLOAD/Michael_QRCode.png"

/ / String encoderContent = "Hello big and small, welcome to QRCode!" + "\ nMyblog [

Http://sjsky.iteye.com] "+"\ nEMail [sjsky007@gmail.com] "

String encoderContent = "http://www.credream.com";

TwoDimensionCode handler = new TwoDimensionCode ()

Handler.encoderQRCode (encoderContent, imgPath, "png")

/ / try {

/ / OutputStream output = new FileOutputStream (imgPath)

/ / handler.encoderQRCode (content, output)

/ /} catch (Exception e) {

/ / e.printStackTrace ()

/ /}

System.out.println ("= encoder success")

String decoderContent = handler.decoderQRCode (imgPath)

System.out.println ("the parsing result is as follows:")

System.out.println (decoderContent)

System.out.println ("= decoder others!")

}

}

-

d. Here is an auxiliary class:

/ RCodeTest/src/com/credream/rcode/TwoDimensionCodeImage.java

Package com.credream.rcode

Import java.awt.image.BufferedImage

Import jp.sourceforge.qrcode.data.QRCodeImage

Public class TwoDimensionCodeImage implements QRCodeImage {

BufferedImage bufImg

Public TwoDimensionCodeImage (BufferedImage bufImg) {

This.bufImg = bufImg

}

@ Override

Public int getHeight () {

Return bufImg.getHeight ()

}

@ Override

Public int getPixel (int x, int y) {

Return bufImg.getRGB (x, y)

}

@ Override

Public int getWidth () {

Return bufImg.getWidth ()

}

}

The above is all the contents of the article "how to use java to generate QR codes". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report