Java uses zpxing. jar to generate a QR code with a url

Source: Internet
Author: User

Step 1 download the zpxing. jar package and load it in (there are many similar jar packages on the Internet. Here we use the jar provided by Google)

QR code tool:

Package com. zpxing. controller; import java. awt. basicStroke; import java. awt. graphics; import java. awt. graphics2D; import java. awt. image; import java. awt. shape; import java. awt. geom. roundRectangle2D; import java. awt. image. bufferedImage; import java. io. file; import java. io. outputStream; import java. util. hashtable; import java. util. random; import javax. imageio. imageIO; import com. google. zxing. barcodeFormat; import com. google. zxing. binaryBitmap; import com. google. zxing. decodeHintType; import com. google. zxing. encodeHintType; import com. google. zxing. multiFormatReader; import com. google. zxing. multiFormatWriter; import com. google. zxing. result; import com. google. zxing. common. bitMatrix; import com. google. zxing. common. hybridBinarizer; import com. google. zxing. qrcode. decoder. errorCorrectionLevel;/*** QR code tool class **/public class QRCodeUtil {private static final String CHARSET = "UTF-8"; private static final String FORMAT_NAME = "JPG "; // QR code size private static final int QRCODE_SIZE = 300; // logo width private static final int WIDTH = 60; // logo height private static final int HEIGHT = 60; private static BufferedImage createImage (String content, String imgPath, boolean needCompress) throws Exception {Hashtable
 
  
Hints = new Hashtable
  
   
(); Hints. put (EncodeHintType. ERROR_CORRECTION, ErrorCorrectionLevel. h); hints. put (EncodeHintType. CHARACTER_SET, CHARSET); hints. put (EncodeHintType. MARGIN, 1); BitMatrix bitMatrix = new MultiFormatWriter (). encode (content, BarcodeFormat. QR_CODE, QRCODE_SIZE, QRCODE_SIZE, hints); int width = bitMatrix. getWidth (); int height = bitMatrix. getHeight (); BufferedImage image = new BufferedImage (width, height, Buffere DImage. TYPE_INT_RGB); for (int x = 0; x <width; x ++) {for (int y = 0; y 
    
Hints = new Hashtable
    
     
(); Hints. put (DecodeHintType. CHARACTER_SET, CHARSET); result = new MultiFormatReader (). decode (bitmap, hints); String resultStr = result. getText (); return resultStr;}/*** resolve the QR code ** @ param path * QR code image address * @ return * @ throws Exception */public static String decode (String path) throws Exception {return QRCodeUtil. decode (new File (path);} public static void main (String [] args) throws Exception {String text = "http://www.thinkphp.cn/extend/295.html"; QRCodeUtil. encode (text, "C:/Users/Public/Pictures/Sample Pictures/images.jpg", "C:/Users/Public/Pictures/Sample Pictures/", true );}}
    
   
  
 

2. BufferedImageLuminanceSource class:

package com.zpxing.controller;import java.awt.Graphics2D;import java.awt.geom.AffineTransform;import java.awt.image.BufferedImage;import com.google.zxing.LuminanceSource;public class BufferedImageLuminanceSource extends LuminanceSource {private final BufferedImage image;private final int left;private final int top;public BufferedImageLuminanceSource(BufferedImage image) {this(image, 0, 0, image.getWidth(), image.getHeight());}public BufferedImageLuminanceSource(BufferedImage image, int left,int top, int width, int height) {super(width, height);int sourceWidth = image.getWidth();int sourceHeight = image.getHeight();if (left + width > sourceWidth || top + height > sourceHeight) {throw new IllegalArgumentException("Crop rectangle does not fit within image data.");}for (int y = top; y < top + height; y++) {for (int x = left; x < left + width; x++) {if ((image.getRGB(x, y) & 0xFF000000) == 0) {image.setRGB(x, y, 0xFFFFFFFF); // = white}}}this.image = new BufferedImage(sourceWidth, sourceHeight,BufferedImage.TYPE_BYTE_GRAY);this.image.getGraphics().drawImage(image, 0, 0, null);this.left = left;this.top = top;}public byte[] getRow(int y, byte[] row) {if (y < 0 || y >= getHeight()) {throw new IllegalArgumentException("Requested row is outside the image: " + y);}int width = getWidth();if (row == null || row.length < width) {row = new byte[width];}image.getRaster().getDataElements(left, top + y, width, 1, row);return row;}public byte[] getMatrix() {int width = getWidth();int height = getHeight();int area = width * height;byte[] matrix = new byte[area];image.getRaster().getDataElements(left, top, width, height, matrix);return matrix;}public boolean isCropSupported() {return true;}public LuminanceSource crop(int left, int top, int width, int height) {return new BufferedImageLuminanceSource(image, this.left + left,this.top + top, width, height);}public boolean isRotateSupported() {return true;}public LuminanceSource rotateCounterClockwise() {int sourceWidth = image.getWidth();int sourceHeight = image.getHeight();AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0,0.0, 0.0, sourceWidth);BufferedImage rotatedImage = new BufferedImage(sourceHeight,sourceWidth, BufferedImage.TYPE_BYTE_GRAY);Graphics2D g = rotatedImage.createGraphics();g.drawImage(image, transform, null);g.dispose();int width = getWidth();return new BufferedImageLuminanceSource(rotatedImage, top,sourceWidth - (left + width), getHeight(), width);}}

This is to set the url to jump after scanning: String text = "http://www.thinkphp.cn/extend/295.html"; readers can set themselves, can also contain parameters

The generated QR code is available and will jump

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.