java 生成带参数的二维码并保存到指定目录


import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import com.google.zxing.BarcodeFormat;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.WriterException;
import com.google.zxing.client.j2se.MatrixToImageWriter;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;

public class QrCodeUtil {

	private final static int WIDTH = 300;// 二维码的宽度
	private final static int HEIGHT = 300;// 二维码的高度
	private final static String FORMAT = "jpg";// 二维码的格式

	public static String encodeQrCode(String textParam) {
		/*
		 * 定义二维码的参数
		 */
		HashMap<EncodeHintType, Object> hashMap = new HashMap<EncodeHintType, Object>();
		// 设置二维码字符编码
		hashMap.put(EncodeHintType.CHARACTER_SET, "UTF-8");
		// 设置二维码纠错等级
		hashMap.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
		// 设置二维码边距
		hashMap.put(EncodeHintType.MARGIN, 2);

		try {
			// 开始生成二维码
			BitMatrix bitMatrix = new MultiFormatWriter().encode(textParam, BarcodeFormat.QR_CODE, WIDTH, HEIGHT, hashMap);
			// 导出到指定目录
			String path = "E:\IMG\testImg.jpg";
			String fileName = UUID.randomUUID().toString().replace("-", "") + ".jpg";
			File file = new File(path, fileName);
			// 如果文件目录不存在,创建目录
			if (!file.getParentFile().exists()) {
				file.getParentFile().mkdirs();
			}
			MatrixToImageWriter.writeToPath(bitMatrix, FORMAT, file.toPath());
			
			// 获取 图片(文件)的存放地址
			String paramPathString = file.getPath();
			// 将反斜杠 \ 替换成 正斜杠 /
			paramPathString = paramPathString.replaceAll("\\\\", "/");
			return paramPathString;
		} catch (WriterException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
您好!关于您的问题,可以使用第三方库zxing,结合微信开放平台提供的接口生成小程序码并传递参数。具体实现步骤如下: 1. 引入zxing库,这里以Maven为例: ```xml <dependency> <groupId>com.google.zxing</groupId> <artifactId>core</artifactId> <version>3.4.0</version> </dependency> <dependency> <groupId>com.google.zxing</groupId> <artifactId>javase</artifactId> <version>3.4.0</version> </dependency> ``` 2. 调用微信接口获取小程序码: ```java public static BufferedImage getMiniProgramCode(String accessToken, String path, int width) { String url = "https://api.weixin.qq.com/wxa/getwxacode?access_token=" + accessToken; HttpPost httpPost = new HttpPost(url); httpPost.setEntity(new StringEntity("{\"path\":\"" + path + "\",\"width\":" + width + "}", ContentType.APPLICATION_JSON)); CloseableHttpResponse response = null; try { response = HttpClients.createDefault().execute(httpPost); if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) { InputStream inputStream = response.getEntity().getContent(); return ImageIO.read(inputStream); } } catch (IOException e) { e.printStackTrace(); } finally { if (response != null) { try { response.close(); } catch (IOException e) { e.printStackTrace(); } } } return null; } ``` 3. 将参数添加到小程序码中: ```java public static BufferedImage addParameterToMiniProgramCode(BufferedImage miniProgramCode, String parameter) { BufferedImage newImage = new BufferedImage(miniProgramCode.getWidth(), miniProgramCode.getHeight() + 20, BufferedImage.TYPE_INT_RGB); Graphics2D graphics = newImage.createGraphics(); graphics.setColor(Color.WHITE); graphics.fillRect(0, 0, newImage.getWidth(), newImage.getHeight()); graphics.drawImage(miniProgramCode, 0, 0, null); graphics.setColor(Color.BLACK); graphics.setFont(new Font("宋体", Font.PLAIN, 16)); graphics.drawString("参数:" + parameter, 0, miniProgramCode.getHeight() + 16); graphics.dispose(); return newImage; } ``` 4. 最后可以将处理后的小程序码保存到本地或直接返回给前端。 以上就是生成小程序码并添加参数的实现过程,希望可以帮到您。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值