疫情期间,在家办公,最近做一个需求,用户长按保存图片时增加对应专属二维码、头像、昵称等信息
之前也没用java处理过图片,到处查查改改搞了一天,才整出来这个工具类
import org.apache.commons.io.output.ByteArrayOutputStream;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.net.URL;
/**
* @Author: Waria
* @Date: 2020/2/20
* @Description: 图片处理工具类
*/
public class ImgUtil {
/**
* 打印可使用的fontName系统字体
*/
public static void sysFont() {
GraphicsEnvironment e = GraphicsEnvironment.getLocalGraphicsEnvironment();
String[] fontName = e.getAvailableFontFamilyNames();
for (int i = 0; i < fontName.length; i++) {
System.out.println(fontName[i]);
}
}
/**
* 向背景图片打印文字水印
*
* @param pressText 文字
* @param bgImage 背景图片
* @param x 横坐标
* @param y 纵坐标
* @param fontName 字体名
* @param fontSize 字体大小
* @param fontColor 字体颜色
* @param style 字体样式:例加粗+斜体:Font.BOLD + Font.ITALIC
*/
public static byte[] pressText(String pressText, BufferedImage bgImage, int x, int y, String fontName,
int fontSize, Color fontColor, int style) {
try {
int wideth = bgImage.getWidth(null);
int height = bgImage.getHeight(null);
BufferedImage image = new BufferedImage(wideth, height, BufferedImage.TYPE_INT_ARGB);
Graphics2D g = image.createGraphics();
g.drawImage(bgImage, 0, 0, wideth, height, null);
//设置字体大小、颜色等
g.setColor(fontColor);
g.setFont(new Font(fontName, style, fontSize));
g.drawString(pressText, x, y);
g.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", out);
return out.toByteArray();
} catch (Exception e) {
System.out.println(e);
}
return null;
}
/**
* 向背景图片打印文字水印
*
* @param pressText 文字
* @param bgPath 背景图片路径(httpUrl或文件路径)
* @param x 横坐标
* @param y 纵坐标
* @param fontName 字体名
* @param fontSize 字体大小
* @param fontColor 字体颜色
* @param style 字体样式:例加粗+斜体:Font.BOLD + Font.ITALIC
*/
public static byte[] pressText(String pressText, String bgPath, int x, int y, String fontName,
int fontSize, Color fontColor, int style) {
try {
BufferedImage bgImage = getByPath(bgPath);
return pressText(pressText, bgImage, x, y, fontName, fontSize, fontColor, style);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
/**
* 根据路径获取图片流
*
* @param path 路径(httpUrl或文件路径)
* @return
* @throws IOException
*/
private static BufferedImage getByPath(String path) throws IOException {
BufferedImage image;
if (path.startsWith("http")) {
image = ImageIO.read(new URL(path));
} else {
image = ImageIO.read(new File(path));
}
return image;
}
/**
* 为背景图片增加水印图片
*
* @param watermarkImage 水印图片
* @param bgImage 背景图片
* @param x 横坐标
* @param y 纵坐标
* @param width 水印图片宽度,0原始宽度
* @param height 水印图片高度,0原始高度
* @return
*/
public static byte[] pressImage(BufferedImage watermarkImage, BufferedImage bgImage, int x, int y, int width, int height) {
try {
/**目标文件*/
int bgWidth = bgImage.getWidth(null);
int bgHeight = bgImage.getHeight(null);
BufferedImage image = new BufferedImage(bgWidth, bgHeight, BufferedImage.TYPE_INT_ARGB);
Graphics g = image.createGraphics();
g.drawImage(bgImage, 0, 0, bgWidth, bgHeight, null);
/**水印文件*/
width = width == 0 ? watermarkImage.getWidth() : width;
height = height == 0 ? watermarkImage.getHeight() : height;
g.drawImage(watermarkImage, x, y, width, height, null);
//水印文件结束
g.dispose();
ByteArrayOutputStream out = new ByteArrayOutputStream();
ImageIO.write(image, "jpg", out);
return out.toByteArray();
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
/**
* 为背景图片增加水印图片(水印图片原始大小)
*
* @param watermarkPath 水印图片路径
* @param bgPath 背景图片径
* @param x 横坐标
* @param y 纵坐标
* @param width 水印图片宽度,0原始宽度
* @param height 水印图片高度,0原始高度
* @return
*/
public static byte[] pressImage(String watermarkPath, String bgPath, int x, int y, int width, int height) {
try {
BufferedImage watermarkImage = getByPath(watermarkPath);
BufferedImage bgImage = getByPath(bgPath);
return pressImage(watermarkImage, bgImage, x, y, width, height);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
import org.junit.Test;
import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.io.*;
/**
* @Author: Waria
* @Date: 2020/2/20
*/
public class ImgUtilTest {
@Test
public void pressImage() {
//水印图片,此处用腾讯公开的二维码
String watermarkUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?" +
"ticket=gQH47joAAAAAAAAAASxodHRwOi8vd2VpeGluLnFxLmNvbS9xL2taZ2Z3TVRtNzJXV1Brb3ZhYmJJAAIEZ23sUwMEmm3sUw==";
//背景图片
String bgFileUrl = "E:\\image\\fissionBack.png";
try {
//把水印放在右下角的,x、y根据自己图片设定一下
byte[] endImg = ImgUtil.pressImage(watermarkUrl, bgFileUrl, 505, 775, 125, 125);
//E:\image\endImg.png
InputStream putS = new ByteArrayInputStream(endImg);
RenderedImage renderedImage = ImageIO.read(putS);
ImageIO.write(renderedImage, "png", new File("E:\\image\\endImg.png"));
} catch (IOException e) {
System.out.println("图片失败");
e.printStackTrace();
}
}
@Test
public void pressImage1() {
// 根据index获取资源文件的file对象
String bgFileUrl = "E:\\image\\fissionBack.png";
try {
BufferedImage bgImage = ImageIO.read(new File(bgFileUrl));
byte[] endImg = ImgUtil1.pressText("测试测试测试测试", bgImage, 30, 35,
"微软雅黑", 24, Color.BLACK, 0);
InputStream putS = new ByteArrayInputStream(endImg);
RenderedImage renderedImage = ImageIO.read(putS);
ImageIO.write(renderedImage, "png", new File("E:\\image\\endImg111.png"));
} catch (IOException e) {
System.out.println("图片失败");
e.printStackTrace();
}
}
}