Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java generate two-dimensional code by Zxing     - Ease of use "Explain Shell" script to understand Shell command (Linux)

- Python programming style (Programming)

- Red Hat Enterprise Linux configuration VNC multi-user access methods (Linux)

- CentOS 7 virt-manager can not connect a local hypervisor (Linux)

- MySQL Online DDL tools of pt-online-schema-change (Database)

- How do I upgrade to Ubuntu 15.04 (Beta) (Linux)

- Analysis of C ++ exception mechanism (Programming)

- How to use the character in C ++ without pressing the Enter key to enter the Show (Programming)

- JavaScript: understanding regular expressions (Programming)

- Firewall chapter of Linux server security configuration (Linux)

- Apache Mina framework Practice (Programming)

- Linux 6 use UDEV binding shared storage (Linux)

- AngularJS notes --- Data Binding (Programming)

- Hadoop 2.7.1 Installation and Configuration under RedHat Linux 6.5 (Server)

- Linux6.1 / 6.5 dual-NIC Binding (Linux)

- MySQL database under Linux to achieve automatic backup scheduled backup day (Database)

- Debian 8.1 (amd64) deployed Memcached (Server)

- Use dump restore mode fast backup and recovery system FreeBSD (Linux)

- Basic Operation Linux files and directories (Linux)

- Nine tips to protect the security of Linux desktop (Linux)

  Java generate two-dimensional code by Zxing
  Add Date : 2018-11-21      
  Java generate two-dimensional code by Zxing

1. Basic class provides two-dimensional code generation tools

package com.green.util;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import javax.imageio.ImageIO;
import com.google.zxing.common.BitMatrix;
public final class MatrixToImageWriter {

    private static final int BLACK = 0xFF000000;

    private static final int WHITE = 0xFFFFFFFF;

    private MatrixToImageWriter () {


    public static BufferedImage toBufferedImage (BitMatrix matrix) {

        int width = matrix.getWidth ();

        int height = matrix.getHeight ();

        BufferedImage image = new BufferedImage (width, height, BufferedImage.TYPE_INT_RGB);

        for (int x = 0; x
            for (int y = 0; y
                image.setRGB (? x, y, matrix.get (x, y) BLACK: WHITE);



        return image;


    public static void writeToFile (BitMatrix matrix, String format, File file) throws IOException {

        BufferedImage image = toBufferedImage (matrix);

        if (! ImageIO.write (image, format, file)) {

            throw new IOException ( "Could not write an image of format" + format + "to" + file);




    public static byte [] writeToStream (BitMatrix matrix, String format) throws IOException {

        ByteArrayOutputStream stream = new ByteArrayOutputStream ();

        BufferedImage image = toBufferedImage (matrix);


        if (! ImageIO.write (image, format, stream)) {

            throw new IOException ( "Could not write an image of format" + format);


        return stream.toByteArray ();



2. Call the utility method to obtain a binary image

package com.green.util;


import java.io.File;

import java.io.IOException;

import java.util.Hashtable;


import com.google.zxing.BarcodeFormat;

import com.google.zxing.EncodeHintType;

import com.google.zxing.MultiFormatWriter;

import com.google.zxing.WriterException;

import com.google.zxing.common.BitMatrix;


/ **


 * /

public class QrCodeGenerator {

    public static byte [] build (String content) throws IOException, WriterException {

            int width = 300;

            int height = 300;

            String format = "gif";

            Hashtable hints = new Hashtable ();

            hints.put (EncodeHintType.CHARACTER_SET, "utf-8");

            BitMatrix bitMatrix = new MultiFormatWriter (). Encode (content,

                    BarcodeFormat.QR_CODE, width, height, hints);

          return MatrixToImageWriter.writeToStream (bitMatrix, format);


- To install Xen in Ubuntu 12.04 (Linux)
- VMware virtual machine to install CentOS 7 (Linux)
- 8 Docker knowledge you may not know (Server)
- Ubuntu deploying Solr (4.4) to Tomcat (7.0.53) (Server)
- Android Studio quick overview of Gradle (Programming)
- lolcat: an output terminal rainbow effects in the Linux command-line tool (Linux)
- How to Install Focuswriter 1.4.5 (Linux)
- JavaScript Advanced Programming notes event capture and event bubbling (Programming)
- Simple Linux file system - df, du, ln (Linux)
- CentOS installation of the ftp (Linux)
- The Oracle VM VirtualBox use U disk under ubuntu (Linux)
- The difference Docker save and export commands (Linux)
- Easily create RPM packages using the FPM (Linux)
- ARP spoofing prevent Linux environment LAN (Linux)
- MySQL5.6.17 compiler installation under CentOS (Database)
- Log4cplus logging facility configuration, installation, testing (Linux)
- Linux remote landing system (Linux)
- About ORA-02391 solution (Database)
- Fedora 8 configured Heartbeat (Linux)
- Linux file system structure Introduction (Linux)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.