
import sun.font.FontDesignMetrics;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
public static void main(String[] args) {
String backgroundPath = "C:\\Users\\Administrator\\Desktop\\海报\\haibaobeijing.jpg";
String qrCodePath = "C:\\Users\\Administrator\\Desktop\\海报\\xiaochnegxuma.jpg";
String people_img="C:\\Users\\Administrator\\Desktop\\海报\\renwu1.png";
String people_img1="C:\\Users\\Administrator\\Desktop\\海报\\renwu1.png";
String message01 ="山东地方个地方官地方官,方大哥大地飞歌电饭锅电饭锅电饭,锅电饭锅地方官方的,个地方官的山东地方个地方官地!!";
String outPutPath="D:\\ruoyi\\qr1.jpg";
overlapImage(backgroundPath,qrCodePath,people_img,people_img1,message01,message02,outPutPath,
age,mHeight,aihao,xueli,zhiye,xianju
public static String overlapImage(String backgroundPath,String qrCodePath,
String people_img,String people_img1,
String message01,String message02,
String outPutPath,String age,
String mHeight,String aihao,
String xueli,String zhiye,
InputStream inputStream = new FileInputStream(backgroundPath);
BufferedImage bi = ImageIO.read(inputStream);
BufferedImage background = resizeImage(width,height, bi);
InputStream inputStream1 = new FileInputStream(qrCodePath);
BufferedImage bi1 = ImageIO.read(inputStream1);
BufferedImage qrCode = resizeImage(60,60, bi1);
InputStream inputStream2 = new FileInputStream(people_img);
BufferedImage bi2 = ImageIO.read(inputStream2);
BufferedImage peopleImg = resizeImage(261,381, bi2);
InputStream inputStream3 = new FileInputStream(people_img1);
BufferedImage bi3 = ImageIO.read(inputStream3);
BufferedImage peopleImg1 = resizeImage(309,216, bi3);
Graphics2D g = background.createGraphics();
Color color1=new Color(142,127,132);
g.setFont(new Font("",Font.BOLD,35));
Font font=new Font("",Font.BOLD,35);
FontMetrics fm = g.getFontMetrics(font);
int textWidth = fm.stringWidth(message02);
int widthX = (width - textWidth) / 2;
g.drawString(message02,widthX ,50);
g.setFont(new Font("",Font.PLAIN,fontsize2));
Font font1=new Font("",Font.PLAIN,fontsize2);
drawWordAndLineFeed(g,font1,message01,70,755,255);
g.setFont(new Font("",Font.BOLD,fontsize2));
Color color2=new Color(241,77,128);
FontMetrics fm1 = g.getFontMetrics(font1);
textWidth =fm1.stringWidth(age);
drawWordAndLineFeed(g,font1,age,126+(52-textWidth)/2,673,52);
textWidth =fm1.stringWidth(mHeight);
drawWordAndLineFeed(g,font1,mHeight,250+(52-textWidth)/2,673,52);
textWidth =fm1.stringWidth(aihao);
drawWordAndLineFeed(g,font1,aihao,375+(52-textWidth)/2,673,52);
textWidth =fm1.stringWidth(xueli);
drawWordAndLineFeed(g,font1,xueli,126+(52-textWidth)/2,718,52);
textWidth =fm1.stringWidth(mHeight);
drawWordAndLineFeed(g,font1,zhiye,250+(52-textWidth)/2,718,52);
textWidth =fm1.stringWidth(xueli);
drawWordAndLineFeed(g,font1,xianju,375+(52-textWidth)/2,718,52);
g.drawImage(peopleImg, 71, 65, peopleImg.getWidth(), peopleImg.getHeight(), null);
g.drawImage(peopleImg1, 121, 361, peopleImg1.getWidth(), peopleImg1.getHeight(), null);
g.drawImage(qrCode, 416, 804, qrCode.getWidth(), qrCode.getHeight(), null);
System.out.printf("outPutPath"+outPutPath);
ImageIO.write(background, "jpg", new File(outPutPath));
private static int drawWordAndLineFeed(Graphics2D g2d, Font font, String words, int wordsX, int wordsY, int wordsWidth) {
FontDesignMetrics metrics = FontDesignMetrics.getMetrics(font);
int height = metrics.getHeight();
int total = words.length();
System.out.println("几行字--"+total);
for (int i = 0; i < total; i++) {
if (width <= wordsWidth) {
width += metrics.charWidth(words.charAt(i));
String substring = subWords.substring(0, count);
MyDrawString(substring, wordsX, wordsY + (b * height),1.2,g2d);
subWords = subWords.substring(count);
g2d.drawString(subWords, wordsX, wordsY + (b * height));
zuidi=wordsY + (b * height);
System.out.println("高度--"+zuidi+25);
public static void MyDrawString(String str,int x,int y,double rate,Graphics g){
String tempStr=new String();
int orgStringWight=g.getFontMetrics().stringWidth(str);
int orgStringLength=str.length();
tempStr=str.substring(0, 1);
str=str.substring(1, str.length());
g.drawString(tempStr, tempx, tempy);
tempx=(int)(tempx+(double)orgStringWight/(double)orgStringLength*rate);
private static File getFile(String url) throws Exception {
String fileName = url.substring(url.lastIndexOf("."),url.length());
InputStream inStream = null;
file = File.createTempFile("net_url", fileName);
inStream = urlfile.openStream();
os = new FileOutputStream(file);
byte[] buffer = new byte[8192];
while ((bytesRead = inStream.read(buffer, 0, 8192)) != -1) {
os.write(buffer, 0, bytesRead);
public static BufferedImage resizeImage(int x, int y, BufferedImage bfi){
BufferedImage bufferedImage = new BufferedImage(x, y, BufferedImage.TYPE_INT_RGB);
bufferedImage.getGraphics().drawImage(
bfi.getScaledInstance(x, y, Image.SCALE_DEFAULT), 0, 0, null);