引入的架包
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.2.0</version>
</dependency>
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>2.5.0</version>
<scope>compile</scope>
</dependency>
原理:首先通过业务需求生成关于某个id的二维码。
传入 id ->zxing生成本地图片 ->将本地图片上传到oss->获取oss上的地址
/**
* 用于生成本地二维码D://cfpSaas并存入oss,返回oss的url
* @Pamae 任意id,此处因为需要使用物品id生成标识,所以传了物品id
*/
public String generateTwoCode(String goodsId) throws IOException, WriterException {
String path;
// 获取当前操作系统信息
String os = System.getProperty("os.name");
// 如果是windows环境则运行,(找一个路径来放置生成的二维码)
if (os.startsWith("Windows")) {
path = "D://cfpSaas";
} else {
path = "/usr/local/cfpSaas";
}
// 路径不存在则创建路径
File basePath = new File(path);
if (!basePath.exists()) {
basePath.mkdirs();
}
//因为没有传id,此处先随机给他一个用于测试
goodsId = "9e0623bd9cda42beb3293e9b593bc34c";
String filePath = path;
//给二维码图片命名
String fileName = "TwoCode" + goodsId + ".png";
//给二维码放入内容,(扫描该二维码会出现的东西)
String content = "http://www.baidu.com";
//生成本地路径包含上面百度地址的名称为fileName的二维码(此方法在后面)
miQRCreate.generation(filePath, fileName, content);
//获取本地生成的二维码
File localFile = new File(path + "/" + fileName);
if (!localFile.exists()) {