1. <!-- 根据链接生成二维码 -->

1

2

3

4

5

6

7

8

9

10

11

   <dependency>

      <groupId>com.google.zxing</groupId>

      <artifactId>core</artifactId>

      <version>3.2.1</version>

   </dependency>

   <dependency>

      <groupId>com.google.zxing</groupId>

      <artifactId>javase</artifactId>

      <version>3.3.3</version>

   </dependency>

</dependencies>

  


2.util

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

178

179

180

181

182

183

184

185

186

187

188

189

190

191

192

193

194

195

196

197

198

199

200

201

202

203

204

205

206

207

208

209

210

211

212

213

214

215

216

217

218

219

220

221

222

223

224

225

226

227

228

229

230

231

232

233

234

235

236

237

238

239

240

241

242

243

244

245

246

247

248

package com.xinlianpu.util;

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;

import javax.imageio.ImageIO;

import javax.imageio.stream.ImageOutputStream;

import java.awt.*;

import java.awt.image.BufferedImage;

import java.io.*;

import java.net.URL;

import java.util.HashMap;

import java.util.Map;

import java.util.Objects;

/**

 * Created by Mafy on 2019/5/9.

 */

public class ZxingUtils {

    public static BufferedImage enQRCode(String contents, int width, int height) throws WriterException {

        //定义二维码参数

        final Map<EncodeHintType, Object> hints = new HashMap(8) {

            {

                //编码

                put(EncodeHintType.CHARACTER_SET, "UTF-8");

                //容错级别

                put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);

                //边距

                put(EncodeHintType.MARGIN, 0);

            }

        };

        return enQRCode(contents, width, height, hints);

    }

    /**

     * 生成二维码

     *

     * @param contents 二维码内容

     * @param width    图片宽度

     * @param height   图片高度

     * @param hints    二维码相关参数

     * @return BufferedImage对象

     * @throws WriterException 编码时出错

     * @throws IOException     写入文件出错

     */

    public static BufferedImage enQRCode(String contents, int width, int height, Map hints) throws WriterException {

//        String uuid = UUID.randomUUID().toString().replace("-", "");

        //本地完整路径

//        String pathname = path + "/" + uuid + "." + format;

        //生成二维码

        BitMatrix bitMatrix = new MultiFormatWriter().encode(contents, BarcodeFormat.QR_CODE, width, height, hints);

//        Path file = new File(pathname).toPath();

        //将二维码保存到路径下

//        MatrixToImageWriter.writeToPa(bitMatrix, format, file);

//        return pathname;

        return MatrixToImageWriter.toBufferedImage(bitMatrix);

    }

    /**

     * 将图片绘制在背景图上

     *

     * @param backgroundPath 背景图路径

     * @param zxingImage     图片

     * @param x              图片在背景图上绘制的x轴起点

     * @param y              图片在背景图上绘制的y轴起点

     * @return

     */

    public static BufferedImage drawImage(String backgroundPath, BufferedImage zxingImage, int x, int y) throws IOException {

        //读取背景图的图片流

        BufferedImage backgroundImage;

        //Try-with-resources 资源自动关闭,会自动调用close()方法关闭资源,只限于实现Closeable或AutoCloseable接口的类

        URL imgurl = new URL(backgroundPath);

//        try (InputStream imagein = new FileInputStream(backgroundPath)) {

//            backgroundImage = ImageIO.read(imagein);

//        }

        try (InputStream imagein = new BufferedInputStream(imgurl.openStream())) {

            backgroundImage = ImageIO.read(imagein);

        }

        return drawImage(backgroundImage, zxingImage, x, y);

    }

    /**

     * 将图片绘制在背景图上

     *

     * @param backgroundImage 背景图

     * @param zxingImage      图片

     * @param x               图片在背景图上绘制的x轴起点

     * @param y               图片在背景图上绘制的y轴起点

     * @return

     * @throws IOException

     */

    public static BufferedImage drawImage(BufferedImage backgroundImage, BufferedImage zxingImage, int x, int y) throws IOException {

        Objects.requireNonNull(backgroundImage, ">>>>>背景图不可为空");

        Objects.requireNonNull(zxingImage, ">>>>>二维码不可为空");

        //二维码宽度+x不可以超过背景图的宽度,长度同理

        if ((zxingImage.getWidth() + x) > backgroundImage.getWidth() || (zxingImage.getHeight() + y) > backgroundImage.getHeight()) {

            throw new IOException(">>>>>二维码宽度+x不可以超过背景图的宽度,长度同理");

        }

        //合并图片

        Graphics2D g = backgroundImage.createGraphics();

        g.drawImage(zxingImage, x, y,

                zxingImage.getWidth(), zxingImage.getHeight(), null);

        return backgroundImage;

    }

    /**

     * 将文字绘制在背景图上

     *

     * @param backgroundImage 背景图

     * @param x               文字在背景图上绘制的x轴起点

     * @param y               文字在背景图上绘制的y轴起点

     * @return

     * @throws IOException

     */

    public static BufferedImage drawString(BufferedImage backgroundImage, String text, int x, int y,Font font,Color color) {

        //绘制文字

        Graphics2D g = backgroundImage.createGraphics();

        //设置颜色

        g.setColor(color);

        //消除锯齿状

        g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING,RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);

        //设置字体

        g.setFont(font);

        //绘制文字

        g.drawString(text, x, y);

        return backgroundImage;

    }

    public static InputStream bufferedImageToInputStream(BufferedImage backgroundImage) throws IOException {

        return bufferedImageToInputStream(backgroundImage, "png");

    }

    /**

     * backgroundImage 转换为输出流

     *

     * @param backgroundImage

     * @param format

     * @return

     * @throws IOException

     */

    public static InputStream bufferedImageToInputStream(BufferedImage backgroundImage, String format) throws IOException {

        ByteArrayOutputStream bs = new ByteArrayOutputStream();

        try (

                ImageOutputStream

                        imOut = ImageIO.createImageOutputStream(bs)) {

            ImageIO.write(backgroundImage, format, imOut);

            InputStream is = new ByteArrayInputStream(bs.toByteArray());

            return is;

        }

    }

    /**

     * 保存为文件

     *

     * @param is

     * @param fileName

     * @throws IOException

     */

    public static void saveFile(InputStream is, String fileName) throws IOException {

        try (BufferedInputStream in = new BufferedInputStream(is);

             BufferedOutputStream out = new BufferedOutputStream(new FileOutputStream(fileName))) {

            int len;

            byte[] b = new byte[1024];

            while ((len = in.read(b)) != -1) {

                out.write(b, 0, len);

            }

        }

    }

    //读取远程url图片,得到宽高

    public static int[] returnImgWH(URL url) {

        int[] a = new int[2];

        BufferedImage bi = null;

        boolean imgwrong=false;

        try {

            //读取图片

            bi = javax.imageio.ImageIO.read(url);

            try{

                //判断文件图片是否能正常显示,有些图片编码不正确

                int i = bi.getType();

                imgwrong=true;

            }catch(Exception e){

                imgwrong=false;

            }

        catch (IOException ex) {

            ex.printStackTrace();

        }

        if(imgwrong){

            a[0] = bi.getWidth(); //获得 宽度

            a[1] = bi.getHeight(); //获得 高度

        }else{

            a=null;

        }

        return a;

    }

    public static void main(String[] args) {

        //二维码宽度

        int width = 293;

        //二维码高度

        int height = 293;

        //二维码内容

        String contcent = "http://192.168.4.162:8802/portal/courseActivity/detail?id=aed2fabfde70409ea0611d4eaf05dd6e:3b8b77dd-a63b-4555-abc0-2c0033af35be";

        BufferedImage zxingImage = null;

        try {

            //二维码图片流

            zxingImage = ZxingUtils.enQRCode(contcent, width, height);

        catch (WriterException e) {

            e.printStackTrace();

        }

        //背景图片地址

        String backgroundPath = "http://test-dfs.enterfaces.com/M00/00/51/CgYBa1zRe4iAM5e9AAEUUh_Ua8g311.jpg";

        InputStream inputStream = null;

        try {

            URL url = new URL(backgroundPath);

            int[] bb = returnImgWH(url);

            //合成二维码和背景图

            BufferedImage image = ZxingUtils.drawImage(backgroundPath, zxingImage, bb[0]-width, bb[1]-height);

//            //-----------------------------------------绘制文字-----------------------------------------------------

//            Font font = new Font("微软雅黑", Font.BOLD, 35);

//            String text = "";

//            image = ZxingUtils.drawString(image, text, 375, 647,font,new Color(244,254,189));

//            //-----------------------------------------绘制文字-----------------------------------------------------

            //图片转inputStream

            inputStream = ZxingUtils.bufferedImageToInputStream(image);

        catch (IOException e) {

            e.printStackTrace();

        }

        //保存的图片路径

        String originalFileName = "D://99999.png";

        try {

            //保存为本地图片

            ZxingUtils.saveFile(inputStream, originalFileName);

        catch (IOException e) {

            e.printStackTrace();

        }

    }

}

  

com.google.zxing图片叠加,二维码生成,图片加文字相关推荐

  1. java二维码生成-谷歌(Google.zxing)开源二维码生成学习及实例

    java二维码生成-谷歌(Google.zxing)开源二维码生成的实例及介绍  这里我们使用比特矩阵(位矩阵)的QR码编码在缓冲图片上画出二维码 实例有以下一个传入参数 OutputStream o ...

  2. springboot使用imageio返回图片_SpringBoot 二维码生成(复制即用)

    二维码生成 基础环境 SpringBoot.Maven 代码 依赖 <!-- 工具类 import Controller import 效果 Base64 字符串 base64 转换为图片在线工 ...

  3. 加载google Z-Xing库实现二维码解析与生成,并将解析结果在另一页面显示

    1.首先需要下载Z-Xing 库项目下载文档,可在http://download.csdn.net/detail/catchingsun/8903065进行下载: 2.解析二维码,并跳转至新建Acti ...

  4. Google Zxing 只扫描二维码或者只扫描条形码

    Google Zxing怎样实现扫描二维码.条形码,具体参考我 这里主要说一下,怎样选择不同的模式. Zxing 默认支持扫描二维码.条形码两种功能,但是因为项目要求,禁止扫描二维码.那么怎样禁止扫描 ...

  5. vue二维码生成且带文字图片下载

    (一)web页面效果: (二)执行结果: (三)vue代码实例: 1)安装qrcode-vue库:npm install --save qrcode-vue 2)安装html2canvas库:npm ...

  6. Zxing实现二维码生成和解析,可带logo

        在项目中使用zxing生成二维码提供项目支撑(ZXing是一个开源Java类库用于解析多种格式的条形码和二维码),其余SwetakeQRCode.BarCode4j等等工具可去了解. 简单介绍 ...

  7. 条形码/二维码生成探索

    条形码/二维码生成探索 所用依赖 <!--条形码生成依赖(轻量型,推荐使用这个)(生成条码的同时会把信息生成到条形码下)--><dependency><groupId&g ...

  8. 使用zxing生成带logo的二维码图片,自动调节logo图片相对二维码图片的大小

    使用zxing生成带logo的二维码图片,自动调节logo图片相对二维码图片的大小  * 可选是否带logo,可选是否保存二维码图片:结果返回base64编码的图片数据字符串  * 页面显示:< ...

  9. 二维码生成、扫描、图片识别(Zxing)

    这样的例子虽然已经很多了,不过我在网上浏览了一圈,也没找到几个图库二维码图片识别例子,好的算法识别率才高.这里有一个好点的算法,算法不是我写的,只是作为整理记录,给众多安卓开发者一个方便.demo的U ...

最新文章

  1. 深入浅出Rust Future - Part 1
  2. 解耦 多态性 java_java多态
  3. MaxCompute JDBC 2.2 发布说明
  4. 如何正确使用迁移学习
  5. 深入理解C++类的构造函数与析构函数
  6. php使用imagemagick,PHP的ImageMagick使用;
  7. acrobat 下拉列表 逻辑_记一次 无限列表 滚动优化
  8. 使用Maven编译Tomcat源码
  9. Angularjs+Nodejs图片上传
  10. 【嵌入式Linux】嵌入式Linux应用开发基础知识之I2C应用编程和SMBus协议及AP3216C应用编程
  11. Redmi Note 10配备NFC 3.0功能:首次支持封闭式门卡
  12. 【SICP练习】123 练习3.54
  13. 偏差(bias)、方差(variance)和噪音(noise)
  14. 深入理解JS中和||
  15. 凸优化第九章无约束优化 9.2下降方法
  16. [精简]托福核心词汇102
  17. Exchange2010启用反垃圾邮件功能
  18. 分享多引擎样本查毒网站+多款杀软在线查毒网站
  19. 在Java开发环境中,输入某年某月某日,判断这一天是哪一年的第几天。
  20. Kanban in Action 免积分下载

热门文章

  1. Git中创建一个新的分支并推送
  2. 自制计算机之与门或门和非门
  3. Js是怎样运行起来的?
  4. ChinaSkills-网络系统管理(2022改革Linux部分国产操作系统统信UOS安装运行预测[带图形界面])
  5. jQuery —— JavaScript 库
  6. JQuery库的使用
  7. Hystrix服务监控
  8. Ubuntu 18.04下搜狗拼音输入法选词面板乱码问题(通过更换输入法版本完美解决,一劳永逸)
  9. 有序有重复、有序无重复、无序无重复、无序有重复区别详解及Python实现
  10. 139邮箱无法连接服务器,手机号登录邮箱,为什么总是连接不到服务器?