首页 新闻 会员 周边 捐助

java批量生成带文字二维码,并输出到pdf中

0
悬赏园豆:100 [已解决问题] 解决于 2018-12-06 16:49

public class ZXingCode {
    /**
     * 颜色
     */
    private static final int QRCOLOR = 0xFF000000;
    /**
     * 背景颜色
     */
    private static final int BGWHITE = 0xFFFFFFFF;
    /**
     * 存放路径
     */
    private static final String CODEPATH = "..\\..\\codeImage\\";


    public static void main(String[] args) {
        try {
            System.out.println(System.currentTimeMillis());
            getQRCode("fe212ac018f4711811", "300-欧阳峰子");
            System.out.println(System.currentTimeMillis());
            System.out.println("生成ok");

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static String getQRCode(String data, String belowText) {
        try {
            ZXingCode zp = new ZXingCode();
            BufferedImage bim = zp.getQR_CODEBufferedImage(data, BarcodeFormat.QR_CODE, 230, 210, zp.getDecodeHintType());
            //字节数组
            return zp.addText_QRCode(bim, belowText);

        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * @param bim       图片
     * @param belowText 二维码下方显示文字
     * @return
     */
    public String addText_QRCode(BufferedImage bim, String belowText) {
        try {
            BufferedImage image = bim;
            if (belowText != null && !belowText.equals("")) {
                BufferedImage outImage = new BufferedImage(230, 225, BufferedImage.TYPE_4BYTE_ABGR);
                Graphics2D outg = outImage.createGraphics();
                outg.drawImage(image, 0, 0, image.getWidth(), image.getHeight(), null);
                outg.setColor(Color.BLACK);
                outg.setFont(new Font("宋体", Font.PLAIN, 18));
                int strWidth = outg.getFontMetrics().stringWidth(belowText);
                outg.drawString(belowText, 100 - strWidth / 2, image.getHeight() + (outImage.getHeight() - image.getHeight()) / 2 + 5);
                outg.dispose();
                outImage.flush();
                image = outImage;
            }

            ByteArrayOutputStream baos = new ByteArrayOutputStream();
            baos.flush();
            ImageIO.write(image, "jpg", baos);

            BufferedImage newBufferedImage = new BufferedImage(
                    image.getWidth(), image.getHeight(),
                    BufferedImage.TYPE_INT_RGB);
            newBufferedImage.createGraphics().drawImage(image, 0, 0,
                    Color.WHITE, null);
            File filePath = new File(CODEPATH);
            ZXingCode.judeDirExists(filePath);
            ImageIO.write(newBufferedImage, "jpg", new File(CODEPATH + "SZ-" + System.currentTimeMillis() + "code.jpg"));
            //图片写到字节数组中
            String imageBase64QRCode = Base64.byteArrayToBase64(baos.toByteArray());
            baos.close();
            image.flush();
            return imageBase64QRCode;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 绘制二维码
     *
     * @param content       扫描内容
     * @param barcodeFormat 格式
     * @param width
     * @param height
     * @param hints         二维码属性设置
     * @return 二维码图片
     */
    public BufferedImage getQR_CODEBufferedImage(String content, BarcodeFormat barcodeFormat, int width, int height, Map<EncodeHintType, ?> hints) {
        MultiFormatWriter multiFormatWriter = null;
        BitMatrix bm = null;
        BufferedImage image = null;
        try {
            multiFormatWriter = new MultiFormatWriter();
            bm = multiFormatWriter.encode(content, barcodeFormat, width, height, hints);
            int w = bm.getWidth();
            int h = bm.getHeight();
            image = new BufferedImage(w, h, BufferedImage.TYPE_INT_RGB);
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    image.setRGB(x, y, bm.get(x, y) ? QRCOLOR : BGWHITE);
                }
            }
        } catch (WriterException e) {
            e.printStackTrace();
        }

        return image;
    }

    /**
     * 设置二维码属性
     *
     * @return
     */
    public Map<EncodeHintType, Object> getDecodeHintType() {
        Map<EncodeHintType, Object> hints = new HashMap<EncodeHintType, Object>();
        hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 0);
        hints.put(EncodeHintType.MAX_SIZE, 350);
        hints.put(EncodeHintType.MIN_SIZE, 100);
        return hints;
    }

    /**
     * 判断文件夹是否存在
     *
     * @param file
     */
    public static void judeDirExists(File file) {
        if (file.exists()) {
            if (file.isDirectory()) {
            } else {
            }
        } else {
            file.mkdir();
        }

    }
}

我已经完成二维码格式样式的生成,不知道如何放入pdf中

suisl的主页 suisl | 初学一级 | 园豆:114
提问于:2018-12-04 10:06
< >
分享
最佳答案
0

使用pdf表格来存放图片 这样循环就不会乱

suisl | 初学一级 |园豆:114 | 2018-12-04 15:06
其他回答(2)
0

百度一下:java pdf
Java生成PDF文件
https://www.cnblogs.com/shuilangyizu/p/5760928.html

收获园豆:50
快乐的欧阳天美1114 | 园豆:4010 (老鸟四级) | 2018-12-04 10:17

俺没用过,

支持(0) 反对(0) 快乐的欧阳天美1114 | 园豆:4010 (老鸟四级) | 2018-12-04 10:17

谢谢你的帮助,我在研究研究

支持(0) 反对(0) suisl | 园豆:114 (初学一级) | 2018-12-04 10:40
0

可以用Spire.PDF for Java库(http://www.e-iceblue.cn/Downloads/Spire-PDF-JAVA.html),将二维码图片插入到PDF中:
import com.spire.pdf.PdfDocument;
import com.spire.pdf.PdfPageBase;

import com.spire.pdf.graphics.*;

public class InsertImage
{
public static void main(String[] args)
{
PdfDocument pdf = new PdfDocument();
PdfPageBase page = pdf.getPages().add();

    PdfImage image = PdfImage.fromFile("code.jpg");

    float x = 0;
    float y = 50;
    float width = 100;
    float hight = 100;

    page.getCanvas().drawImage(image, x, y, width, hight);

    pdf.saveToFile("Output.pdf");
}

}

收获园豆:50
ms_doudou | 园豆:1170 (小虾三级) | 2018-12-05 15:07
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册