net.ymate.framework.commons.QRCodeHelper.java Source code

Java tutorial

Introduction

Here is the source code for net.ymate.framework.commons.QRCodeHelper.java

Source

/*
 * Copyright 2007-2016 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.ymate.framework.commons;

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;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.lang.StringUtils;

import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.Map;

/**
 * ??
 *
 * @author  (suninformation@163.com) on 15/1/2 ?4:21
 * @version 1.0
 */
public class QRCodeHelper {

    private static final int BLACK = 0xFF000000;
    private static final int WHITE = 0xFFFFFFFF;

    private BitMatrix __matrix;

    // ??
    private String __format = "png";

    private QRCodeHelper(BitMatrix matrix) {
        __matrix = matrix;
    }

    /**
     * @param content      ?
     * @param characterSet ?UTF-8
     * @param width        ?
     * @param height       ?
     * @param margin       ??3
     * @param level        ?
     * @return ?
     * @throws WriterException ?
     */
    public static QRCodeHelper create(String content, String characterSet, int width, int height, int margin,
            ErrorCorrectionLevel level) throws WriterException {
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        //?
        hints.put(EncodeHintType.CHARACTER_SET, StringUtils.defaultIfEmpty(characterSet, "UTF-8"));
        //        // ...
        //        hints.put(EncodeHintType.MAX_SIZE, 298);
        //        hints.put(EncodeHintType.MIN_SIZE, 235);
        hints.put(EncodeHintType.MARGIN, margin <= 0 ? 3 : margin);
        //QR?H
        if (level != null) {
            hints.put(EncodeHintType.ERROR_CORRECTION, level);
        }
        //??
        return new QRCodeHelper(
                new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints));
    }

    /**
     * @param content ?
     * @param width   ?
     * @param height  ?
     * @param level   ?
     * @return ?
     * @throws WriterException ?
     */
    public static QRCodeHelper create(String content, int width, int height, ErrorCorrectionLevel level)
            throws WriterException {
        return create(content, null, width, height, 0, level);
    }

    /**
     * @param content ?
     * @param width   ?
     * @param height  ?
     * @return ?
     * @throws WriterException ?
     */
    public static QRCodeHelper create(String content, int width, int height) throws WriterException {
        return create(content, null, width, height, 0, null);
    }

    /**
     * @param format ?
     * @return ??PNG
     */
    public QRCodeHelper setFormat(String format) {
        this.__format = StringUtils.defaultIfEmpty(format, "png");
        return this;
    }

    public BufferedImage toBufferedImage() {
        int width = __matrix.getWidth();
        int height = __matrix.getHeight();
        BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
        for (int x = 0; x < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, __matrix.get(x, y) ? BLACK : WHITE);
            }
        }
        return image;
    }

    /**
     * ?
     *
     * @param file 
     * @throws IOException ?
     */
    public void writeToFile(File file) throws IOException {
        BufferedImage image = toBufferedImage();
        if (!ImageIO.write(image, __format, file)) {
            throw new IOException("Could not write an image of format " + __format + " to " + file);
        }
    }

    /**
     * ??
     *
     * @param stream ?
     * @throws IOException ?
     */
    public void writeToStream(OutputStream stream) throws IOException {
        BufferedImage image = toBufferedImage();
        if (!ImageIO.write(image, __format, stream)) {
            throw new IOException("Could not write an image of format " + __format);
        }
    }

}