Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java generate two-dimensional code by Zxing     - Forgot Linux root password (Linux)

- Security basics: simple analytical framework for Linux system firewall (Linux)

- C # / iOS / Android Universal Encryption and decryption (Programming)

- Ubuntu 15.04 using the Eclipse 4.4, Java 8 and WTP (Linux)

- Android Fragment really fully resolve (Programming)

- To configure parameter configuration and software installation and uninstallation under Linux (Linux)

- RHEL6.5 install the latest version of Vim and increase support for the Python2.7.5 (Linux)

- Set up MySQL master and slave servers under Ubuntu 14.04 (Server)

- Linux RPM default installation path (Linux)

- Oracle 11g maintenance partitions - Adding Partitions (Database)

- Upgrading to MySQL 5.7.9 MySQL 5.6.23 (Database)

- Httpclient4.4 of principle (Http execution context) (Programming)

- Android Delete project useless resource file (Programming)

- Using the Linux VNC service (Server)

- Ubuntu 14.04 set auto sleep time (Linux)

- Build RPM package uses Docker mirror (Linux)

- Use Vagrant up a local development environment tutorials (Server)

- MySQL use benchmarking tool sysbench (Database)

- MySQL backup tool to back up mydumper (Database)

- Ubuntu15 core CLR (Server)

  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);


- Linux system crash (no such file or directory) How to rescue database (Linux)
- Win8 mount disk partitions under Ubuntu (Linux)
- Ubuntu deploying Solr (4.4) to Tomcat (7.0.53) (Server)
- Shell Programming points to note about the function (Programming)
- Configuring xdebug debugging environment in Ubuntu 14.04 under the PhpStorm (Linux)
- Redhat 5 prohibit IPv6 (Linux)
- Java enum use (Programming)
- Wi-Fi hackers use to attack your seven methods (Linux)
- Linux memory management -free learning experience (Linux)
- Learn about EditText little depth (Programming)
- C # C ++ Java interface type conversion (Programming)
- VSFTPD Security (Linux)
- Getting jQuery - progress bar (Programming)
- Ubuntu 14.04 virtual machine switching desktop environments (Linux)
- 3 tips Linux command (Linux)
- PLSQL Developer synchronization table tools (Database)
- IBM Data Studio to use ---- window displays all rows (Database)
- A deep understanding of Java enum (Programming)
- C ++ stderr / stdout redirected to a file (Programming)
- RHEL 6.4 installed MySQL 5.6.27 (Database)
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.