package cn.com.tenlion.buildingpictures.util;

import com.google.zxing.*;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import com.google.zxing.qrcode.encoder.QRCode;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;
import java.util.Hashtable;
  
/**
 * 创建二维码工具类
 * ClassName: BarCodeUtils
 * @Description: TODO
 * 创建工具: MyEclipse2014
 * 运行环境: [Tomcat7以上,MySql5.6以上,JDK7以上]
 * @author 崔宝铖
 * @date 2019年3月1日
 */
public class BarCodeUtils {  

	public static  String CODE = "utf-8";  
	public static  int SUBCODE = 10; // 取uuid的前10位 
	public static  int BLACK = 0xff000000;  
	public static  int WHITE = 0xFFFFFFFF;  
	public static  int WIDTH = 180;  
	public static  int HEIGHT = 100;

    /** 
     * 生成RQ二维码 
     * 
     * @param str    内容 
     * @param height 高度（px） 
     * @author wuhongbo 
     */  
    public static BufferedImage getRQ(String str, Integer width, Integer height, Color color) {
        try {
            /** 定义Map集合封装二维码配置信息 */
            Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
            /** 设置二维码图片的内容编码 */
            hints.put(EncodeHintType.CHARACTER_SET, CODE);
            /** 设置二维码的纠错级别 */
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            /** 设置二维码图片的上、下、左、右间隙 */
            hints.put(EncodeHintType.MARGIN, 0);
            /**
             * 创建二维码字节转换对象
             * 第一个参数：二维码图片中的内容
             * 第二个参数：二维码格式器
             * 第三个参数：生成二维码图片的宽度
             * 第四个参数：生成二维码图片的高度
             * 第五个参数：生成二维码需要配置信息
             *  */
            BitMatrix bitMatrix = new MultiFormatWriter().encode(str,  BarcodeFormat.QR_CODE, width, height, hints);

            BufferedImage image = new BufferedImage(width, height,  BufferedImage.TYPE_INT_RGB);
            // 设置背景为透明颜色
            Graphics2D graphics = image.createGraphics();
            image = graphics.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
            Graphics imageGraphics = image.getGraphics();
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    if(bitMatrix.get(x, y)) {
                        // 自定义颜色
                        imageGraphics.setColor(color);
                        imageGraphics.drawRect ( x, y, 1, 1);
                    }
                }
            }
            return image;
            // return toBufferedImage(bitMatrix);
            // 输出方式
            // 网页  
            // ImageIO.write(image, "png", response.getOutputStream());  
            // 文件
            // ImageIO.write(image, "png", file);  
        } catch (Exception e) {  
            e.printStackTrace();  
        }  
        return null;  
    }

    /** 
     * 生成一维码（128） 
     * 
     * @param str 
     * @param width 
     * @param height 
     * @return 
     * @author wuhongbo 
     */  
    public static BufferedImage getBarcode(String str, Integer width, Integer height, Color color) {
        try {
            // 文字编码  
            Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();
            hints.put(EncodeHintType.CHARACTER_SET, CODE);
            hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
            hints.put(EncodeHintType.MARGIN, 0); // 一维码的大小
            BitMatrix bitMatrix = new MultiFormatWriter().encode(str,  BarcodeFormat.CODE_128, width, height, hints);

            BufferedImage image = new BufferedImage(width, height,  BufferedImage.TYPE_INT_RGB);
            // 设置背景为透明颜色
            Graphics2D graphics = image.createGraphics();
            image = graphics.getDeviceConfiguration().createCompatibleImage(width, height, Transparency.TRANSLUCENT);
            Graphics imageGraphics = image.getGraphics();
            for (int x = 0; x < width; x++) {
                for (int y = 0; y < height; y++) {
                    if(bitMatrix.get(x, y)) {
                        // 自定义颜色
                        imageGraphics.setColor(color);
                        imageGraphics.drawRect ( x, y, 1, 1);
                    }
                }
            }
            return image;
        } catch (Exception e) {
            e.printStackTrace();  
        }  
        return null;  
    }  
  
    /**
     * 转换成图片 
     * 
     * @param matrix 
     * @return 
     * @author wuhongbo 
     */  
    private static BufferedImage toBufferedImage(BitMatrix matrix) {  
        int width = matrix.getWidth();  
        int height = matrix.getHeight();  
        BufferedImage image = new BufferedImage(width, height,  
                BufferedImage.TYPE_INT_ARGB);  
        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;  
    }  
  
}
