간단하게 QR 코드 생 성

23372 단어 기타QR 코드자바
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Hashtable;

import javax.imageio.ImageIO;

import com.alibaba.fastjson.JSONObject;
import com.google.zxing.*;
import com.google.zxing.client.j2se.BufferedImageLuminanceSource;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.QRCodeReader;
import com.google.zxing.qrcode.QRCodeWriter;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
import org.apache.commons.lang3.StringUtils;

/**
 * Created by robin on 2018/6/4.
 */
public class QRCodeUtil {
    private final static String FILE_URL_YOUR_CONFIG = "C:\\Users\\29007\\Desktop\\";//       
    private final static String IMAGE_FORMAT = ".png";//    
    private final static Integer IMAGE_SIZE = 900;//        900      700*700 8k        > 200

    /**
     *                
     * @param fileName    
     * @param qrCode        
     * @throws WriterException
     * @throws IOException
     */
    public static boolean createQrCode(String fileName, QRCode qrCode) throws WriterException, IOException{
        return createQrCode(FILE_URL_YOUR_CONFIG,fileName,qrCode,IMAGE_SIZE,IMAGE_FORMAT);
    }

    /**
     *                
     * @param outputUrl        
     * @param fileName    
     * @param qrCode        
     * @param qrCodeSize        
     * @param imageFormat       
     * @throws WriterException
     * @throws IOException
     */
    public static boolean createQrCode(String outputUrl,String fileName, QRCode qrCode, int qrCodeSize, String imageFormat) throws WriterException, IOException{
        String url = outputUrl + fileName + imageFormat;
        FileOutputStream outputStream = new FileOutputStream(new File(url));
        String content = JSONObject.toJSONString(qrCode);
        return createQrCode(outputStream,content,qrCodeSize,imageFormat);
    }

    /**
     *                
     * @param outputStream        
     * @param content        
     * @param qrCodeSize        
     * @param imageFormat       
     * @throws WriterException
     * @throws IOException
     */
    public static boolean createQrCode(OutputStream outputStream, String content, int qrCodeSize, String imageFormat) throws WriterException, IOException{
    //        
    content = new String(content.getBytes("UTF-8"),"ISO-8859-1");
    //         MAP
        Hashtable, ErrorCorrectionLevel> hintMap = new Hashtable, ErrorCorrectionLevel>();
        hintMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.L);  //     
        QRCodeWriter qrCodeWriter = new QRCodeWriter();
        //      (   ) QR       
        BitMatrix byteMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, qrCodeSize, qrCodeSize, hintMap);
        //  BufferedImage  QRCode  (matrixWidth         )
        int matrixWidth = byteMatrix.getWidth();
        BufferedImage image = new BufferedImage(matrixWidth-200, matrixWidth-200, BufferedImage.TYPE_INT_RGB);
        image.createGraphics();
        Graphics2D graphics = (Graphics2D) image.getGraphics();
        graphics.setColor(Color.WHITE);
        graphics.fillRect(0, 0, matrixWidth, matrixWidth);
        //             
        graphics.setColor(Color.BLACK);
        for (int i = 0; i < matrixWidth; i++){
            for (int j = 0; j < matrixWidth; j++){
                if (byteMatrix.get(i, j)){
                    graphics.fillRect(i-100, j-100, 1, 1);
                }
            }
        }
        return ImageIO.write(image,imageFormat.substring(1,imageFormat.length()), outputStream);
    }

    /**
     *             
     */
    public static QRCode readQrCode(String fileName) throws IOException, FormatException, ChecksumException, NotFoundException {
        String inputUrl = FILE_URL_YOUR_CONFIG + fileName + IMAGE_FORMAT;
        FileInputStream inputStream = new FileInputStream(new File(inputUrl));
        return readQrCode(inputStream);
    }

    /**
     *             
     */
    public static QRCode readQrCode(String inputUrl,String fileName,String imageFormat) throws IOException, FormatException, ChecksumException, NotFoundException {
        String url = inputUrl + fileName + imageFormat;
        FileInputStream inputStream = new FileInputStream(new File(url));
        return readQrCode(inputStream);
    }

    /**
     *             
     */
    public static QRCode readQrCode(InputStream inputStream) throws IOException, FormatException, ChecksumException, NotFoundException {
        //            
        BufferedImage image = ImageIO.read(inputStream);
        //            
        LuminanceSource source = new BufferedImageLuminanceSource(image);
        BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
        QRCodeReader reader = new QRCodeReader();
        QRCode qrCode = new QRCode();
        Result result = reader.decode(bitmap);
        String text = result.getText();
        if(StringUtils.isNotBlank(text)){
            qrCode = JSONObject.parseObject(text, QRCode.class);
        }
        return qrCode;
    }
    /**
     *     
     * @throws WriterException
     */
    public static void main(String[] args) throws IOException, WriterException {
        try {
            QRCode qrCode = new QRCode();
            qrCode.setPhone("17712340000");
            String fileName = "3";
            /*     */
            createQrCode(fileName,qrCode);
            /*     */
            readQrCode(fileName);
        } catch (FormatException e) {
            e.printStackTrace();
        } catch (ChecksumException e) {
            e.printStackTrace();
        } catch (NotFoundException e) {
            e.printStackTrace();
        }
    }


    /**
     *      bean
     */
    public static class QRCode {
        private String phone;

        public String getPhone() {
            return phone;
        }

        public void setPhone(String phone) {
            this.phone = phone;
        }
    }

}
         :https://blog.csdn.net/lixuegen/article/details/80886225

좋은 웹페이지 즐겨찾기