package com.ducetech.zxing;
import java.awt.BasicStroke;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.file.Path;
import java.util.HashMap;
import javax.imageio.ImageIO;
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 CreateQRCode {
public static void main(String[] args) {
int width = 300;
int height = 300;
String format = "jpeg";
String contents="http://www.baidu.com";
HashMap hashMap = new HashMap();
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(contents, BarcodeFormat.QR_CODE, width, height,hashMap);
Path file = new File("D:/1.png").toPath(); //已存在的一张二维码图片
MatrixToImageWriter.writeToPath(bitMatrix, format, file);
//读取二维码图片
BufferedImage twodimensioncode = ImageIO.read(new File(file.toString()));
//获取画笔
Graphics2D g = twodimensioncode.createGraphics();
//读取logo图片
BufferedImage logo = ImageIO.read(new File("D:/12.jpg")); //加入的log图片
//设置二维码大小,太大,会覆盖二维码,此处20%
int logoWidth = logo.getWidth() > twodimensioncode.getWidth()*2 /10 ? (twodimensioncode.getWidth()*2 /10) : logo.getWidth();
int logoHeight = logo.getHeight() > twodimensioncode.getHeight()*2 /10 ? (twodimensioncode.getHeight()*2 /10) : logo.getHeight();
//设置logo图片放置位置
//中心
int x = (twodimensioncode.getWidth() - logoWidth) / 2;
int y = (twodimensioncode.getHeight() - logoHeight) / 2;
//开始合并绘制图片
g.drawImage(logo, x, y, logoWidth, logoHeight, null);
g.drawRoundRect(x, y, logoWidth, logoHeight, 15 ,15);
//logo边框大小
g.setStroke(new BasicStroke(2));
//logo边框颜色
g.setColor(Color.WHITE);
g.drawRect(x, y, logoWidth, logoHeight);
g.dispose();
logo.flush();
twodimensioncode.flush();
ImageIO.write(twodimensioncode, format, new File("D:/img2.png"));
} catch (WriterException | IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
}
点击查看更多内容
5人点赞
评论
共同学习,写下你的评论
评论加载中...
作者其他优质文章
正在加载中
感谢您的支持,我会继续努力的~
扫码打赏,你说多少就多少
赞赏金额会直接到老师账户
支付方式
打开微信扫一扫,即可进行扫码打赏哦