java自定义颜色二维码生成


import com.swetake.util.Qrcode;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

/**
* 2 * @Author:
* 3 * @Date: 2020/2/26 上午 10:17
* 4
*/
public class MatrixToImageWriter {
public static void main(String[] args) {
try {
qrcodeCom();
} catch (IOException e) {
// TODO 自动生成的 catch 块
e.printStackTrace();
}
}
private static void qrcodeCom() throws IOException {
int height=200; //定义图片高度
int width=200;
String qrContent="140429199709061617";
Qrcode qrcode = new Qrcode();
qrcode.setQrcodeEncodeMode('B');//N代表数字,A代表a-Z,B代表其它字符
qrcode.setQrcodeErrorCorrect('Q'); // 设置二维码排错率,可选L(7%)、M(15%)、Q(25%)、H(30%),排错率越高可存储的信息越少,但对二维码清晰度的要求越小
qrcode.setQrcodeVersion(12);//版本,取值范围1-40,值越大尺寸越大,可存储的信息越大
byte[] contentBytes = qrContent.getBytes("utf-8"); //设置编码
BufferedImage bfimg =new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D grap=bfimg.createGraphics();
grap.setBackground(Color.white);//设置背景色
grap.setColor(Color.black);//设置二维码颜色
grap.clearRect(0, 0, width, height);//清除下画板内容
int pixoff = 2;// 设置偏移量,不设置可能导致解析出错
if (contentBytes.length>0&&contentBytes.length<800) {
boolean[][] codeout=qrcode.calQrcode(contentBytes);//让字符串生成二维码
for (int i = 0; i < codeout.length; i++) {
for (int j = 0; j < codeout.length; j++) {
if (codeout[i][j]) {
grap.fillRect(i*3+pixoff, j*3+pixoff, 3, 3);
}
}
}
}
/***带logo开始***//*
Image img = ImageIO.read(new File("D://logo.png"));// 实例化一个Image对象。
grap.drawImage(img, 70, 70, 60, 60, null);// 70,70是距离grap两个边的距离,60,60是中间logo的大小
*//****logo结束******//*
grap.dispose();
bfimg.flush();*/
ImageIO.write(bfimg, "png", new File("D://zhangsan04.png"));
}
}

猜你喜欢

转载自www.cnblogs.com/wsxmiss/p/12367068.html