目录
一.二维码生成
1.在pom.xml中添加需要的依赖
2.生成工具类,QPutils
3.在测试类中调用工具类使用方法并传入参数
4.根据生成图片的路径打开文件夹
二.将二维码传入阿里云并获得返回的url
1.开通OSS
2.添加依赖
3.实现代码
4.结果
5.参考资料
com.google.zxing
core
3.4.1
com.google.zxing
javase
3.4.1
package com.hb.utils;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.util.HashMap;
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 QPUtils {
//二维码黑白对应的三原色数据
private final int white = 0xFF000000;
private final int black = 0xFFFFFFFF;
public byte[] create(String string) throws WriterException, IOException {
final int width = 300;/*若想改变图片的大小,可将width、height设置为create方法的参数并去掉final关键字,然后在调用方法时传入*/
final int height = 300;
// final String format = "png";
HashMap hits = new HashMap();
hits.put(EncodeHintType.CHARACTER_SET,"utf-8");
hits.put(EncodeHintType.ERROR_CORRECTION, ErrorCorrectionLevel.M);
hits.put(EncodeHintType.MARGIN,1);
BarcodeFormat format = BarcodeFormat.QR_CODE;
BitMatrix bitMatrix = new MultiFormatWriter().encode(string, format, width, height, hits);
ByteArrayOutputStream os = new ByteArrayOutputStream();
MatrixToImageWriter.writeToStream(bitMatrix,"png",os);
os.close();
return os.toByteArray();
}
}
package com.hb;
import com.google.zxing.WriterException;
import com.hb.utils.QPUtils;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
@SpringBootTest
public class ZxingTest {
@Test
void test1() throws IOException, WriterException {
String path = "D://picture";//文件夹路径
File file = new File(path);
//如果文件夹不存在则生成文件夹来保存图片
if(!file.exists()){
file.mkdirs();
}
QPUtils qpUtils = new QPUtils();
byte[] create = qpUtils.create("https://www.baidu.com/");
OutputStream fileOutputStream = new FileOutputStream(path+"//"+"baidu.png");/*生成图片的输出流*/
fileOutputStream.write(create);
fileOutputStream.close();
}
}
由于我传入的数据时一个https请求扫码之后就可跳转到百度首页,如果是一个普通的字符串,扫码之后就会显示字符串的内容。
具体方法登录阿里云官网查看,开通之后生成accessKeyId,accessKeySecret
com.aliyun.oss
aliyun-sdk-oss
3.10.2
package com.example.area.exmaple;
import com.aliyun.oss.OSS;
import com.google.zxing.WriterException;
import com.hb.utils.QPutils;
import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URL;
import java.util.Date;
import java.util.Scanner;
import com.aliyun.oss.OSSClientBuilder;
public class OSSExample{
public static void main(String[] args) throws IOException, WriterException {
System.out.println("输入想要生成的字符串:");
String s = new Scanner(System.in).nextLine();
//调用QPutil类返回一个
QPutils erweimaUtil = new QPutils();
//二维码生成到本地
byte[] create = erweimaUtil.create(s);
OutputStream fileOutputStream = new FileOutputStream("F://picture//picture.png");/*生成图片的输出流*/
fileOutputStream.write(create);
fileOutputStream.close();
//二维码上传到阿里云oss
String objectName = "example/"+s.replace(".","")+".png";//二维码在阿里云的存储路径
String endpoint = "oss-cn-**.aliyuncs.com";//**表示创建阿里云时所选得区域
String accessKeyId = "youraccessKeyId";//你的阿里云youraccessKeyId
String accessKeySecret = "youraccessKeySecret";//你的阿里云accessKeySecret
String bucketName = "lijinlong930522";//阿里云中创建的bucket名称
OSS ossClient = new OSSClientBuilder().build( endpoint, accessKeyId, accessKeySecret);//创建OSS实例
ossClient.putObject(bucketName, objectName, new ByteArrayInputStream(create));
// MultipartFile文件存储
// byte[] bytes = file.getBytes();
// InputStream inputStream = new ByteArrayInputStream(bytes);
// ossClient.putObject(bucketName,objectName,inputStream);
//阿里云返回的二维码下载链接,最后一个参数为链接的有效时间,本人所写的参数代表一百天
URL surl = ossClient.generatePresignedUrl(bucketName, objectName, new Date(new Date().getTime() + 100 * 24 * 3600 * 1000));
System.out.println(surl);
ossClient.shutdown();
}
}
下图为返回的二维码下载地址,本地生成的图片根据 newFileOutputStream中的参数去寻找
二维码下载链接:
http://lijinlong930522.oss-cn-chengdu.aliyuncs.com/example/https%3A//wwwbaiducom.png?Expires=1650232062&OSSAccessKeyId=LTAI5tGgui4uSkhzVrNa7VnW&Signature=dx1%2Bha2z2beyLTvor0AdHIvvbAw%3D
https://help.aliyun.com/document_detail/84781.html
https://help.aliyun.com/document_detail/32016.html