版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_26230421/article/details/84957814
前言
本文是通过QRCode生成的二维码
兄弟篇(使用zxing生成):java代码生成二维码(二)
JAR包
只给代码,不给jar包就是耍流氓n(*≧▽≦*)n
产生二维码需要用到jar包:
链接:点我下载
密码:srw7
package erweima;
import java.awt.Color;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
import com.swetake.util.Qrcode;
public class WeiXinCode {
public static void main(String[] args) {
String s="https://blog.csdn.net/qq_26230421";
getQrcodeImage(s, "d:/output.png");
}
public static void getQrcodeImage(String content, String imgPath) {
int width = 235;
int height = 235;
//实例化一个对象
Qrcode qrcode = new Qrcode();
//编码方式
qrcode.setQrcodeEncodeMode('B');
//设置拍错率
qrcode.setQrcodeErrorCorrect('M');
//二维码的版本
qrcode.setQrcodeVersion(15);
//绘制二维码
//画板
BufferedImage image = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
//画笔
Graphics2D gs = image.createGraphics();
//设置背景颜色
gs.setBackground(Color.red);
//设置二维码的颜色
gs.setColor(Color.red);
//创建一个二维码的绘制区域
gs.clearRect(0, 0, width, height);
byte[] codeOut;
try {
codeOut = content.getBytes("utf-8");
boolean[][] code = qrcode.calQrcode(codeOut);
for (int i = 0; i < code.length; i++) {
for (int j = 0; j < code.length; j++) {
if (code[j][i]) {
gs.fillRect(j * 3 + 2, i * 3 + 2, 3, 3);
}
}
}
//加载logo D:/111.png 这个你需要改成你自己的
File file = new File("D:/input.png");
Image srcImage = ImageIO.read(file);
int imgWidth = srcImage.getWidth(null);
int imgHeith = srcImage.getHeight(null);
gs.drawImage(srcImage, 50, 90, imgWidth, imgHeith, null);
//释放资源
gs.dispose();
image.flush();
//写入指定路径
ImageIO.write(image, "png", new File(imgPath));
System.out.println("二维码生成成功了!");
} catch (Exception e) {
//TODO Auto-generated catch block
e.printStackTrace();
} finally {
}
}
}