1、pom文件添加依赖
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>core</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>com.google.zxing</groupId>
<artifactId>javase</artifactId>
<version>3.2.1</version>
</dependency>
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
2、实体类
public class ImgVo {
private String picUrl; //图片
private String qrcodeUrl; //在线详情链接 例如:https://www.baidu.com/
private String qrCodeImgUrl; //读取本地二维码
public String getPicUrl() {
return picUrl;
}
public void setPicUrl(String picUrl) {
this.picUrl = picUrl;
}
public String getQrcodeUrl() {
return qrcodeUrl;
}
public void setQrcodeUrl(String qrcodeUrl) {
this.qrcodeUrl = qrcodeUrl;
}
public String getQrCodeImgUrl() {
return qrCodeImgUrl;
}
public void setQrCodeImgUrl(String qrCodeImgUrl) {
this.qrCodeImgUrl = qrCodeImgUrl;
}
}
3、海报工具类
public class PosterUtils {
public static String generatePoster(ImgVo vo){
Date dt = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
String qrCode = QrCodeUtil.createQrCode(vo.getQrcodeUrl(), sdf.format(dt) +".jpg");
String url = null;
try {
//InputStream bgImg =new ImgUtils().getImageStream(vo.getPicUrl());//网络图片
InputStream bgImg = new FileInputStream(vo.getPicUrl());//本地图片
vo.setQrCodeImgUrl(qrCode);
url = new ImgUtils().drawImage(bgImg,vo);
}catch (Exception e){e.printStackTrace();}
return url;
}
}
4、图片合成工具类(图片和二维码合成一张图片)
public class ImgUtils {
/**
* 合成图片
* @return url
* @throws IOException
*/
public static String drawImage(InputStream bgImgUrl, ImgVo vo) throws IOException {
BufferedImage posterBufImage = ImageIO.read(bgImgUrl);
Graphics2D posterBufImageGraphics = posterBufImage.createGraphics();
InputStream qrCodeImgUrl = new FileInputStream(vo.getQrCodeImgUrl());
BufferedImage qrCodeImage = ImageIO.read(qrCodeImgUrl);
posterBufImageGraphics.drawImage(qrCodeImage, (posterBufImage.getWidth() - qrCodeImage.getWidth()), posterBufImage.getHeight()-qrCodeImage.getHeight(), qrCodeImage.getWidth(), qrCodeImage.getHeight(), null);
posterBufImageGraphics.dispose();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imgOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(posterBufImage, "png", imgOut);
String localUrl = byte2image(bs.toByteArray());
// 上传到服务器上
String url = QiniuUtil.upload(localUrl);
return url;
}
public static String byte2image(byte[] data){
Date dt = new Date();
SimpleDateFormat sdf = new SimpleDateFormat("yyyyMMddhhmmss");
String fileName = sdf.format(dt) +".jpg";
String path = FileSystemView.getFileSystemView().getHomeDirectory() + File.separator + "testQrcode\\"+fileName;
if(data.length<3||path.equals("")) return "";//判断输入的byte是否为空
try{
FileImageOutputStream imageOutput = new FileImageOutputStream(new File(path));//打开输入流
imageOutput.write(data, 0, data.length);//将byte写入硬盘
imageOutput.close();
return path;
} catch(Exception ex) {
ex.printStackTrace();
}
return path;
}
/**
* 获取网络图片流
*/
public InputStream getImageStream(String url) {
try {
HttpURLConnection connection = (HttpURLConnection) new URL(url).openConnection();
connection.setReadTimeout(5000);
connection.setConnectTimeout(5000);
connection.setRequestMethod("GET");
if (connection.getResponseCode() == HttpURLConnection.HTTP_OK) {
InputStream inputStream = connection.getInputStream();
return inputStream;
}
} catch (IOException e) {
System.out.println("获取网络图片出现异常,图片路径为:" + url);
e.printStackTrace();
}
return null;
}
}
5、二维码工具类
public class QrCodeUtil {
/**
* 生成二维码
* @param url 参数路径
* @param fileName 文件名称
* @return
*/
public static String createQrCode(String url,String fileName) {
//生成的地址
String path = FileSystemView.getFileSystemView().getHomeDirectory() + File.separator + "testQrcode";
try {
Map<EncodeHintType, String> hints = new HashMap<>();
hints.put(EncodeHintType.CHARACTER_SET, "UTF-8");
BitMatrix bitMatrix = new MultiFormatWriter().encode(url, BarcodeFormat.QR_CODE, 220, 220, hints);
File file = new File(path, fileName);
if (file.exists() || ((file.getParentFile().exists() || file.getParentFile().mkdirs()) && file.createNewFile())) {
writeToFile(bitMatrix, "png", file);
return file.getPath();
}
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
static void writeToFile(BitMatrix matrix, String format, File file) throws IOException {
BufferedImage image = toBufferedImage(matrix);
if (!ImageIO.write(image, format, file)) {
throw new IOException("Could not write an image of format " + format + " to " + file);
}
}
private static final int BLACK = 0xFF000000;
private static final int WHITE = 0xFFFFFFFF;
/**
* 生成内容
* @param matrix
* @return
*/
private 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 x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
image.setRGB(x, y, matrix.get(x, y) ? BLACK : WHITE);
}
}
return image;
}
}
6、七牛云上传工具类
public class QiniuUtil {
// 设置需要操作的账号的AK和SK
private static final String ACCESS_KEY = "";
private static final String SECRET_KEY = "";
// 空间名
private static final String bucket = "";
public static String upload(String localFilePath) {
Configuration configuration = new Configuration(Zone.zone2());
UploadManager manager = new UploadManager(configuration);
Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);
String upToken = auth.uploadToken(bucket);
String[] arr = localFilePath.split("\\\\");
String fileName = arr[arr.length-1].split("\\.")[0];
String key = "turing"+fileName;
try {
manager.put(localFilePath, key, upToken);
String posterPicture = key;
return posterPicture;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
}
参考:
七牛云全国各地服务器上传地址:http://www.xyok.net/share/735.html
图片+二维码合成图片:https://my.oschina.net/u/2426590/blog/3027726
七牛云上传图片云存储:https://blog.youkuaiyun.com/tangzekk/article/details/88094130