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);
	}
  • 3
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您可以使用以下代码来生成二维码海报: ```java import java.awt.*; import java.awt.image.BufferedImage; import java.io.File; import javax.imageio.ImageIO; import com.google.zxing.BarcodeFormat; import com.google.zxing.common.BitMatrix;import com.google.zxing.qrcode.QRCodeWriter; public class QRCodeGenerator { public static void main(String[] args) { String qrCodeText = "https://www.example.com"; // 二维码包含的文本信息 String filePath = "qr_code.png"; // 生成的二维码图片保存的文件路径 int size = 300; // 二维码图片的大小 try { // 创建QRCodeWriter对象 QRCodeWriter qrCodeWriter = new QRCodeWriter(); // 创建BitMatrix对象,表示二维码的矩阵 BitMatrix bitMatrix = qrCodeWriter.encode(qrCodeText, BarcodeFormat.QR_CODE, size, size); // 创建BufferedImage对象,表示二维码图片 BufferedImage bufferedImage = new BufferedImage(size, size, BufferedImage.TYPE_INT_RGB); bufferedImage.createGraphics(); // 设置二维码图片的背景颜色和前景颜色 Graphics2D graphics = (Graphics2D) bufferedImage.getGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, size, size); graphics.setColor(Color.BLACK); // 绘制二维码图片 for (int i = 0; i < size; i++) { for (int j = 0; j < size; j++) { if (bitMatrix.get(i, j)) { graphics.fillRect(i, j, 1, 1); } } } // 保存二维码图片到文件 ImageIO.write(bufferedImage, "png", new File(filePath)); System.out.println("二维码海报已生成并保存到文件:" + filePath); } catch (Exception e) { System.out.println("生成二维码海报时出现错误:" + e.getMessage()); } } } ``` 这段代码使用了 Google 的 ZXing 库来生成二维码。您需要在代码中设置二维码包含的文本信息、生成的二维码图片保存的文件路径以及二维码图片的大小。然后,该代码将会生成一个黑白的二维码海报,并保存到指定的文件路径中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值