|
@@ -10,7 +10,6 @@ import com.google.zxing.WriterException;
|
|
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
|
import com.google.zxing.client.j2se.MatrixToImageWriter;
|
|
import com.google.zxing.common.BitMatrix;
|
|
import com.google.zxing.common.BitMatrix;
|
|
import com.google.zxing.qrcode.QRCodeWriter;
|
|
import com.google.zxing.qrcode.QRCodeWriter;
|
|
-import com.yh.saas.plugin.yiliangyiyun.entity.LogoConfig;
|
|
|
|
import lombok.Getter;
|
|
import lombok.Getter;
|
|
import org.apache.http.entity.ContentType;
|
|
import org.apache.http.entity.ContentType;
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
import org.springframework.beans.factory.InitializingBean;
|
|
@@ -20,12 +19,9 @@ import org.springframework.mock.web.MockMultipartFile;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.stereotype.Component;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
import org.springframework.web.multipart.MultipartFile;
|
|
|
|
|
|
-import javax.imageio.ImageIO;
|
|
|
|
-import java.awt.*;
|
|
|
|
-import java.awt.image.BufferedImage;
|
|
|
|
-import java.io.*;
|
|
|
|
-import java.net.URL;
|
|
|
|
-import java.net.URLConnection;
|
|
|
|
|
|
+import java.io.File;
|
|
|
|
+import java.io.FileInputStream;
|
|
|
|
+import java.io.IOException;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.nio.file.FileSystems;
|
|
import java.nio.file.FileSystems;
|
|
import java.nio.file.Path;
|
|
import java.nio.file.Path;
|
|
@@ -78,36 +74,12 @@ public class QRCodeUtil implements InitializingBean {
|
|
Path path = FileSystems.getDefault().getPath(file.getAbsoluteFile().getPath());
|
|
Path path = FileSystems.getDefault().getPath(file.getAbsoluteFile().getPath());
|
|
|
|
|
|
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
|
|
MatrixToImageWriter.writeToPath(bitMatrix, "PNG", path);
|
|
- //存放logo的文件夹
|
|
|
|
- // https://taohaoliang.oss-cn-beijing.aliyuncs.com/logo.png
|
|
|
|
- String path1 = "https://taohaoliang.oss-cn-beijing.aliyuncs.com/";
|
|
|
|
|
|
|
|
-
|
|
|
|
- URL url1 = new URL("https://taohaoliang.oss-cn-beijing.aliyuncs.com/logo.png");
|
|
|
|
- URLConnection uc = url1.openConnection();
|
|
|
|
- InputStream inputStream = uc.getInputStream();
|
|
|
|
-
|
|
|
|
- FileOutputStream out = new FileOutputStream("D:\\logo.png");
|
|
|
|
- int j = 0;
|
|
|
|
- while ((j = inputStream.read()) != -1) {
|
|
|
|
- out.write(j);
|
|
|
|
- }
|
|
|
|
- inputStream.close();
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- //qrcFile用来存放生成的二维码图片(无logo,无文字)
|
|
|
|
-// File qrcFile = new File(path1, name + "123.jpg");
|
|
|
|
- //logoFile用来存放带有logo的二维码图片(二维码+logo,无文字)
|
|
|
|
- File logoFile = new File("D:\\", "logo.png");
|
|
|
|
-
|
|
|
|
- LogoConfig logoConfig = new LogoConfig(); //LogoConfig中设置Logo的属性
|
|
|
|
- File file1 = addLogo_QRCode(file, logoFile, logoConfig,name);
|
|
|
|
- FileInputStream fileInputStream = new FileInputStream(file1.getAbsoluteFile());
|
|
|
|
|
|
+ FileInputStream fileInputStream = new FileInputStream(file.getAbsoluteFile());
|
|
|
|
|
|
MultipartFile multipartFile = new MockMultipartFile( ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
|
|
MultipartFile multipartFile = new MockMultipartFile( ContentType.APPLICATION_OCTET_STREAM.toString(), fileInputStream);
|
|
String result = upload(multipartFile,name);
|
|
String result = upload(multipartFile,name);
|
|
file.delete();
|
|
file.delete();
|
|
- file1.delete();
|
|
|
|
return result;
|
|
return result;
|
|
}
|
|
}
|
|
catch (Exception e){
|
|
catch (Exception e){
|
|
@@ -115,56 +87,6 @@ public class QRCodeUtil implements InitializingBean {
|
|
return "error";
|
|
return "error";
|
|
}
|
|
}
|
|
}
|
|
}
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 给二维码图片添加Logo
|
|
|
|
- *
|
|
|
|
- * @param qrPic
|
|
|
|
- * @param logoPic
|
|
|
|
- */
|
|
|
|
- public File addLogo_QRCode(File qrPic, File logoPic, LogoConfig logoConfig,String name) {
|
|
|
|
- try {
|
|
|
|
- if (!qrPic.isFile() || !logoPic.isFile()) {
|
|
|
|
- System.out.print("file not find !");
|
|
|
|
- System.exit(0);
|
|
|
|
- }
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 读取二维码图片,并构建绘图对象
|
|
|
|
- */
|
|
|
|
- BufferedImage image = ImageIO.read(qrPic);
|
|
|
|
- Graphics2D g = image.createGraphics();
|
|
|
|
-
|
|
|
|
- /**
|
|
|
|
- * 读取Logo图片
|
|
|
|
- */
|
|
|
|
- BufferedImage logo = ImageIO.read(logoPic);
|
|
|
|
-
|
|
|
|
- int widthLogo = image.getWidth() / logoConfig.getLogoPart();
|
|
|
|
- // int heightLogo = image.getHeight()/logoConfig.getLogoPart();
|
|
|
|
- int heightLogo = image.getWidth() / logoConfig.getLogoPart(); //保持二维码是正方形的
|
|
|
|
-
|
|
|
|
- // 计算图片放置位置
|
|
|
|
- int x = (image.getWidth() - widthLogo) / 2;
|
|
|
|
- int y = (image.getHeight() - heightLogo) / 2;
|
|
|
|
-
|
|
|
|
-
|
|
|
|
- //开始绘制图片
|
|
|
|
- g.drawImage(logo, x, y, widthLogo, heightLogo, null);
|
|
|
|
- g.drawRoundRect(x, y, widthLogo, heightLogo, 10, 10);
|
|
|
|
- g.setStroke(new BasicStroke(logoConfig.getBorder()));
|
|
|
|
- g.setColor(logoConfig.getBorderColor());
|
|
|
|
- g.drawRect(x, y, widthLogo, heightLogo);
|
|
|
|
-
|
|
|
|
- g.dispose();
|
|
|
|
-
|
|
|
|
- ImageIO.write(image, "jpeg", new File("D:/" + name + ".jpg"));
|
|
|
|
-
|
|
|
|
- } catch (Exception e) {
|
|
|
|
- e.printStackTrace();
|
|
|
|
- }
|
|
|
|
- return new File("D:/" + name + ".jpg");
|
|
|
|
- }
|
|
|
|
/**
|
|
/**
|
|
* 后台通过服务器间接传文件
|
|
* 后台通过服务器间接传文件
|
|
* @param file
|
|
* @param file
|
|
@@ -179,6 +101,4 @@ public class QRCodeUtil implements InitializingBean {
|
|
ossClient.putObject(putObjectRequest);
|
|
ossClient.putObject(putObjectRequest);
|
|
return baseUrl + "QRCode/" + sendCarNo+".png";
|
|
return baseUrl + "QRCode/" + sendCarNo+".png";
|
|
}
|
|
}
|
|
-
|
|
|
|
-
|
|
|
|
}
|
|
}
|