前言

场景: 一、推广海报贴上二维码,用户扫码跳转
            二、二维码中间贴logo
 
eg:这里使用展示第一种场景


一、使用工具

Google开源项目ZXing(二维条码编解码)。

二、使用步骤

1.引入依赖

代码如下(示例):

<dependency><groupId>com.google.zxing</groupId><artifactId>javase</artifactId><version>3.3.0</version>
</dependency>

2.使用工具类

代码如下(示例):

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 = 400;// LOGO贴图位置高度private static final int HEIGHT = 400;// LOGO开始位置宽度private static final int LEFT = 50;// LOGO开始位置高度private static final int UP = 50;private static BufferedImage createImage(String content, String imgPath, boolean needCompress) throws Exception {Map<EncodeHintType, Object> hints = new ConcurrentHashMap();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) { // 压缩LOGOif (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;}// 插入LOGOGraphics2D 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);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 byte[] getQRCodeImage(String content) throws WriterException, IOException {QRCodeWriter qrCodeWriter = new QRCodeWriter();BitMatrix bitMatrix = qrCodeWriter.encode(content, BarcodeFormat.QR_CODE, QRCODE_SIZE, QRCODE_SIZE);ByteArrayOutputStream pngOutputStream = new ByteArrayOutputStream();MatrixToImageWriter.writeToStream(bitMatrix, FORMAT_NAME, pngOutputStream);byte[] pngData = pngOutputStream.toByteArray();return pngData;}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));}//二维码中间插入图片public static ByteArrayOutputStream encodeIO(String content, String imgPath, Boolean needCompress) throws Exception {BufferedImage image = QRCodeUtil.createImage(content, imgPath,needCompress);//创建储存图片二进制流的输出流ByteArrayOutputStream baos = new ByteArrayOutputStream();//将二进制数据写入ByteArrayOutputStreamImageIO.write(image, "jpg", baos);return baos;}private static BufferedImage createFromImage(String content, BufferedImage outer, boolean needCompress) throws Exception {Map<EncodeHintType, Object> hints = new ConcurrentHashMap();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);}}// 插入图片QRCodeUtil.insertFromImage(outer, image, needCompress);return outer;}private static void insertFromImage(BufferedImage src,BufferedImage qrCode,  boolean needCompress) throws Exception {Image image = null;int width = qrCode.getWidth(null);int height = qrCode.getHeight(null);if (needCompress) { // 压缩LOGOif (width > WIDTH) {width = WIDTH;}if (height > HEIGHT) {height = HEIGHT;}image = qrCode.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();}// 插入LOGOGraphics2D graph = src.createGraphics();graph.drawImage(image, LEFT, UP, null);graph.setColor(Color.WHITE);graph.dispose();}//图片中插入二维码public static ByteArrayOutputStream encodeIOWith(String content, BufferedImage outer, Boolean needCompress) throws Exception {//插入二维码BufferedImage image = QRCodeUtil.createFromImage(content, outer,needCompress);//添加文字水印addText(image, content);//创建储存图片二进制流的输出流ByteArrayOutputStream baos = new ByteArrayOutputStream();//将二进制数据写入ByteArrayOutputStreamImageIO.write(image, "jpg", baos);return baos;}public static void addText(BufferedImage image,String text){Graphics2D graph = image.createGraphics();
//        graph.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);// 5、设置水印文字颜色graph.setColor(Color.black);graph.setFont(new Font("宋体", Font.BOLD, 15));// 文字增加清晰度graph.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);// 6、设置文字水印内容graph.drawString("邀请码:", 240, 450);graph.drawString(text, 220, 470);graph.dispose();}
}

3.调用

 try {//二维码内容String content = "https://www.csdn.net/";String ossPath="poster1.jpg";//sso获取图片位数据byte[] download = ossSupport.download(ossPath);BufferedImage outerImg = ImageIO.read(new ByteArrayInputStream(download));//是否根据贴图位置空间压缩二维码Boolean needCompress = true;//图片海报outerImg中插入二维码和文字水印ByteArrayOutputStream out = QRCodeUtil.encodeIOWith(content, outerImg, needCompress);//返回二维码response.setCharacterEncoding("UTF-8");response.setContentType("image/jpeg;charset=UTF-8");response.setContentLength(out.size());ServletOutputStream outputStream = response.getOutputStream();outputStream.write(out.toByteArray());outputStream.flush();outputStream.close();}catch (Exception e){e.printStackTrace();}

结果

java生成二维码 推广海报添加二维码 文字水印 二维码添加LOGO相关推荐

  1. Java实现在线word文档添加铺满效果文字水印,文字可换行

    Java实现在线word文档添加铺满效果文字水印,文字可换行 使用插件Aspose.words for java 生成水印的工具类 调用生成水印方法 需求:为在线预览的word文档添加自定义水印,还要 ...

  2. JAVA生成微信小程序分享海报图片

    由于小程序里面分享产品,不能直接到朋友圈,因此很多系统的做法是生成一张带有二维码,或者小程序码的海报图片.用户保存到本地,然后分享到朋友圈. 生成海报,有两种方式: 前端小程通过 canvs 绘图到模 ...

  3. android 水印视频教程,如何给视频添加一个摇摆的文字水印?安卓手机视频编辑助手app给视频加文字水印...

    注意此教程方案是『安卓手机端教程方案』 如果在手机端操作不方便或对眼睛不好 也可以用另外电脑端的教程方案操作:视频加旋转水印[找更多方案] 今天要介绍安卓手机上视频编辑助手是可以给视频添加摇摆的文字水 ...

  4. 微信小程序——二维码推广海报

  5. Java生成随机不重复推广码邀请码

    欢迎进入我的博客:blog.scarlettbai.com查看更多文章 最近接到一个需求,要批量生成推广码,首先我们知道推广码的特效有如下两点: 1:不可重复 2:不可以被推测出 关于这两点,我们的思 ...

  6. java生成不重复的推广码(邀请码、注册码)

    在上网参考了很多文章,发现都是太严谨了,这次就来一个不严谨的,不过可以保证不重复. 1.为什么生成邀请码 因为公司或自身需要. 2.邀请码生成了作用是干什么 更好的控制和统计用户. 从上面可以得出两个 ...

  7. Java生成微信小程序分销海报图

    第一步:授权获取小程序二维码 https://blog.csdn.net/weixin_37545129/article/details/88699623 第二步:准备好二维码背景图存放到本地文件夹中 ...

  8. php实现微信公众号生成淘宝客推广海报(正则匹配淘宝联盟)

    现在很多人做淘宝客,包括我.做淘宝客一个月也可以赚个一两千零用钱,但是"淘宝联盟"APP生成的带二维码宣传图在微信是被屏蔽的,无法打开的! 例如: 所以很多做淘客的,开始开发自己的 ...

  9. php实现微信公众号生成淘宝客推广海报(正则匹配淘宝联盟) 1

    现在很多人做淘宝客,包括我.做淘宝客一个月也可以赚个一两千零用钱,但是"淘宝联盟"APP生成的带二维码宣传图在微信是被屏蔽的,无法打开的! 例如: 所以很多做淘客的,开始开发自己的 ...

最新文章

  1. 图像处理(五)双指数磨皮
  2. PHP之Smarty简单实现
  3. 搭建Jenkins+Sonarqub+Mysql+Android(上篇)
  4. ES6箭头函数(Arrow Functions)
  5. JAVA程序员面试必知32个知识点
  6. 餐厅数据分析报告_如何使用数据科学选择理想的餐厅设计场所
  7. 在C#中实现托盘是多么简单
  8. 杂记 C中的volatile
  9. 【笔试/面试】—— linux 拾遗(一)
  10. 4*4矩阵键盘原理分析以及代码展示
  11. mac使用开源方案实现读取ntfs
  12. java算法一个岛屿上一个说真话 一个说假话_面试常问智力题40道(逻辑题)+ 参考答案...
  13. 7-38 数列求和-给定某数字A(1≤A≤9)以及非负整数N(0≤N≤100000),求数列之和S=A+AA+AAA+⋯+AA⋯A(N个A)。例如A=1, N=3时,S=1+11+111=123。
  14. 日本流行语------收录
  15. 中式英语如何产生?该怎么办?
  16. Cortex-A8处理器编程(上)
  17. STM32三种BOOT模式
  18. 利用SPSS做数据分析②之数据处理2
  19. 4. PCIe 接口时序
  20. 二维码生成、扫描、图片识别(Zxing)

热门文章

  1. springboot available: expected at least 1 bean which qualifies as autowire candidate
  2. 在重大灾难面前,你有抵御风险的能力吗 ? 关于借钱这件事
  3. Flutter高仿微信-第48篇-群聊-图片
  4. 关于java后端重定向
  5. html5试卷分数提交制作,在线试卷制作很麻烦,问卷星试题导入示例送给需要的人...
  6. 【异常检测数据集】MVTEC AD
  7. : set ff=unix 然后保存
  8. 弘辽科技:拼多多免费流量怎么去做?如何获取?
  9. 计算机键盘高手,键盘中的万能Win键,学会秒变电脑高手!快来看看吧
  10. Ubuntu Desktop Server - 添加用户 / 删除用户和添加 sudo 权限