Home PC Games Linux Windows Database Network Programming Server Mobile  
  Home \ Programming \ Java generate two-dimensional code by Zxing     - Echo Command Examples (Linux)

- Several Ceph performance optimization of new methods and ideas (2015 SH Ceph Day after flu reference) (Server)

- Clojure programming languages: take full advantage of the Clojure plug-in Eclipse (Programming)

- Java developers question (Programming)

- Supervisor Installation and Configuration (Server)

- How to download GOG games in Linux command line (Linux)

- Spring classic face questions Share (Programming)

- Boost notes --Thread - problems encountered in the initial use on Ubuntu (Programming)

- Linux System Getting Started Tutorial: Five image processing software (Linux)

- Linux kernel socket protocol stack routing lookup cache mechanism (Linux)

- Ubuntu 14.04 Solution login interface infinite loop (Linux)

- Debian 7.7 Installation and Configuration (Linux)

- How to install and use the malware detection tool LMD and ClamAV antivirus engine on Linux (Linux)

- Oracle table of nested loop connection (Database)

- Linux operating system boot manager -GRUB (Linux)

- Learn to read the source code of vmstat (Linux)

- CentOS 5.5 install ntop (Linux)

- J2EE Example of Filter (Programming)

- Elaborate .NET Multithreading: Thread Pool (Programming)

- ActiveMQ5.10.2 version configuration JMX (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);


- An example of troubleshooting of embedded Linux OpenWRT (Linux)
- Linux System Getting Started Tutorial: permission to permanently modify the USB device in Linux (Linux)
- Kali Linux resolve GPG error KEYEXPIRED 1425567400 (Linux)
- systemd run levels and service management command Introduction (Linux)
- To build PHP environment (Nginx + MariaDB + PHP7) under CentOS 6.5 (Server)
- Linux module mechanism of (Programming)
- MySQL combination Git achieve differential backups can be used in the production environment (Database)
- Node.js Getting the basics: Helloworld! (Linux)
- Using LLVM Clang and Blocks under Linux (Programming)
- Mac OS X 10.9 build Nginx + MySQL + php-fpm environment (Server)
- About Linux operating system security (Linux)
- Running the open-source Swift under Linux platform (Linux)
- JavaScript cross-browser event object library (Programming)
- Under CentOS using yum command to install the Task Scheduler crontab (Linux)
- Kitematic how seamless and DockerFILE (Server)
- How to allow users to access only a specific database (MSSQL) (Database)
- Windows Ubuntu dual system a key Ghost, grub rescue prompt solution (Linux)
- Ubuntu update bug fixes Daquan (Linux)
- To install the Ubuntu Touch emulator on Ubuntu (Linux)
- Linux (SUSE) mount NTFS mobile hard practice (Linux)
  CopyRight 2002-2020 newfreesoft.com, All Rights Reserved.