Use zxing to generate a QR code for Embedded Images

Source: Internet
Author: User

Involves two aspects of knowledge: 1.QR code basics, see Daniel works http://coolshell.cn/articles/10590.html 2. the basic idea of the java graphics library image and Graphics2D is to first use the QR code image generated by zxing, then read the image, insert the icon in it, and then output the entire image. Another way is to create a QR code image and directly overwrite the icon on the QR code. This will cause loss of some data. However, due to the error correction capability of the QR code, the scanner can retrieve the lost information. Without talking nonsense, go directly to the Code:

/*****/Package barcode; import java. awt. color; import java. awt. graphics2D; import java. awt. image; import java. awt. geom. affineTransform; import java. awt. image. affineTransformOp; import java. awt. image. bufferedImage; import java. io. file; import java. io. IOException; import java. util. hashMap; import java. util. map; import javax. imageio. imageIO; import com. google. zxing. barcodeFormat; import com. google. z Xing. encodeHintType; import com. google. zxing. multiFormatWriter; import com. google. zxing. writerException; import com. google. zxing. common. bitMatrix; import com. google. zxing. qrcode. decoder. errorCorrectionLevel;/*** @ author lichunxi **/public class BarcodeFactory {// General private static final int IMAGE_WIDTH = 80; private static final int IMAGE_HEIGHT = 80; private static final int IMAGE_HALF _ WIDTH = IMAGE_WIDTH/2; private static final int FRAME_WIDTH = 2; // two-dimensional code: private static MultiFormatWriter mutiWriter = new MultiFormatWriter (); public static void encode (String content, int width, int height, String srcImagePath, String destImagePath) {try {ImageIO. write (genBarcode (content, width, height, srcImagePath), "jpg", new File (destImagePath);} catch (IOException e) {e. printStackT Race ();} catch (WriterException e) {e. printStackTrace () ;}} private static BufferedImage genBarcode (String content, int width, int height, String srcImagePath) throws WriterException, IOException {// read source image BufferedImage scaleImage = scale (srcImagePath, IMAGE_WIDTH, IMAGE_HEIGHT, true); int [] [] srcPixels = new int [IMAGE_WIDTH] [IMAGE_HEIGHT]; for (int I = 0; I <scaleImage. getWidth (); I ++) {(Int j = 0; j <scaleImage. getHeight (); j ++) {srcPixels [I] [j] = scaleImage. getRGB (I, j) ;}} Map <EncodeHintType, Object> hint = new HashMap <EncodeHintType, Object> (); hint. put (EncodeHintType. CHARACTER_SET, "UTF-8"); hint. put (EncodeHintType. ERROR_CORRECTION, ErrorCorrectionLevel. h); // generate the QR code BitMatrix matrix = mutiWriter. encode (content, BarcodeFormat. QR_CODE, width, height, hint); // convert a two-dimensional matrix to a one-dimensional pixel array Int halfW = matrix. getWidth ()/2; int halfH = matrix. getHeight ()/2; int [] pixels = new int [width * height]; for (int y = 0; y <matrix. getHeight (); y ++) {for (int x = 0; x <matrix. getWidth (); x ++) {// read image if (x> halfW-IMAGE_HALF_WIDTH & x 

 

 

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.