java代码绘制生成图片(用于多张图片合成)
import java.awt.Color;
import java.awt.Font;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.File;
import javax.imageio.ImageIO;
public class ImageService {
public String background_img;
public String qrcode_img;
public String gzh_img;
public String outputdir;
public File createImage(String name) {
return overlapImage(background_img, gzh_img,qrcode_img, name, outputdir);
}
public File overlapImage(String backgroundPath, String gzhPath, String qrCodePath, String text, String outPutPath) {
try {
// 设置图片大小
//设置图片大小
BufferedImage background = resizeImage(848,1018, ImageIO.read(new File(backgroundPath)));
BufferedImage qrCode = resizeImage(80,80,ImageIO.read(new File(qrCodePath)));
BufferedImage gzh = resizeImage(80,80,ImageIO.read(new File(gzhPath)));
Graphics2D g = background.createGraphics();
g.setColor(Color.black);
g.setFont(new Font("微软雅黑",Font.PLAIN,40));
g.drawString(text,436 ,630);
//在背景图片上添加二维码图片
g.drawImage(qrCode, 760, 920, qrCode.getWidth(), qrCode.getHeight(), null);
g.drawImage(gzh, 660, 920, gzh.getWidth(), gzh.getHeight(), null);
g.dispose();
File out = new File(outPutPath + System.currentTimeMillis()+".jpg");
ImageIO.write(background, "jpg", out);
return out;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
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_SMOOTH), 0, 0, null);
return bufferedImage;
}
}