Java二維碼生成工具類

public class CreateCode {
    /*二維碼
    * */
    public static BitMatrix updateBit(BitMatrix matrix, int margin) {
        int tempM = margin * 2;
        int[] rec = matrix.getEnclosingRectangle();//獲取二維碼圖案的屬性
        int resWidth = rec[2] + tempM;
        int resHeight = rec[3] + tempM;
        BitMatrix resMatrix = new BitMatrix(resWidth, resHeight); // 按照自定義邊框生成新的BitMatrix
        resMatrix.clear();
        for (int i = margin; i < resWidth - margin; i++) {//循環,將二維碼圖案繪製到新的bitMatrix中
            for (int j = margin; j < resHeight - margin; j++) {
                if (matrix.get(i - margin + rec[0], j - margin + rec[1])) {
                    resMatrix.set(i, j);
                }
            }
        }
        return resMatrix;
    }

    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 < width; x++) {
            for (int y = 0; y < height; y++) {
                image.setRGB(x, y, matrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
            }
        }
        return image;
    }

    public static byte[] imageToBytes(BufferedImage bImage)  {
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        try {
            ImageIO.write(bImage, "png", out);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return out.toByteArray();
    }

    public static OutputStream imageToStream(BufferedImage bImage) {
        OutputStream outputStream = new ByteArrayOutputStream();
        try {
            ImageIO.write(bImage, "png", outputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return outputStream;
    }
}
    public void test(HttpServletResponse response, HttpServletRequest request) throws IOException, ClassNotFoundException, WriterException {
//        BufferedImage bufferedImage = MatrixToImageWriterWithLogo.genBarcode("10010", 512, 512, "F:\\images2\\1 - 副本 - 副本.jpg"); // 二維碼的內容,寬,高,二維碼中心的圖片地址
//        ImageIO.write(bufferedImage, "jpg", response.getOutputStream());
        int width = 50; // 二維碼圖片寬度
        int height = 50; // 二維碼圖片高度
        int margin = 5;  //自定義白邊邊框寬度
        Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        // 指定糾錯等級,糾錯級別(L 7%、M 15%、Q 25%、H 30%)
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");   // 內容所使用字符集編碼
        hints.put(EncodeHintType.MARGIN, 0);
        BitMatrix bitMatrix = new MultiFormatWriter().encode("10010", BarcodeFormat.QR_CODE, width, height, hints);

        BitMatrix bitMatrix1=CreateCode.updateBit(bitMatrix,margin);
        BufferedImage bufferedImage=CreateCode.toBufferedImage(bitMatrix1);
        response.reset();
        response.setContentType("application/octet-stream");
        response.setHeader("Content-Disposition", "attachment;filename=" + new String("測試.png".getBytes("utf-8"), "ISO8859-1"));

        OutputStream outputStream =  response.getOutputStream();
        ImageIO.write(bufferedImage, "png", outputStream);




    }

 

發佈了44 篇原創文章 · 獲贊 12 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章