参考网站
https://my.oschina.net/u/2426590/blog/3027726
java Graphics2D绘制文字并居中并解决服务器乱码问题
https://blog.youkuaiyun.com/w410589502/article/details/72898184
我自己的代码:
import java.awt.Color;
import java.awt.Font;
import java.awt.FontMetrics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.Transparency;
import java.awt.geom.Ellipse2D;
import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.net.URL;
import javax.imageio.ImageIO;
import javax.imageio.stream.ImageOutputStream;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import com.platform.annotation.IgnoreAuth;
import com.platform.utils.util.OssUtil;
@Controller
@RequestMapping(value = "/api/img")
public class ImageUtil {
static final String IMAGE_SUFFIX = ".png";
/**
* @param posterImgUrl
* 海报
* @param tempQrCodeImgUrl
* 临时二维码
* @param headImgUrl
* 头像 nameText 昵称 cqText 二维码文字
* @return 合成图片地址
*/
public static String drawImage(String posterImgUrl, String tempQrCodeImgUrl, String headImgUrl, String nameText,
String cqText) throws IOException {
int width = 620;
int height = 1004;
BufferedImage bgBufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// RGB形式
Graphics2D bgBufImageGraphics = bgBufImage.createGraphics();
bgBufImageGraphics.setBackground(Color.WHITE);// 设置背景色
bgBufImageGraphics.clearRect(0, 0, width, height);// 通过使用当前绘图表面的背景色进行填充来清除指定的矩形。
// bgBufImageGraphics.setBackground(new Color(255,255,255));
bgBufImageGraphics.setPaint(Color.black);// 设置画笔,设置Paint属性
Font font = new Font("宋体", Font.PLAIN, 28);
bgBufImageGraphics.setFont(font);
// 抗锯齿
bgBufImageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 计算文字长度,计算居中的x点坐标
FontMetrics fm = bgBufImageGraphics.getFontMetrics(font);
int textWidth = fm.stringWidth(nameText);
// int widthX = (width - textWidth) / 2;
int widthX = 128;
// 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
bgBufImageGraphics.drawString(nameText, widthX, 926);
// 计算文字长度,计算居中的x点坐标
// int widthX = (width - textWidth) / 2;
int cqCodeWidthX = 372;
// 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
bgBufImageGraphics.drawString(cqText, cqCodeWidthX, 958);
// BufferedImage posterBufImage = ImageIO.read(new URL(posterImgUrl));
// //直接使用图片做背景,自定义背景使用上面方式
// Graphics2D posterBufImageGraphics = posterBufImage.createGraphics();
BufferedImage posterBufImage = ImageIO.read(new URL(posterImgUrl));
BufferedImage qrCodeImage = ImageIO.read(new URL(tempQrCodeImgUrl));
BufferedImage headImage = ImageIO.read(new URL(headImgUrl));
// 设置圆形头像
BufferedImage roundHeadImg = new BufferedImage(headImage.getWidth(), headImage.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D roundHeadGraphics = roundHeadImg.createGraphics();
Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, roundHeadImg.getWidth(), roundHeadImg.getHeight());
roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
roundHeadImg = roundHeadGraphics.getDeviceConfiguration().createCompatibleImage(headImage.getWidth(),
headImage.getHeight(), Transparency.TRANSLUCENT);
roundHeadGraphics = roundHeadImg.createGraphics();
// 使用 setRenderingHint 设置抗锯齿
roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
roundHeadGraphics.setClip(shape);
roundHeadGraphics.drawImage(headImage, 0, 0, null);
roundHeadGraphics.dispose();
// bgBufImageGraphics.drawImage(qrCodeImage, (posterBufImage.getWidth()
// - qrCodeImage.getWidth()), 10, qrCodeImage.getWidth(),
// qrCodeImage.getHeight(), null);
// posterBufImageGraphics.drawImage(roundHeadImg, 50, 100,
// HEAD_URL_WIDTH, HEAD_URL_HEIGHT, null);
bgBufImageGraphics.drawImage(qrCodeImage, 444, 848, 72, 72, null);
bgBufImageGraphics.drawImage(roundHeadImg, 32, 876, 80, 80, null);
bgBufImageGraphics.drawImage(posterBufImage, 0, 0, 620, 826, null);
bgBufImageGraphics.dispose();
ByteArrayOutputStream bs = new ByteArrayOutputStream();
ImageOutputStream imgOut = ImageIO.createImageOutputStream(bs);
ImageIO.write(bgBufImage, "png", imgOut);
// 上传到服务器上
// InputStream inSteam = new ByteArrayInputStream(bs.toByteArray());
// String url = OSSFactory.build().uploadSuffix(inSteam, IMAGE_SUFFIX);
// 上传到服务器上
String url = OssUtil.uploadObject2OSS(bs.toByteArray());
return url;// 返回合成的图片地址url
}
public static void drawImage() throws IOException {
// 海报图
// String backgroundImageUrl =
// "http://img1.juimg.com/171010/330841-1G01000050879.jpg";
String backgroundImageUrl = "http://diaosaas-prod.oss-cn-shenzhen.aliyuncs.com/education/155547979572510070.jpg";
// 二维码
String qrCodeImageUrl = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553504957130&di=02fae20a5c0f885d52299b2b1d682c86&imgtype=0&src=http%3A%2F%2Fimg.atobo.com%2FProductImg%2FEWM%2FUWeb%2F3%2F2%2F1%2F3%2F061%2F3213061%2F1.gif";
// 头像
// String headUrl =
// "http://pic.51yuansu.com/pic3/cover/00/63/25/589bdedf5475d_610.jpg";
String headUrl = "http://thirdwx.qlogo.cn/mmopen/vi_32/ZW8dgKbUicrCdotRItMTpAaxibAwj6nVMJvQoGLyKbaRvah93dXWrENSoum20dKicnJqd2xh1U4NhmC41L7q2Gwqg/132";
String nameText = "古月争取";
String cqText = "扫一扫,为我加油!";
int width = 310;
int height = 502;
BufferedImage bgBufImage = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);// RGB形式
Graphics2D bgBufImageGraphics = bgBufImage.createGraphics();
bgBufImageGraphics.setBackground(Color.WHITE);// 设置背景色
bgBufImageGraphics.clearRect(0, 0, width, height);// 通过使用当前绘图表面的背景色进行填充来清除指定的矩形。
// bgBufImageGraphics.setBackground(new Color(255,255,255));
bgBufImageGraphics.setPaint(Color.black);// 设置画笔,设置Paint属性
Font font = new Font("宋体", Font.PLAIN, 14);
bgBufImageGraphics.setFont(font);
// 抗锯齿
bgBufImageGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
// 计算文字长度,计算居中的x点坐标
FontMetrics fm = bgBufImageGraphics.getFontMetrics(font);
int textWidth = fm.stringWidth(nameText);
// int widthX = (width - textWidth) / 2;
int widthX = 64;
// 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
bgBufImageGraphics.drawString(nameText, widthX, 463);
// 计算文字长度,计算居中的x点坐标
// int widthX = (width - textWidth) / 2;
int cqCodeWidthX = 186;
// 表示这段文字在图片上的位置(x,y) .第一个是你设置的内容。
bgBufImageGraphics.drawString(cqText, cqCodeWidthX, 479);
BufferedImage posterImage = ImageIO.read(new URL(backgroundImageUrl));
BufferedImage qrCodeImage = ImageIO.read(new URL(qrCodeImageUrl));
BufferedImage headImage = ImageIO.read(new URL(headUrl));
// 设置圆形图片
BufferedImage roundHeadImg = new BufferedImage(headImage.getWidth(), headImage.getHeight(),
BufferedImage.TYPE_INT_RGB);
Graphics2D roundHeadGraphics = roundHeadImg.createGraphics();
Ellipse2D.Double shape = new Ellipse2D.Double(0, 0, roundHeadImg.getWidth(), roundHeadImg.getHeight());
roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
roundHeadImg = roundHeadGraphics.getDeviceConfiguration().createCompatibleImage(headImage.getWidth(),
headImage.getHeight(), Transparency.TRANSLUCENT);
roundHeadGraphics = roundHeadImg.createGraphics();
// 使用 setRenderingHint 设置抗锯齿
roundHeadGraphics.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
roundHeadGraphics.setClip(shape);
roundHeadGraphics.drawImage(headImage, 0, 0, null);
roundHeadGraphics.dispose();
bgBufImageGraphics.drawImage(qrCodeImage, 444, 848, 72, 72, null);
// bgBufImageGraphics.drawImage(qrCodeImage, (bgBufImage.getWidth() -
// qrCodeImage.getWidth()), 10, qrCodeImage.getWidth(),
// qrCodeImage.getHeight(), null);
bgBufImageGraphics.drawImage(roundHeadImg, 32, 876, 80, 80, null);
bgBufImageGraphics.drawImage(posterImage, 0, 0, 620, 826, null);
bgBufImageGraphics.dispose();
ImageIO.write(bgBufImage, "png", new File("E:\\demo1.png"));
System.out.println("生成图片完成");
}
/**
* 上传到网络
*
* @author LHB
* @since JDK 1.8
*/
/*
* public static void main(String[] args) throws IOException { //海报图 String
* backgroundImageUrl =
* "http://47.92.209.5:8080/upload/15460848171054280132559.jpg"; // String
* backgroundImageUrl =
* "http://img1.juimg.com/171010/330841-1G01000050879.jpg"; //二维码 // String
* qrCodeImageUrl =
* "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553504957130&di=02fae20a5c0f885d52299b2b1d682c86&imgtype=0&src=http%3A%2F%2Fimg.atobo.com%2FProductImg%2FEWM%2FUWeb%2F3%2F2%2F1%2F3%2F061%2F3213061%2F1.gif";
* String qrCodeImageUrl =
* "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQE18TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyNEhIcXBjNk1mZzExMDAwMDAwN0UAAgR-AMNcAwQAAAAA";
* //头像 String headUrl =
* "http://thirdwx.qlogo.cn/mmopen/vi_32/J8e7icsMJ9r122G4h349YdB3WewUyaICxuoqC0HsaHDOfuRibPiclcOP8mnfQ5FoZLA1Q9SCe9SH46H72U25rAG9A/132";
* // String headUrl =
* "http://pic.51yuansu.com/pic3/cover/00/63/25/589bdedf5475d_610.jpg";
*
* String nameText = "古月争取"; String cqText = "扫一扫,为我加油!"; String url =
* ImageUtil.drawImage(backgroundImageUrl, qrCodeImageUrl,
* headUrl,nameText,cqText); System.out.println("url={} "+url);
*
*
* }
*/
// 上传到本地
// public static void main(String[] args) throws IOException {
// drawImage();
//
// }
@IgnoreAuth
@RequestMapping("/testIMG")
public void test() throws IOException {
// 海报图
String backgroundImageUrl = "http://47.92.209.5:8080/upload/15460848171054280132559.jpg";
// String backgroundImageUrl =
// "http://img1.juimg.com/171010/330841-1G01000050879.jpg";
// 二维码
// String qrCodeImageUrl =
// "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1553504957130&di=02fae20a5c0f885d52299b2b1d682c86&imgtype=0&src=http%3A%2F%2Fimg.atobo.com%2FProductImg%2FEWM%2FUWeb%2F3%2F2%2F1%2F3%2F061%2F3213061%2F1.gif";
String qrCodeImageUrl = "https://mp.weixin.qq.com/cgi-bin/showqrcode?ticket=gQE18TwAAAAAAAAAAS5odHRwOi8vd2VpeGluLnFxLmNvbS9xLzAyNEhIcXBjNk1mZzExMDAwMDAwN0UAAgR-AMNcAwQAAAAA";
// 头像
String headUrl = "http://thirdwx.qlogo.cn/mmopen/vi_32/J8e7icsMJ9r122G4h349YdB3WewUyaICxuoqC0HsaHDOfuRibPiclcOP8mnfQ5FoZLA1Q9SCe9SH46H72U25rAG9A/132";
// String headUrl =
// "http://pic.51yuansu.com/pic3/cover/00/63/25/589bdedf5475d_610.jpg";
String nameText = "古月争取";
String cqText = "扫一扫,为我加油!";
String url = ImageUtil.drawImage(backgroundImageUrl, qrCodeImageUrl, headUrl, nameText, cqText);
System.out.println("url={} " + url);
}
}