使用用戶影像、昵稱和邀請二維碼,及企業logo、名稱生成用戶的分享裂變海報,
適用場景:拉新裂變活動,生成用戶自己的分享裂變海報,
相關代碼
ImageProcessException
public class ImageProcessException extends RuntimeException {
public ImageProcessException() {
}
public ImageProcessException(String message) {
super(message);
}
public ImageProcessException(String message, Throwable cause) {
super(message, cause);
}
public ImageProcessException(Throwable cause) {
super(cause);
}
public ImageProcessException(String message, Throwable cause, boolean enableSuppression, boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
ImageUtils
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.*;
import java.net.URL;
public class ImageUtils {
/**
* 讀取本地圖片
*
* @param imgPath 圖片讀取路徑,示例:"D:\\test.png"
*/
public static BufferedImage readFromLocal(String imgPath) {
checkValue(imgPath, "imgPath == null!");
try {
return ImageIO.read(new File(imgPath));
} catch (IOException e) {
throw new ImageProcessException("讀取本地圖片出錯", e);
}
}
/**
* 讀取圖片資料流
*/
public static BufferedImage readFromStream(InputStream imgStream) {
checkValue(imgStream, "imgStream == null!");
try {
return ImageIO.read(imgStream);
} catch (IOException e) {
throw new ImageProcessException("讀取圖片資料流出錯", e);
}
}
/**
* 讀取網路圖片
*/
public static BufferedImage readFromUrl(String imgUrl) {
checkValue(imgUrl, "imgUrl == null!");
try {
URL url = new URL(imgUrl);
return ImageIO.read(url);
} catch (IOException e) {
throw new ImageProcessException("讀取網路圖片出錯", e);
}
}
/**
* 保存到本地
*
* @param imgPath 圖片存盤路徑,示例:"D:\\test.png"
*/
public static void writeToLocal(BufferedImage image, String imgPath) {
checkValue(image, "image == null!");
checkValue(imgPath, "imgPath == null!");
int index = imgPath.lastIndexOf(".");
if (index < 0){
throw new ImageProcessException("圖片沒有設定保存格式后綴");
}
String suffix = imgPath.substring(index + 1);
image = imageFormat(image, suffix);
try {
ImageIO.write(image, suffix, new File(imgPath));
} catch (IOException e) {
throw new ImageProcessException("圖片保存到本地出錯", e);
}
}
/**
* 保存為 byte陣列
*
* @param formatName 圖片格式,示例:png、jpg、jpeg 等
*/
public static byte[] writeToBytes(BufferedImage image, String formatName) {
checkValue(image, "image == null!");
checkValue(formatName, "formatName == null!");
image = imageFormat(image, formatName);
try {
ByteArrayOutputStream os = new ByteArrayOutputStream();
ImageIO.write(image, formatName, os);
return os.toByteArray();
} catch (IOException e) {
throw new ImageProcessException("圖片保存為 byte 陣列出錯", e);
}
}
/**
* 保存到資料流
*
* @param formatName 圖片格式,示例:png、jpg、jpeg 等
*/
public static boolean writeToStream(BufferedImage image, String formatName, OutputStream os) {
checkValue(image, "image == null!");
checkValue(formatName, "formatName == null!");
checkValue(os, "os == null!");
image = imageFormat(image, formatName);
try {
return ImageIO.write(image, formatName, os);
} catch (IOException e) {
throw new ImageProcessException("圖片保存到資料流出錯", e);
}
}
/**
* 添加文字
*
* @param source 源圖片
* @param text 添加的文字
* @param color 文字顏色
* @param x 距離左上角的X偏移量
* @param y 距離左上角的Y偏移量
*/
public static void addText(final BufferedImage source, String text, Color color, Font font, int x, int y) {
Graphics2D g = source.createGraphics();
g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_LCD_HRGB);
g.setStroke(new BasicStroke(3));
g.setColor(color);
if (font != null) {
g.setFont(font);
}
if (text != null) {
g.translate(0, 0);
g.drawString(text, x, y);
}
g.dispose();
}
/**
* 添加圖片
*
* @param source 源圖片
* @param addImage 添加的圖片
* @param alpha 透明度,0.0~1.0: 完全透明~完全不透明
* @param x 距離左上角的X偏移量
* @param y 距離左上角的Y偏移量
*/
public static void addImage(final BufferedImage source, BufferedImage addImage, float alpha, int x, int y) {
Graphics2D g2d = source.createGraphics();
int width = addImage.getWidth();
int height = addImage.getHeight();
// 在圖形和影像中實作混合和透明效果
g2d.setComposite(AlphaComposite.getInstance(AlphaComposite.SRC_ATOP, alpha));
g2d.drawImage(addImage, x, y, width, height, null);
g2d.dispose();
}
/**
* 圖片尺寸調整
*
* @param source 源圖片
* @param width 寬
* @param height 高
*/
public static BufferedImage resizeImage(BufferedImage source, int width, int height){
// 獲取源圖片透明度型別
int type = source.getColorModel().getTransparency();
int sourceWidth = source.getWidth();
int sourceHeight = source.getHeight();
BufferedImage image = new BufferedImage(width, height, type);
Graphics2D graphics2d = image.createGraphics();
// 抗鋸齒
graphics2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 筆畫歸一化控制提示值 - 幾何應歸一化,以提高線條的均勻性或間距以及整體美感,
graphics2d.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
// 渲染提示值 - 選擇渲染演算法以優化輸出質量,
graphics2d.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
Image tmp = source.getScaledInstance(sourceWidth, sourceHeight, Image.SCALE_SMOOTH);
graphics2d.drawImage(tmp, 0, 0, width, height, 0, 0, sourceWidth, sourceHeight, null);
graphics2d.dispose();
return image;
}
/**
* 正方形圖片轉圓形
*
* @param source 源圖片
* @param diameter 直徑
*/
public static BufferedImage convertCircular(BufferedImage source, int diameter){
// 透明底的圖片
BufferedImage image = new BufferedImage(diameter, diameter, BufferedImage.TYPE_4BYTE_ABGR);
Ellipse2D.Double shape = new Ellipse2D.Double(0, 0,diameter, diameter);
Graphics2D g2 = image.createGraphics();
g2.setClip(shape);
// 抗鋸齒
g2.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 筆畫歸一化控制提示值 - 幾何應歸一化,以提高線條的均勻性或間距以及整體美感,
g2.setRenderingHint(RenderingHints.KEY_STROKE_CONTROL, RenderingHints.VALUE_STROKE_NORMALIZE);
// 渲染提示值 - 選擇渲染演算法以優化輸出質量,
g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);
// 抖動提示值 - 渲染幾何時不要抖動,
g2.setRenderingHint(RenderingHints.KEY_DITHERING, RenderingHints.VALUE_DITHER_DISABLE);
g2.drawImage(source, 0, 0,diameter, diameter, null);
// 設定顏色
g2.setBackground(Color.green);
g2.dispose();
return image;
}
private static void checkValue(Object value, String errorMsg) {
if (value =https://www.cnblogs.com/crazy-dev/archive/2021/10/17/= null) {
throw new IllegalArgumentException(errorMsg);
}
}
private static BufferedImage imageFormat(BufferedImage image, String formatName) {
if (!formatName.equalsIgnoreCase("jpg") && !formatName.equalsIgnoreCase("jpeg")) {
return image;
}
// 處理 jpg 和 jpeg 格式
BufferedImage formatImage = new BufferedImage(image.getWidth(), image.getHeight(), BufferedImage.TYPE_INT_BGR);
Graphics g = formatImage.getGraphics();
g.drawImage(image, 0, 0, null);
g.dispose();
return formatImage;
}
}
測驗
public class ImageUtilsTest {
public static void main(String[] args) {
ClassLoader loader = ImageUtilsTest.class.getClassLoader();
BufferedImage image = readFromStream(loader.getResourceAsStream("海報模板.png"));
image = resizeImage(image, 828, 1792);
BufferedImage qrCodeImage = readFromStream(loader.getResourceAsStream("qrCode.png"));
qrCodeImage = resizeImage(qrCodeImage, 150, 150);
BufferedImage logoImage = readFromStream(loader.getResourceAsStream("logo.png"));
logoImage = resizeImage(logoImage, 70, 70);
BufferedImage userImage = readFromStream(loader.getResourceAsStream("用戶影像.jpg"));
userImage = convertCircular(userImage, userImage.getHeight());
userImage = resizeImage(userImage, 70, 70);
Font font = new Font("Microsoft YaHei UI Light", Font.BOLD, 26);
addText(image,"測驗", new Color(0,0,0), font,131, 127);
addText(image,"張三股份有限公司", new Color(0,0,0),font, 560, 127);
addImage(image, userImage, 1, 40, 80);
addImage(image, logoImage, 1, 470, 80);
addImage(image, qrCodeImage, 1,40, 1571);
writeToLocal(image, getProjectPath() + File.separator + "Image-" + System.currentTimeMillis() +".jpeg");
}
private static String getProjectPath() {
return System.getProperty("user.dir");
}
}
測驗結果

本文來自博客園,作者:crazy_dev,轉載請注明原文鏈接:https://www.cnblogs.com/crazy-dev/p/15399497.html
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/320923.html
標籤:其他
上一篇:生成裂變海報
