Home PC Games Linux Windows Database Network Programming Server Mobile  
           
  Home \ Programming \ Java generate two-dimensional code by Zxing     - sed and awk in shell usage and some examples (Linux)

- IntelliJ IDEA run in Mac10.9 and JDK7 environment (Linux)

- Using Python to find a particular file extension directory (Programming)

- HBase cluster installation and deployment (Server)

- Linux non-root user uses less than 1024 ports (Linux)

- Ubuntu users how to install the latest Nvidia graphics drivers (Linux)

- Acting on JavaScript events (Programming)

- Shell script on the variables with double quotation marks grep small problem (Programming)

- Linux operating system security settings initial understanding (Linux)

- 2016, the new Node project Precautions (Programming)

- Configure shared library PCRE (Linux)

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

- CentOS install Memcached (Server)

- Use Pylint regulate your Python code (Programming)

- CentOS 6.5 set under Oracle 12c at startup (Database)

- String JavaScript type system (Programming)

- Getting Started with Linux system to learn: how to configure a static IP address for CentOS7 (Linux)

- Linux directory configuration (Linux)

- Android Studio interface-related settings (Linux)

- Intrusion prevention network server security maintenance tips (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:      
 
- MySQL Tutorial: Building MySQL Cluster under Linux (Database)
- How to Install Android Studio on Ubuntu 15.04 / CentOS7 (Linux)
- dd command: do hard disk IO performance test (Linux)
- Detailed software to run UnixBench (Linux)
- How to properly set up a Linux swap partition (Linux)
- Through the source code to install MySQL 5.6.26 under CentOS6 (Database)
- ORA-00600 error solve one case (Database)
- RHEL6 install Python and other packages from source (Linux)
- Java implementation chain store binary tree (Programming)
- Java polymorphism and exception handling (Programming)
- How to configure SNMPv3 on Ubuntu, CentOS and Cisco systems (Linux)
- To install the Oracle 10.2.0.1.0 process notes on Oracle Linux 4u4 (Database)
- Oracle 11gr2 new APPEND_VALUES tips (Database)
- Oracle PLS-00231 error analysis (Database)
- Linux command execution judge sentences -;, &&, || (Linux)
- wget command examples (Linux)
- Android source code compiled fatal error solutions (Programming)
- Ease of use "Explain Shell" script to understand Shell command (Linux)
- Ubuntu 15.04 using the Eclipse 4.4, Java 8 and WTP (Linux)
- JavaScript original values and complex values (Programming)
     
           
     
  CopyRight 2002-2022 newfreesoft.com, All Rights Reserved.