Project UI (1) QR code

Source: Internet
Author: User


Preface

Series of articles:[Portal]

I have been idle for weeks. No, no. Blog, I don't know when you will change it. I will not forget you. Well, I will also update it on the OS and csdn. Every day, no matter how useful or useless.

 

Body

The main body of this article is to start with a project, with clear requirements in the project.

Scan the QR code to implement the sign-in function.

Naturally, it is linked to the app. It's okay. Let's take a step.

QR code

A QR code (QR code), also known as a two-dimensional barcode, is a black-and-white image distributed on a plane (two-dimensional direction) based on a specific geometric pattern, it is a key to all information data.

  

Zxing

ZXing is an open-source code library that uses multiple formats of 1D/2D bar code image processing implemented in Java. It contains ports connected to other languages. Zxing can scan and decode the barcode using the built-in camera of the mobile phone. Code encoding and decoding implemented by this project

You can understand

Https://github.com/zxing/zxing/wiki/Getting-Started-Developing

QR code generation
/*** Generate a QR code image ** @ param content * @ param width * @ param height * @ param imgPath stores the image path */
Package sedion. jeffli. wmuui. util. zxing; import java. io. file; import java. util. hashtable; import com. google. zxing. barcodeFormat; import com. google. zxing. encodeHintType; import com. google. zxing. multiFormatWriter; import com. google. zxing. client. j2se. matrixToImageWriter; import com. google. zxing. common. bitMatrix; import com. google. zxing. qrcode. decoder. errorCorrectionLevel;/*** @ author Jeff Lee */public class ZxingEncoderHelper {/*** generates a QR code image * @ param content * @ param width * @ param height * @ param imgPath stores the image path */public void encode (String content, int width, int height, String imgPath) {Hashtable <EncodeHintType, Object> hts = new Hashtable <EncodeHintType, Object> (); hts. put (EncodeHintType. ERROR_CORRECTION, ErrorCorrectionLevel. l); // Error Correction level (hts. put (EncodeHintType. CHARACTER_SET, "UTF-8 ");// Specify encoding format as UTF-8 try {BitMatrix bitMatrix = new MultiFormatWriter (). encode (content, // encoding content, encoding type (specified here as a QR code), BarcodeFormat. QR_CODE, width, height, hts); // specifies the image width and height. Set the parameter MatrixToImageWriter. writeToFile (bitMatrix, "png", new File (imgPath); // generated QR code image} catch (Exception e) {e. printStackTrace () ;}} public static void main (String [] args) {String imgPath = "d:/33.png"; String contents =" Hello! My blog: http://www.cnblogs.com/Alandre/ "; int width = 300, height = 300; ZxingEncoderHelper handler = new ZxingEncoderHelper (); handler. encode (contents, width, height, imgPath );}}

# BitMatrix parameters: encoding content, encoding type, generating image width, generating image height, and setting parameters

# MatrixToImageWriter

 

You will find this image

  

 

QR code decoding

  

/*** Decode * @ param imgPath: QR code image path * @ return */
Package sedion. jeffli. wmuui. util. zxing; import java. awt. image. bufferedImage; import java. io. file; import java. util. hashtable; import javax. imageio. imageIO; import com. google. zxing. binaryBitmap; import com. google. zxing. decodeHintType; import com. google. zxing. luminanceSource; import com. google. zxing. multiFormatReader; import com. google. zxing. result; import com. google. zxing. client. j2se. bufferedImageLuminanceSo Urce; import com. google. zxing. common. hybridBinarizer;/*** @ author Jeff Lee */public class ZxingDecoderHandler {/*** decodes * @ param imgPath QR code image path * @ return */public String decode (String imgPath) {BufferedImage image = null; Result result = null; try {image = ImageIO. read (new File (imgPath); if (image = null) {System. out. println ("the file does not exist! "); // An exception should be thrown} LuminanceSource source = new BufferedImageLuminanceSource (image); BinaryBitmap bitmap = new BinaryBitmap (new HybridBinarizer (source); Hashtable <DecodeHintType, object> hints = new Hashtable <DecodeHintType, Object> (); hints. put (DecodeHintType. CHARACTER_SET, "UTF-8"); result = new MultiFormatReader (). decode (bitmap, hints); return result. getText ();} catch (Exception e) {e. printStackTrace ();} return null;} public static void main (String [] args) {String imgPath = "d:/33.png"; ZxingDecoderHandler handler = new ZxingDecoderHandler (); string content = handler. decode (imgPath); System. out. println ("content:"); System. out. println (content );}}

 

# Opposite to generation

   

Summary

 QR code generation

QR code decoding

 

Thanks and Resource Sharing

    

One step on the road, I hope everyone will join me.

Thank you! I like your support. If yes, click like.

Knowledge Source: https://github.com/zxing/zxing/wiki/Getting-Started-Developing

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.