package shop.alien.util.qrcode;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.QRCodeWriter;

import java.io.*;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.CharacterCodingException;
import java.nio.charset.Charset;
import java.nio.charset.CharsetEncoder;
import java.util.Base64;

/**
 * @author ssk
 * @version 1.0
 * @date 2020/4/26 9:23
 */
public class QrCodeImage {

    public static void main() {
        //图片转base64
        String getImageBase64 = imgToBase64("d://tushetou.jpg");
        System.out.println(getImageBase64);
        System.out.println(getImageBase64.length());
        //读取html前后文并添加base64文本
        //String newTxt = readTxt("d://html1.txt") + getImageBase64 + readTxt("d://html2.txt");
        //String newTxt = getImageBase64;
        //String newTxt = readTxt("d://html.txt");
        //String newTxt = "data:image/png;base64,"+getImageBase64;
        //String newTxt = "data:image/png;base64,/9j/4AAQSkZJRgABAQEASABIAAD/2wBDAAwICQoJBwwKCQoNDAwOER0TERAQESMZGxUdKiUsKyklKCguNEI4LjE/MigoOk46P0RHSktKLTdRV1FIVkJJSkf/2wBDAQwNDREPESITEyJHMCgwR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0dHR0f/wAARCAAwADADASIAAhEBAxEB/8QAGgAAAwEAAwAAAAAAAAAAAAAABAUGBwECA//EADMQAAIBAwIEAwYEBwAAAAAAAAIDBAABBQYSERMiMhQxQiEjUmJykjOCsbJBQ1FzgaLB/8QAFAEBAAAAAAAAAAAAAAAAAAAAAP/EABQRAQAAAAAAAAAAAAAAAAAAAAD/2gAMAwEAAhEDEQA/ANUoOVNjQo93THLjrt5kw9tCZjIjjce+aYkfKDoAP4l7aSjAjLkLyOp3IkS79nM/AXfu2rEvp7qAq2rYLB2wI+RyC/jiw2EP3VyGrIgLNmQx2RxwD5skRiEfuoqJqjASXWRFysVjC8gBlMpUtENN3SmgtY+ZnQdYkuNNj3ZBkA8L+sD3UZwvUEiVp2VII9LZFUbIl1WDcYrkf3B9Xd9VUuJkjkIS5GwrHfpavb2EPcP3UAmaFTMjgxZ7RtP3f55TSH9KkdX6Sy2SyLJMRKnmR7gYbOrb07R/Wq3VimDCVkkCRsx7hk7LeoR7v9SvTmG9UqKD0HzFEG4DoM80fpTNQ1PVNShCjdHed7t4lflnu9m3tqp1FjpeRjIGOpDOSwj5T+2/QY/D81eWXfnobY9/Gw7LfMFFuEQiKwl+alUaTm3ZJSCzphZsmTGve0ZXHcvt9NBL4zReoosrmSMegLEBBa92D7u5jt3dNaXigsjLZhNx4bpIvD6SUA/uG9GwrPVGAZbRkMEOtlh28fy0swknxuayL1CPIC649jt6yHcRfuoKG3lUA+VI0pkY2LUASYUoyNQbfeJH27hEPVV/UoYeO1TkSjkISIkZYpZcN3LMubu/5QA56dl5caNwxoouclZJWx/v7kPE+3tHt+KicSm0rLKnR4kqIhN3OMHr2kbmF1DTGBiFRpF5jjbLmEG3xTO7h8Nvhp/50EHLymWzMyRGgQpQREMISvYuX4jad/5npH6eqnGm50iVGYLYqFRE7RS2KRcplv6h8tL8bhWzeeOQlkcK053COvpuz35fil6h+Wq1ACtdlrEAC3ba3lQf/9k=";
        String newTxt = "www.qianhaikeji.cn";
        //文本转二维码
        encode(newTxt, "d://111111.png");
    }

    public static String imgToBase64(String filePath) {
        try {
            InputStream is = new FileInputStream(filePath);
            byte[] b = new byte[is.available()];
            is.read(b);
            is.close();
            return Base64.getEncoder().encodeToString(b);
        } catch (Exception e) {
            e.getMessage();
            return null;
        }
    }

    private static String readTxt(String path) {
        if (path == null || "".equals(path)) {
            return "";
        }
        StringBuilder sb = new StringBuilder();
        File file = new File(path);
        InputStreamReader read = null;
        BufferedReader reader = null;
        try {
            read = new InputStreamReader(new FileInputStream(file), "gb2312");
            reader = new BufferedReader(read);
            String line;
            while ((line = reader.readLine()) != null) {
                sb.append(line);
                sb.append("\n");
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (read != null) {
                try {
                    read.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return sb.toString();
    }

    static void encode(String conent, String filePath) {
        Charset charset = Charset.forName("UTF-8");
        CharsetEncoder encoder = charset.newEncoder();
        byte[] b = null;
        try { // Convert a string to ISO-8859-1 bytes in a ByteBuffer
            System.out.println("-------->" + conent.length());
            ByteBuffer bbuf = encoder.encode(CharBuffer.wrap(conent));
            b = bbuf.array();
        } catch (CharacterCodingException e) {
            System.out.println(e.getMessage());
        }
        String data = "";
        try {
            data = new String(b, "iso8859-1");
        } catch (UnsupportedEncodingException e) {
            System.out.println(e.getMessage());
        } // get a byte matrix for the data
        BitMatrix matrix = null;
        int height = 900;
        int width = 800;
        com.google.zxing.Writer writer = new QRCodeWriter();
        try {
            matrix = writer.encode(data, BarcodeFormat.QR_CODE, width, height);
        } catch (WriterException e) {
            System.out.println(e.getMessage());
        }
        File file = new File(filePath);
        try {
            MatrixToImageWriter.writeToFile(matrix, "PNG", file);
            System.out.println("printing to " + file.getAbsolutePath());
        } catch (IOException e) {
            System.out.println(e.getMessage());
        }
    }
}
