Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java generate two-dimensional code by Zxing     - How do I use Linux development environment (Linux)

- Linux Network Programming --TCP and UDP datagram type Explanation (Programming)

- Linux process stack and process function stack frame (Linux)

- How to clear the DNS query cache under Linux / Unix / Mac (Linux)

- Ubuntu Linux Change the PATH (Linux)

- Testing Oracle 11g RMAN replicate database (Database)

- Eight sorting algorithm implemented in Python (Programming)

- SQL Server memory Misunderstanding (Database)

- RHEL6.5 replace local YUM source (Linux)

- To solve the Mac in question invalid BASH under configuration environment variable (Linux)

- LVM management parameters commonly used commands explained in detail (Linux)

- dmidecode command Detailed (Hardware information) (Linux)

- VirtualBox installation enhancements let the mouse move and share CentOS 6.4 (Linux)

- Daemon under Linux (Linux)

- The most simple drive to write and test procedures under linux (Programming)

- How to create a new file system / partitions under Linux terminal (Linux)

- Swift notes - let you two hours to learn Swift (Programming)

- RedHat6.4 installation tutorial --- Minimal Edition (Linux)

- stat Usage: Get permission to file the corresponding figures (Linux)

- Linux disk and File System Concepts (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);

    }

}
     
         
         
         
  More:      
 
- Forbid screen change the window size when creating a new window under CentOS (Linux)
- JDK comes with tools jinfo (Linux)
- Linux file permissions to modify the command: chmod (Linux)
- How to release the cache memory on Linux (Linux)
- Build a super simple "hardware" firewall router (Linux)
- Recover Ubuntu 14.04 wakes up from standby in case mouse keyboard appears dead (Linux)
- Increase Linux system security --chattr (Linux)
- Ubuntu 12.04 / 14.04 users to install software LyX document processing (Linux)
- Linux script to copy the folder to all folders with the same name (Linux)
- CentOS yum install LAMP (Server)
- build Android environment on Ubuntu 12.04 (Server)
- Install Ubuntu text editor KKEdit 0.2.10 (Linux)
- Oracle 11G using DG Broker create DataGuard (Database)
- Dialogue UNIX:! $ # @ *% (Linux)
- When the master key encounter NULL (Database)
- Ubuntu Tutorial: E: Failed to get lock / var / lib / apt / lists / lock - open (Linux)
- Ubuntu How to mount iso file (Linux)
- Struts2 interceptor simulation (Programming)
- Linux three ways to set environment variables (Linux)
- Github Remote Assistance (Linux)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.