修正钉钉手机无法展示 Base64 图片问题

This commit is contained in:
昭荣伊
2019-08-14 10:28:42 +08:00
parent c7af7ec973
commit 45489c6fae
3 changed files with 34 additions and 2 deletions
@@ -39,9 +39,12 @@ public class PackageService {
String tempIconPath = PathManager.getTempIconPath(aPackage);
String iconPath = packagePath + File.separator + "icon.png";
String sourcePath = packagePath + File.separator + fileName;
String jpgIconPath = packagePath + File.separator + "icon.jpg";
// 拷贝图标
ImageUtils.resize(tempIconPath, iconPath, 192, 192);
// 生成钉钉发送所需要图片
ImageUtils.convertPNGToJPG(iconPath, jpgIconPath, 64, 64);
// 源文件
FileUtils.copyFile(new File(filePath), new File(sourcePath));
@@ -58,4 +58,33 @@ public class ImageUtils {
e.printStackTrace();
}
}
/**
* 将 PNG 转为 JPG 并指定图片大小
* @param soureFilePath
* @param targetFilePath
* @param width
* @param height
*/
public static void convertPNGToJPG(String soureFilePath, String targetFilePath, int width, int height) {
try {
//read image file
BufferedImage bufferedImage = ImageIO.read(new File(soureFilePath));
Image tmp = bufferedImage.getScaledInstance(width, height, Image.SCALE_SMOOTH);
// create a blank, RGB, same width and height, and a white background
if (width <= 0 || height <= 0) {
width = bufferedImage.getWidth();
height = bufferedImage.getHeight();
}
BufferedImage newBufferedImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
//TYPE_INT_RGB:创建一个RBG图像,24位深度,成功将32位图转化成24位
newBufferedImage.createGraphics().drawImage(tmp, 0, 0, Color.WHITE, null);
// write to jpeg file
ImageIO.write(newBufferedImage, "jpg", new File(targetFilePath));
} catch (IOException e) {
e.printStackTrace();
}
}
}
@@ -54,8 +54,8 @@ public class DingDingWebHook implements IWebHook {
String appInfo = String.format("[%s(%s)更新](%s)", app.getName(), platform, url);
// 将图片转为 base64, 内网 ip 钉钉无法访问,直接给图片数据
String iconPath = PathManager.getFullPath(app.getCurrentPackage()) + "icon.png";
String icon = "data:image/png;base64," + ImageUtils.convertImageToBase64(iconPath);
String iconPath = PathManager.getFullPath(app.getCurrentPackage()) + "icon.jpg";
String icon = "data:image/jpg;base64," + ImageUtils.convertImageToBase64(iconPath);
String pathInfo = String.format("![%s](%s)", app.getName(), icon);
String otherInfo = String.format("链接:[%s](%s) \n\n 版本:%s (Build: %s)", url, url, app.getCurrentPackage().getVersion(), app.getCurrentPackage().getBuildVersion());
String text = appInfo + " \n\n " + pathInfo + " \n\n " + otherInfo;