JAVA代码实现扫码购带圆图二维码生成

tech2023-07-28  123

JAVA代码生成带图片的圆形二维码

需求背景技术方案程序设计代码实现实现效果过程中遇到的问题写在最后

需求背景

针对常规的新媒体运营渠道,经常要推出一些福利商品,只能通过自媒体的渠道进行购买,因为在当前商城中商品一旦上架,所有的用户都可进行购买,所以需要控制商品购买入口,提供新的商品购买入口。

技术方案

针对以上的需求,开发侧和产品侧讨论之后确定使用扫码购的方式来实现这一需求。

程序设计

商品信息添加购买渠道标识因为商品的购买一般是经过商品详情页进行加车的,所以直接提供特殊通道让用户可以跳转到详情页,而正常购买渠道无法进入此详情页,通过商品详情页路径生成商品二维码为了提升品牌形象,在商品二维码中间添加商品logo或者品牌logo

代码实现

代码入口Controller

@ApiOperation(value = "获取商品二维码", notes = "获取商品二维码", httpMethod = "GET") @RequestMapping(value = "/qrcode/get", method = RequestMethod.GET) @ResponseBody public void getQrCode(Long itemNo, HttpServletResponse response) { try{ // 定义商品详情路径 String detailUrl = "https://www.xxx.com/details?itemNo=123456"; // 获取二维码中心商品图,此处我设置的是固定的公司logo图,建议可以换成商品的缩略图 // 这个logo图是放在项目的resource目录下的 ClassPathResource resource = new ClassPathResource("logo.png"); InputStream inputStream = resource.getInputStream(); File file = new File("./logo.png"); FileUtils.copyInputStreamToFile(inputStream,file); // 创建用来接收二维码的图片 String rootPath = "./"+itemNo+".png"; // 用商品详情信息和图片地址生成二维码,350为二维码尺寸 File qrCodeImge = ZxingUtils.getQRCodeImge(detailUrl, 350, rootPath); // 将二维码图片与logo图片进行叠合 BufferedImage bufferedImage = ZxingUtils.encodeImgLogo(qrCodeImge, file); ImageIO.write(bufferedImage,"png",qrCodeImge); // 通过流将图片写回到前端 InputStream in = new FileInputStream(qrCodeImge); response.setContentType("image/png"); OutputStream outputStream = new BufferedOutputStream(response.getOutputStream()); //创建存放文件内容的数组 byte[] buff =new byte[1024]; //所读取的内容使用n来接收 int n; //当没有读取完时,继续读取,循环 while((n=in.read(buff))!=-1){ //将字节数组的数据全部写入到输出流中 outputStream.write(buff,0,n); } //强制将缓存区的数据进行输出 outputStream.flush(); //关流 outputStream.close(); in.close(); }catch (Exception e){ e.printStackTrace(); } }

生成二维码工具类

import com.google.zxing.BarcodeFormat; import com.google.zxing.EncodeHintType; import com.google.zxing.MultiFormatWriter; import com.google.zxing.common.BitMatrix; import com.google.zxing.qrcode.decoder.ErrorCorrectionLevel; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import javax.imageio.ImageIO; import java.awt.*; import java.awt.geom.Ellipse2D; import java.awt.image.BufferedImage; import java.io.File; import java.io.IOException; import java.util.Hashtable; import java.util.Map; /** * @author huachao * @生二维码工具类 2020年9月3日 */ public class ZxingUtils { private static Log log = LogFactory.getLog(ZxingUtils.class); private static final int BLACK = 0xFF000000; private static final int WHITE = 0xFFFFFFFF; private static BufferedImage toBufferedImage(BitMatrix matrix) { int width = matrix.getWidth(); int height = matrix.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, matrix.get(x, y) ? BLACK : WHITE); } } return image; } private static void writeToFile(BitMatrix matrix, String format, File file) throws IOException { BufferedImage image = toBufferedImage(matrix); if (!ImageIO.write(image, format, file)) { throw new IOException("Could not write an image of format " + format + " to " + file); } } /** * 将内容contents生成长宽均为width的图片,图片路径由imgPath指定 */ public static File getQRCodeImge(String contents, int width, String imgPath) { return getQRCodeImge(contents, width, width, imgPath); } /** * 将内容contents生成长为width,宽为width的图片,图片路径由imgPath指定 */ public static File getQRCodeImge(String contents, int width, int height, String imgPath) { try { Map<EncodeHintType, Object> hints = new Hashtable<EncodeHintType, Object>(); hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M); hints.put(EncodeHintType.CHARACTER_SET, "UTF8"); BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints); File imageFile = new File(imgPath); writeToFile(bitMatrix, "png", imageFile); return imageFile; } catch (Exception e) { log.error("create QR code error!", e); return null; } } /** * 在已有的二维码图片加上logo图片 * * @param twodimensioncodeImg 二维码图片文件 * @param logoImg logo图片文件 * @return */ public static BufferedImage encodeImgLogo(File twodimensioncodeImg, File logoImg) { BufferedImage twodimensioncode = null; try { if (!twodimensioncodeImg.isFile() || !logoImg.isFile()) { System.out.println("输入非图片"); return null; } //读取二维码图片 twodimensioncode = ImageIO.read(twodimensioncodeImg); //获取画笔 Graphics2D g = twodimensioncode.createGraphics(); //读取logo图片 BufferedImage logo = ImageIO.read(logoImg); //透明底的图片 BufferedImage bi2 = new BufferedImage(logo.getWidth(),logo.getHeight(),BufferedImage.TYPE_4BYTE_ABGR); Ellipse2D.Double shape = new Ellipse2D.Double(0,0,logo.getWidth(),logo.getHeight()); Graphics2D g2 = bi2.createGraphics(); g2.setClip(shape); // 使用 setRenderingHint 设置抗锯齿 g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); g2.drawImage(logo,0,0,null); //设置颜色 g2.setBackground(Color.green); g2.dispose(); //设置二维码大小,太大,会覆盖二维码,此处20% int logoWidth = bi2.getWidth(null) > twodimensioncode.getWidth() * 3 / 10 ? (twodimensioncode.getWidth() * 3 / 10) : logo.getWidth(null); int logoHeight = bi2.getHeight(null) > twodimensioncode.getHeight() * 3 / 10 ? (twodimensioncode.getHeight() * 3 / 10) : logo.getHeight(null); // 确定二维码的中心位置坐标,设置logo图片放置的位置 int x = (twodimensioncode.getWidth() - logoWidth) / 2; int y = (twodimensioncode.getHeight() - logoHeight) / 2; g.drawOval(x, y, logoWidth, logoHeight); //开始合并绘制图片 g.drawImage(bi2, x, y, logoWidth, logoHeight, null); // 此处是划圆形,如果需要方形注释掉即可 g.drawRoundRect(x, y, logoWidth, logoHeight, logoWidth, logoHeight); g.dispose(); logo.flush(); twodimensioncode.flush(); } catch (Exception e) { System.out.println("二维码绘制logo失败"); } return twodimensioncode; } }

实现效果

过程中遇到的问题

产品开始提的是放图片就行,上线当天下午需要改成圆形图标,因为现在大家都用的圆形图。个人感觉确实圆形好看多了,因为对awt包不熟悉,改了一个多小时吧。关于中间的logo图,第一个反应是在文件服务器拿这个图,但是png的图下载下来在压缩,糊的没法看,最后找的美工拿的矢量图转pdf然后丢在resource目录下(这就牵出了第三个问题)。springboot项目resource目录下的资源获取,按照以往的获取方式,拿到的都是jar包中的压缩内容,无法获取到文件,试了好多种方式后,最终使用以下方式获取到了。 ClassPathResource resource = new ClassPathResource("logo.png"); InputStream inputStream = resource.getInputStream(); File file = new File("./logo.png"); FileUtils.copyInputStreamToFile(inputStream,file);

写在最后

大多数人会说微信有直接生成的工具类,为啥不直接用?如果有试过的同学就知道,微信工具生成的那个二维码用微信扫可以跳转,使用支付宝或者其他工具是无法跳转的。目前采用的方式是直接把文件流给前端,感觉很不好,可以将生成的图片传到资源服务器,直接给前端返回url即可。
最新回复(0)