您的位置 首页 java

java生成海报

第一步 生成 二维码

/**

* 生成二维码矩阵信息

*

* @param content 二维码图片内容

* @param width 二维码图片宽度

* @param height 二维码图片高度

*/

public static BitMatrix setBitMatrix(String content, int width, int height) {

Hashtable<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>();

hints.put(EncodeHintType.CHARACTER_SET, “UTF-8”); // 指定编码方式,防止中文乱码

hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H); // 指定纠错等级

hints.put(EncodeHintType. MARGIN , margin); // 指定二维码四周白色区域大小

BitMatrix bitMatrix = null;

try {

bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, width, height, hints);

} catch (WriterException e) {

e.printStackTrace();

}

return bitMatrix;

}

第二步 保存二维码

/**

* 生成海报二维码

*

* @param content

* @param width

* @param height

* @return

*/

public static String setQRCodeHB(String content, int width, int height) {

String format = “jpg” ;

BitMatrix bitMatrix = setBitMatrix(content, width, height);

// 可通过输出流输出到页面,也可直接保存到文件

OutputStream outStream = null;

String fileName = “qrcode/” + new Date().getTime() + “.jpg” ;

String path = “/profile/test/” + fileName;

try {

outStream = new file OutputStream(new File(path));

writeToFile(bitMatrix, format, outStream, null);

outStream. close ();

} catch (Exception e) {

e.printStackTrace();

}

return path;

}

第三步 合成海报

/**

* 生成邀请好友海报

*

* @param code

* @return

* @throws IOException

*/

public static String drawImage(String code) throws IOException {

//海报背景

String backgroundImageUrl = “huanyinshiyong” ;

// 二维码

String qrCodeImageUrl = setQRCodeHB(“www.baidu.com”, 115, 115);

String nameText = code;

int width = 750;

int height = 1060;

//创建空画布

BufferedImage bgBufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// RGB形式

Graphics2D bgBufImageGraphics = bgBufImage.createGraphics();

//设置背景图片

BufferedImage posterImage = ImageIO.read(new URL(backgroundImageUrl));

bgBufImageGraphics.drawImage(posterImage, 0, 0, 750, 1060, null);

//设置二维码

BufferedImage qrCodeImage = ImageIO.read(new File(qrCodeImageUrl));

bgBufImageGraphics.drawImage(qrCodeImage, 560, 902, 115, 115, null);

Font font = new Font(“宋体”, Font.PLAIN, 24);

bgBufImageGraphics.setFont(font);

//内容

bgBufImageGraphics.drawString(nameText, 400, 945);

bgBufImageGraphics.dispose();

String file = “HB/” + new Date().getTime();

ImageIO.write(bgBufImage, “png”, new File(“/profile/test/” + file + “.jpg”));

System.out.println(“生成图片完成”);

return “/profile/test/” + file + “.jpg” ;

}

文章来源:智云一二三科技

文章标题:java生成海报

文章地址:https://www.zhihuclub.com/187285.shtml

关于作者: 智云科技

热门文章

网站地图