前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >微信小程序 带参二维码 纯Java实现

微信小程序 带参二维码 纯Java实现

作者头像
Designer 小郑
发布2023-08-01 09:31:09
2630
发布2023-08-01 09:31:09
举报
文章被收录于专栏:跟着小郑学JAVA

本文原创首发CSDN,链接 https://blog.csdn.net/qq_41464123/article/details/106335476 ,作者博客https://blog.csdn.net/qq_41464123 ,转载请带上本段内容,尤其是脚本之家、码神岛等平台,谢谢配合。



说明:笔者重新规划了博客方向,想更详细的讲解微信小程序的所有技术内容,本文于2020年5月25日初次写作。 本文首发CSDN,纯笔者原创手打,欢迎社会各界朋友来转载我的文章,希望先来私信我,转载后文章加上原文地址! 同时笔者也欢迎一起合作共赢,愿意写杂志,写书,贡献自己的一份微薄之力! 本篇主要讲解,本文主要介绍:实现微信小程序中自定义二维码 如果您想系统的学习微信小程序,欢迎关注我的CSDN微信小程序专栏,我将不定期更新所学技术,谢谢!

零:前言

开发微信小程序中,有一个常见的需求,就是“一物一码”。

比如说,一个租房管理系统,你需要为每一个房屋,甚至每一个租客,设置一个二维码,方便核验;

比如说,一个餐厅点餐系统,你需要为每一张桌,甚至每一个座位,设置一个二维码,方便用户点餐;

比如说,一个在线社交系统,你需要为每一个用户,设置一个二维码,方便用户之间添加好友;

在实际开发的系统中,能用到二维码的项目比比皆是。

那么,我们如何去生成二维码,实现客户的需求呢?

一:二维码是什么?

常见的二维码为QR Code,QR全称Quick Response,是一个近几年来移动设备上超流行的一种编码方式,它比传统的Bar Code条形码能存更多的信息,也能表示更多的数据类型。 二维条码/二维码是用某种特定的几何图形按一定规律在平面分布的、黑白相间的、记录数据符号信息的图形;在代码编制上巧妙地利用构成计算机内部逻辑基础的“0”、“1”比特流的概念,使用若干个与二进制相对应的几何形体来表示文字数值信息,通过图象输入设备或光电扫描设备自动识读以实现信息自动处理:它具有条码技术的一些共性:每种码制有其特定的字符集;每个字符占有一定的宽度;具有一定的校验功能等。同时还具有对不同行的信息自动识别功能、及处理图形旋转变化点。

二维码可以这样用......

●信息获取(名片、地图、WIFI密码、资料) ●网站跳转(跳转到微博、手机网站、网站) ●广告推送(用户扫码,直接浏览商家推送的视频、音频广告) ●手机电商(用户扫码、手机直接购物下单) ●防伪溯源(用户扫码、即可查看生产地;同时后台可以获取最终消费地) ●优惠促销(用户扫码,下载电子优惠券,抽奖) ●会员管理(用户手机上获取电子会员信息、VIP服务) ●手机支付(扫描商品二维码,通过银行或第三方支付提供的手机端通道完成支付) ●账号登录(扫描二维码进行各个网站或软件的登录)

个人理解:

1.二维码就是一个字符串,使用QQ、微信等解码工具扫描二维码后,会扫出这个字符串;

2.如果这个字符串是一个网址,则会被解码工具的自带浏览器直接打开;

3.如果使用微信扫描,且该网址(域名)关联小程序,则会优先打开关联的小程序;


二:二维码如何生成?

2.1 :我们需要用到  core-3.3.0.jar  这样一个jar包。

2.2 : 我们需要两个工具类

QRCodeUtil

代码语言:javascript
复制
package ypc.zwz.util;

import java.awt.BasicStroke;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Shape;
import java.awt.geom.RoundRectangle2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.OutputStream;
import java.util.Hashtable;
import java.util.Random;
import javax.imageio.ImageIO;
import com.google.zxing.BarcodeFormat;
import com.google.zxing.BinaryBitmap;
import com.google.zxing.DecodeHintType;
import com.google.zxing.EncodeHintType;
import com.google.zxing.MultiFormatReader;
import com.google.zxing.MultiFormatWriter;
import com.google.zxing.Result;
import com.google.zxing.common.BitMatrix;
import com.google.zxing.common.HybridBinarizer;
import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel;
 
public class QRCodeUtil {
	private static final String CHARSET = "utf-8";
	private static final String FORMAT_NAME = "JPG";
	// 二维码尺寸
	private static final int QRCODE_SIZE = 300;
	// LOGO宽度
	private static final int WIDTH = 60;
	// LOGO高度
	private static final int HEIGHT = 60;
 
	
	private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {
		Hashtable hints = new Hashtable();
		hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
		hints.put(EncodeHintType.CHARACTER_SET, CHARSET);
		hints.put(EncodeHintType.MARGIN, 1);
		BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE,
				hints);
		int width = bitMatrix.getWidth();
		int height = bitMatrix.getHeight();
		BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
		for (int x = 0; x < width; x++) {
			for (int y = 0; y < height; y++) {
				image.setRGB(x, y, bitMatrix.get(x, y) ? 0xFF000000 : 0xFFFFFFFF);
			}
		}
		if (imgPath == null || "".equals(imgPath)) {
			return image;
		}
		// 插入图片
		QRCodeUtil.insertImage(image, imgPath, needCompress);
		return image;
	}
 
	private static void insertImage(BufferedImage source, String imgPath, boolean needCompress) throws Exception {
		File file = new File(imgPath);
		if (!file.exists()) {
			System.err.println("" + imgPath + "   该文件不存在!");
			return;
		}
		Image src = ImageIO.read(new File(imgPath));
		int width = src.getWidth(null);
		int height = src.getHeight(null);
		if (needCompress) { // 压缩LOGO
			if (width > WIDTH) {
				width = WIDTH;
			}
			if (height > HEIGHT) {
				height = HEIGHT;
			}
			Image image = src.getScaledInstance(width, height, Image.SCALE_SMOOTH);
			BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
			Graphics g = tag.getGraphics();
			g.drawImage(image, 0, 0, null); // 绘制缩小后的图
			g.dispose();
			src = image;
		}
		// 插入LOGO
		Graphics2D graph = source.createGraphics();
		int x = (QRCODE_SIZE - width) / 2;
		int y = (QRCODE_SIZE - height) / 2;
		graph.drawImage(src, x, y, width, height, null);
		Shape shape = new RoundRectangle2D.Float(x, y, width, width, 6, 6);
		graph.setStroke(new BasicStroke(3f));
		graph.draw(shape);
		graph.dispose();
	}
 
	/*
	 * 生成二维码
	 */
	public static void encode(String content, String imgPath, String destPath, boolean needCompress) throws Exception {
		BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
		mkdirs(destPath);
		System.out.println("++++++++++++++++++++++++++");
		System.out.println(destPath);//user.dir指定了当前的路径 
		ImageIO.write(image, FORMAT_NAME, new File(destPath));
	}
 
	public static BufferedImage encode(String content, String imgPath, boolean needCompress) throws Exception {
		BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
		return image;
	}
 
	public static void mkdirs(String destPath) {
		File file = new File(destPath);
		// 当文件夹不存在时,mkdirs会自动创建多层目录,区别于mkdir.(mkdir如果父目录不存在则会抛出异常)
		if (!file.exists() && !file.isDirectory()) {
			file.mkdirs();
		}
	}
 
	public static void encode(String content, String imgPath, String destPath) throws Exception {
		QRCodeUtil.encode(content, imgPath, destPath, false);
	}
 
	public static void encode(String content, String destPath) throws Exception {
		QRCodeUtil.encode(content, null, destPath, false);
	}
 
	public static void encode(String content, String imgPath, OutputStream output, boolean needCompress)
			throws Exception {
		BufferedImage image = QRCodeUtil.createImage(content, imgPath, needCompress);
		ImageIO.write(image, FORMAT_NAME, output);
	}
 
	public static void encode(String content, OutputStream output) throws Exception {
		QRCodeUtil.encode(content, null, output, false);
	}
 
	/*
	 * 解析二维码
	 */
	public static String decode(File file) throws Exception {
		BufferedImage image;
		image = ImageIO.read(file);
		if (image == null) {
			return null;
		}
		BufferedImageLuminanceSource source = new BufferedImageLuminanceSource(image);
		BinaryBitmap bitmap = new BinaryBitmap(new HybridBinarizer(source));
		Result result;
		Hashtable hints = new Hashtable();
		hints.put(DecodeHintType.CHARACTER_SET, CHARSET);
		result = new MultiFormatReader().decode(bitmap, hints);
		String resultStr = result.getText();
		return resultStr;
	}
 
	public static String decode(String path) throws Exception {
		return QRCodeUtil.decode(new File(path));
	}
}

BufferedImageLuminanceSource.java

代码语言:javascript
复制
package ypc.zwz.util;

import java.awt.Graphics2D;
import java.awt.geom.AffineTransform;
import java.awt.image.BufferedImage;
import com.google.zxing.LuminanceSource;
 
public class BufferedImageLuminanceSource extends LuminanceSource {
 
	private final BufferedImage image;
	private final int left;
	private final int top;
 
	public BufferedImageLuminanceSource(BufferedImage image) {
		this(image, 0, 0, image.getWidth(), image.getHeight());
	}
 
	public BufferedImageLuminanceSource(BufferedImage image, int left, int top, int width, int height) {
		super(width, height);
 
		int sourceWidth = image.getWidth();
		int sourceHeight = image.getHeight();
		if (left + width > sourceWidth || top + height > sourceHeight) {
			throw new IllegalArgumentException("Crop rectangle does not fit within image data.");
		}
 
		for (int y = top; y < top + height; y++) {
			for (int x = left; x < left + width; x++) {
				if ((image.getRGB(x, y) & 0xFF000000) == 0) {
					image.setRGB(x, y, 0xFFFFFFFF); // = white
				}
			}
		}
 
		this.image = new BufferedImage(sourceWidth, sourceHeight, BufferedImage.TYPE_BYTE_GRAY);
		this.image.getGraphics().drawImage(image, 0, 0, null);
		this.left = left;
		this.top = top;
	}
 
	public byte[] getRow(int y, byte[] row) {
		if (y < 0 || y >= getHeight()) {
			throw new IllegalArgumentException("Requested row is outside the image: " + y);
		}
		int width = getWidth();
		if (row == null || row.length < width) {
			row = new byte[width];
		}
		image.getRaster().getDataElements(left, top + y, width, 1, row);
		return row;
	}
 
	public byte[] getMatrix() {
		int width = getWidth();
		int height = getHeight();
		int area = width * height;
		byte[] matrix = new byte[area];
		image.getRaster().getDataElements(left, top, width, height, matrix);
		return matrix;
	}
 
	public boolean isCropSupported() {
		return true;
	}
 
	public LuminanceSource crop(int left, int top, int width, int height) {
		return new BufferedImageLuminanceSource(image, this.left + left, this.top + top, width, height);
	}
 
	public boolean isRotateSupported() {
		return true;
	}
 
	public LuminanceSource rotateCounterClockwise() {
		int sourceWidth = image.getWidth();
		int sourceHeight = image.getHeight();
		AffineTransform transform = new AffineTransform(0.0, -1.0, 1.0, 0.0, 0.0, sourceWidth);
		BufferedImage rotatedImage = new BufferedImage(sourceHeight, sourceWidth, BufferedImage.TYPE_BYTE_GRAY);
		Graphics2D g = rotatedImage.createGraphics();
		g.drawImage(image, transform, null);
		g.dispose();
		int width = getWidth();
		return new BufferedImageLuminanceSource(rotatedImage, top, sourceWidth - (left + width), getHeight(), width);
	}
 
}

2.3 编写测试函数

main.java

代码语言:javascript
复制
package ypc.zwz.main;

import ypc.zwz.util.QRCodeUtil;

public class Main {

	public static void main(String[] args) {
		for(int id = 1; id<=1;id ++) {
			saveHouseTwoDimensionalCode("c:",Long.valueOf(Long.parseLong(""+id)));
		}
	}
	
	public static int saveHouseTwoDimensionalCode(String filePath,Long id){
		String text = "https://ypcqmm.net?id=" + id;
		// 二维码中心的内嵌图片
		String imgPath = filePath+"\\zwz\\logo.png";
		// 生成的二维码的路径及名称
		String destPath = filePath+"\\zwz\\house_"+String.valueOf(id)+".png";
		//生成二维码
		try {
			QRCodeUtil.encode(text, imgPath, destPath, true);
		} catch (Exception e) {
			e.printStackTrace();
		}
		return 1;
	}
}

网址(二维码内容)可以根据实际需求进行更改(String 类型的 text 变量)

内嵌图片的文件名和位置,也可以按需更改(String 类型的 imgPath 变量)

生成二维码的图片文件位置,也可以按需更改(Stringl类型的 destPath 变量)

2.4 运行项目,产生二维码

然后打开指定的目录,我的是C盘zwz文件夹

三: 如何关联到微信小程序?

首先,你需要:

1.请确认你的微信小程序,是不是企业、媒体、政府及其他组织类型小程序;

2.请确认你有没有通过TCP备案的域名?

如果没有,可以参考我的另外一篇文章:《腾讯云服务器备案全流程 40天备案的血与泪》

3.请确认该域名的绑定服务器能否可以用?


如果以上三个条件都具备了,那么可以参考官网文档进行扫普通链接二维码打开小程序的配置了。

四: 总结

总而言之,本文讲解了微信小程序中,如何生成自定义的二维码,使得用户使用微信,扫描该二维码,可以进入指定的微信小程序。

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2023-07-31,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 零:前言
  • 一:二维码是什么?
    • 二维码可以这样用......
      • 个人理解:
        • 1.二维码就是一个字符串,使用QQ、微信等解码工具扫描二维码后,会扫出这个字符串;
          • 2.如果这个字符串是一个网址,则会被解码工具的自带浏览器直接打开;
            • 3.如果使用微信扫描,且该网址(域名)关联小程序,则会优先打开关联的小程序;
            • 二:二维码如何生成?
            • 三: 如何关联到微信小程序?
            相关产品与服务
            云开发 CloudBase
            云开发(Tencent CloudBase,TCB)是腾讯云提供的云原生一体化开发环境和工具平台,为200万+企业和开发者提供高可用、自动弹性扩缩的后端云服务,可用于云端一体化开发多种端应用(小程序、公众号、Web 应用等),避免了应用开发过程中繁琐的服务器搭建及运维,开发者可以专注于业务逻辑的实现,开发门槛更低,效率更高。
            领券
            问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档