Bootstrap

java生成二维码合并海报并加上文字

1.生成二维码

	public static byte[] generateQRCodeImages(String text, int width, int height) throws WriterException, IOException {
		String binary = null;
		QRCodeWriter qrCodeWriter = new QRCodeWriter();
		//调整白边大小
		Hashtable<EncodeHintType, Object> hints = new Hashtable<>();
        hints.put(EncodeHintType.CHARACTER_SET, "utf-8");
        hints.put(EncodeHintType.MARGIN, 1);
        BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height,hints);
		//BitMatrix bitMatrix = qrCodeWriter.encode(text, BarcodeFormat.QR_CODE, width, height);
		//bitMatrix = deleteWhite(bitMatrix);
		ByteArrayOutputStream out = new ByteArrayOutputStream();
		MatrixToImageWriter.writeToStream(bitMatrix,"PNG",out);
		byte[] byteArray = out.toByteArray();
		/*ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(out.toByteArray());
		byte[] bytes = new byte[out.size()];
		byteArrayInputStream.read(bytes);
		BASE64Encoder encoder = new BASE64Encoder();
		String base64 = encoder.encode(bytes);*/
		return byteArray;
	}

2.合并海报

public static void merge(String backGroundImageUrl, /*String qrCodeUrl*/byte[] buf, HttpServletResponse response) {
		String title="全平台累计中奖100000人";
		int size = 35;
		// 添加字体的属性设置
		Font font = new Font("黑体", Font.BOLD, size);
		try {
			//加载背景图片(也就是模板图)
			BufferedImage backGroundImage = ImageIO.read(Objects.requireNonNull(getInputStreamByGet(backGroundImageUrl)));
			//加载二维码图片(也就是需要合成到模板图上的图片)
			//BufferedImage imageCode = ImageIO.read(new File(qrCodeUrl));
			BufferedImage imageCode = ImageIO.read(new ByteArrayInputStream(buf));
			//把背景图片当做为模板
			Graphics2D graphics = backGroundImage.createGraphics();
			//在模板上绘制图象(需要绘图的图片,左边距,上边距,图片宽度,图片高度,图像观察者)同一个模板一般是不会变的
			//graphics.drawImage(imageCode, 30, 30, 20, 20, null);
			int width = Math.min(imageCode.getWidth(null), backGroundImage.getWidth() * 5 / 10);
			int height = imageCode.getHeight(null) > backGroundImage.getHeight() * 5 / 10 ? (backGroundImage.getHeight() * 5 / 10) : imageCode.getWidth(null);
			graphics.drawImage(imageCode, (backGroundImage.getWidth() - width) / 2, backGroundImage.getHeight() / 2 + 100, width, height, null);
			//设置字体
			graphics.setFont(font);
			//设置颜色
			graphics.setColor(Color.BLACK);
			//获取字体度量(字体度量是指对于指定字号的某种字体,在度量方面的各种属性)
			FontMetrics fontMetrics = graphics.getFontMetrics(font);
			//获取字体度量的宽度
			int textWidth = fontMetrics.stringWidth(title);
			//左边距=(模板图宽度-文字宽度)/2
			int widthX = (backGroundImage.getWidth() - textWidth) / 2-150;
			//g.drawString(title, 820, 2850);
			//绘制文字(内容,左边距,上边距),同一个模板上边距一般也是不变的
			graphics.drawString(title, widthX, backGroundImage.getHeight() - (size * 2 + 20));
			graphics.drawString(title, widthX, backGroundImage.getHeight() - (size + 20));
			//完成模板修改
			graphics.dispose();
			//获取新文件的地址
			//imageName="E://aa.png;
			//File outPutFile = new File(imageName);
			ServletOutputStream outPutFile = response.getOutputStream();
			//生成新的合成过的用户二维码并写入新图片,指定类型为png
			ImageIO.write(backGroundImage, "png", outPutFile);
			// 刷新输出流
			outPutFile.flush();
			outPutFile.close();
		} catch (Exception e) {
			e.printStackTrace();
		}
		// 返回给页面的图片地址(因为绝对路径无法访问)
		//return imageName;
	}

	public static InputStream getInputStreamByGet(String url) {
		try {
			HttpURLConnection conn = (HttpURLConnection) new URL(url).openConnection();
			conn.setReadTimeout(50000);
			conn.setConnectTimeout(50000);
			conn.setRequestMethod("GET");
			if (conn.getResponseCode() == HttpURLConnection.HTTP_OK) {
				return conn.getInputStream();
			}

		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	public static void main(String[] args) throws IOException, WriterException {
		byte[] bytes = generateQRCodeImages("4d5sa4ds4adsad4sd64d6s4a", 350, 350);
		ImageUtil.merge("http://oss.yunlegeyou.cn/wisdomlife/face/images/1715243426866.jpg",bytes,response);
	}
;