二维码简单Demo
程序员文章站
2022-07-14 18:18:46
...
二维码
生成二维码还有中间有logo的二维码
背景:之前上班有一个需求,要生成一个二维码,扫出指定的内容,然后我向外拓展,想做一个中间有logo的二维码。
效果图片
图片:
代码
Maven依赖
<!-- 条形码 -->
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.3.3</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.3.3</version>
</dependency>
普通的二维码代码
public class App {
public static void main(String[] args) {
String url = createCode("迷人的桓记");
readCode(url);
}
public static String createCode(String text){
int width = 150;
int height = 150;
String format = "png";
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(text, BarcodeFormat.QR_CODE,width,height,hashMap);
String url = "e:/excel/"+text+".png";
Path file = new File("e:/excel/"+text+".png").toPath();
MatrixToImageWriter.writeToPath(bitMatrix,format,file);
return url;
}catch (Exception e){
e.printStackTrace();
return null;
}
}
public static void readCode(String url){
try{
MultiFormatReader formatReader = new MultiFormatReader();
File file = new File(url);
BufferedImage image = ImageIO.read(file);
BinaryBitmap binaryBitmap = new BinaryBitmap(new HybridBinarizer(new BufferedImageLuminanceSource(image)));
HashMap hashMap = new HashMap();
hashMap.put(EncodeHintType.CHARACTER_SET,"utf-8");
Result result = formatReader.decode(binaryBitmap,hashMap);
System.out.println("解析结果:"+result.toString());
System.out.println("二维码格式类型:"+result.getBarcodeFormat());
System.out.println("二维码文本内容:"+result.getText());
}catch (Exception e){
e.printStackTrace();
}
}
}
中间有logo的二维码
public class QRCodeFactory {
/**
* 生成二维码中间的logo
* @param matrixImage 生成的二维码
* @param logUri logo地址
* @return 带有LOGO的二维码图片
* @throws IOException LOGO地址找不到会有io异常
*/
public BufferedImage setMatrixLogo(BufferedImage matrixImage,String logUri) throws IOException {
// 读取二维码图片,构建绘图对象
Graphics2D g2 =matrixImage.createGraphics();
int matrixWidth = matrixImage.getWidth();
int matrixHeigh = matrixImage.getHeight();
/*读取Logo图片*/
BufferedImage logo = ImageIO.read(new File(logUri));
/*开始绘制图片*/
g2.drawImage(logo,matrixWidth/5*2,matrixHeigh/5*2,matrixWidth/5,matrixHeigh/5,null);
/*绘制边框*/
BasicStroke basicStroke = new BasicStroke(5,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
/*设置笔画对象*/
g2.setStroke(basicStroke);
/*设置弧度的圆角矩形*/
RoundRectangle2D.Float round = new RoundRectangle2D.Float(matrixWidth/5*2,matrixHeigh/5*2,matrixWidth/5,matrixHeigh/5,20,20);
g2.setColor(Color.white);
/*绘制圆弧矩形*/
g2.draw(round);
/*设置logo ,加上一道灰色边框*/
BasicStroke stroke = new BasicStroke(1,BasicStroke.CAP_ROUND,BasicStroke.JOIN_ROUND);
/*设置笔画对象*/
g2.setStroke(stroke);
RoundRectangle2D.Float round2 = new RoundRectangle2D.Float(matrixWidth/5*2+2,matrixHeigh/5*2+2,matrixWidth/5-4,matrixHeigh/5-4,20,20);
g2.setColor(new Color(128,128,128));
g2.draw(round2);
g2.dispose();
matrixImage.flush();
return matrixImage;
}
/**
* 创建二维码图片
* @param content 二维码内容
* @param format 二维码格式
* @param logUri 二维码中间图片的logo地址
* @param size 用于设定图片的大小(可变参数,宽高
* @throws IOException
* @throws WriterException
*/
public void createQRImage(String content,String format,String logUri,int ...size) throws IOException, WriterException {
int width = 430; //二维码宽度
int height = 430; //二维码高度
//如果存储大小的不为空,那么对图片的大小进行设定
if (size.length == 2){
width=size[0];
height=size[1];
}else if (size.length==1){
width=height=size[0];
}
Hashtable<EncodeHintType,Object> hints = new Hashtable<EncodeHintType,Object>();
/*指定纠错等级,纠错等级(L 7%、M 15%、Q 25%、H 30%)*/
hints.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.H);
hints.put(EncodeHintType.CHARACTER_SET,"utf-8");
hints.put(EncodeHintType.MARGIN,1);
BitMatrix bitMatrix = new MultiFormatWriter().encode(content, BarcodeFormat.QR_CODE,width,height,hints);
/*设置默认生成地址*/
String filePath = "e:/excel/"+content+".jpg";
/*生成二维码图片文件*/
File outputFile = new File(filePath);
/*指定输出路径*/
System.out.println("输出文件路径为:"+filePath);
MatrixToImageWriter.writeToFile(bitMatrix,format,outputFile,logUri);
}
}
public class MatrixToImageWriter {
private static final int BLACK = 0xFF000000; //设置图案颜色
private static final int WHITE = 0xFFFFFFFF; //设置背景色
private MatrixToImageWriter(){
}
public static BufferedImage toBufferedImage(BitMatrix matrix){
int width = matrix.getWidth();
int height = matrix.getHeight();
BufferedImage image = new BufferedImage(width,height,BufferedImage.TYPE_INT_RGB);
for (int i = 0;i<width;i++){
for (int j = 0;j<height;j++){
image.setRGB(i,j,(matrix.get(i,j) ? BLACK : WHITE));
}
}
return image;
}
public static void writeToFile(BitMatrix matrix, String format, File file,String logUri) throws IOException {
System.out.println("写入文件");
BufferedImage image = toBufferedImage(matrix);
//设置图标
QRCodeFactory logoConfig = new QRCodeFactory();
image = logoConfig.setMatrixLogo(image,logUri);
if (!ImageIO.write(image,format,file)){
System.out.println("生成图片失败!");
}else {
System.out.println("生成图片成功!");
}
}
public static void writeStream(BitMatrix matrix, String format, OutputStream stream,String logUri) throws IOException, WriterException {
BufferedImage image = toBufferedImage(matrix);
/*设置logo图标*/
QRCodeFactory logoConfig = new QRCodeFactory();
image = logoConfig.setMatrixLogo(image,logUri);
if (!ImageIO.write(image,format,stream)){
System.out.println("生成图片失败!");
}
}
}
public class Qrest {
public static void main(String[] args) {
String content = "你好我叫Richard,好高兴认识你";
String logUri = "E:/私人保存/huan/DSC_0135.jpg";
String format = "jpg";
int [] size = new int[]{430,430};
try{
new QRCodeFactory().createQRImage(content,format,logUri,size);
}catch (Exception e){
e.printStackTrace();
}
}
}