主要是将logo,二维码接入模板 细节处理在上一篇文章体现
图片的处理工具使用开源插件thumbnailator,具体可实现的功能有介绍的很完整的帖子了,借鉴了里面的功能,完成了图片的放缩处理,很适合我这样的小白
import net.coobird.thumbnailator.Thumbnails;
import net.coobird.thumbnailator.name.Rename;
import java.io.*;
public class Handle {
public void testHandlePicture() throws IOException {
/* File logofromPic = new File("E:\\logo\\C4.jpg");
File logotoPic = new File("E:\\logo缩小图\\C4.jpg");
//按照指定大小把图片进行缩放(不遵循原图比例)
Thumbnails.of(logofromPic).size(200, 100).keepAspectRatio(false).toFile(logotoPic);
*/ try{
//上面的方法一样可以使用,这是在测试的时候我又算好比例测试的
Thumbnails.of(new File("E:\\企业logo\\logo").listFiles()).scale(1.25).toFiles(new File("E:\\企业logo缩小图"),Rename.NO_CHANGE);
}catch (IOException e){
// System.out.println("原因:"+e.getMessage()); e.printStackTrace();
}
/* File QRfromPic = new File("E:\\二维码\\Q4.jpg");
File QRtoPic = new File("E:\\二维码缩小图\\Q4.jpg");
//按照指定大小把图片进行缩放(遵循原图比例)
Thumbnails.of(QRfromPic).size(200, 200).toFile(QRtoPic);*/
/*偷懒使用循环体批处理了*/
for (int i=1;i<15;i=i+1) {
File QRfromPic = new File("E:\\png格式logo\\H" +i +".png");
File QRtoPic = new File("E:\\二维码缩小图\\H"+i +".png");
Thumbnails.of(QRfromPic).size(200, 200).toFile(QRtoPic);
}
}
public static void main(String[] args) throws IOException {
try {
/*通过循环将一个目录下的图片进行格式转换处理*/
for (int i = 1; i < 15; i++) {
File fromPic = new File("E:\\二维码缩小图\\H" + i + ".jpg");
File toPic = new File("E:\\png格式logo\\H" + i + ".png");
Thumbnails.of(fromPic).scale(1f).outputFormat("png").toFile(toPic);
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
以下代码参考资料:https://blog.csdn.net/hongwangzhang/article/details/50748356
还有是从一个16年发布的帖子里参考的,找不到地址了,唉
import javax.imageio.ImageIO;
import java.awt.*
;import java.awt.image.BufferedImage;
import java.io.File;import java.io.IOException;
import java.text.ParseException;
public class ImageMerge {
public static void main (String[] args)throws ParseException{
String templateImg_1 = "E:\\模板\\2.png";
String logoImg_1 = "E:\\C1.jpg";
String QRcodeImg_1 = "E:\\Q1.jpg";
String outPath_1 = "E:\\合成贺卡2.jpg";
try{
//参数具体自己计算原图,看放哪合适
templateImgAddlogoImgAndQRcodeImg(templateImg_1,logoImg_1,QRcodeImg_1,275,150,275,736,outPath_1);
}catch (IOException e){
e.printStackTrace();
}
String templateImg = "E:\\1.jpg";
String QRcodeImg = "E:Q3.jpg";
String outPath = "E:\\合成贺卡.jpg";
String content = "祝福语";
try {
templateImgAddQRcodeImgAndText(templateImg, QRcodeImg, 57, 1099, content, 135, 1028, outPath);
} catch (IOException e) {
e.printStackTrace();
}
}
/*在模板图上添加二维码和logo
@param templateImgPath 模板路径
@param logoImgPath logo路径
@param QRcodeImgPath 二维码路径
@param lx logo在模板x轴位置
@param ly logo在模板Y轴位置
@param Qx 二维码在模板X轴位置
@param Qy 二维码在模板Y轴位置
@param outPathWithFileName 输出结果路径
*/
public static void templateImgAddlogoImgAndQRcodeImg(String templateImgPath, String logoImgPath, String QRcodeImgPath, int lx,int ly,int Qx,int Qy, String outPathWithFileName) throws IOException {
//读取模板
BufferedImage template = ImageIO.read(new File(templateImgPath));
int width = template.getWidth(); int height = template.getHeight();
//读取二维码 BufferedImage QR = ImageIO.read(new File(QRcodeImgPath));
int Qwight = QR.getWidth(); int Qheight = QR.getHeight();
//读取logo BufferedImage logo = ImageIO.read(new File(logoImgPath));
int lwight = logo.getWidth(); int lheight = logo.getHeight();
//创建画布 BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = img.createGraphics();
//载入模板
g.drawImage(template,0,0,width,height,null);
//二维码在模板上的位置
g.drawImage(QR, Qx, Qy,Qwight,Qheight, null);
//logo在模板上的位置
g.drawImage(logo, lx, ly,lwight,lheight, null);
g.dispose();
ImageIO.write(img,"jpg",new File(outPathWithFileName));
}
public static void templateImgAddQRcodeImgAndText(String templateImgPath, String QRcodeImgPath, int Qx,int Qy, String content,int fx,int fy, String outPathWithFileName) throws IOException{
//读取模板图
BufferedImage template = ImageIO.read(new File(templateImgPath));
int width = template.getWidth(); int height = template.getHeight();
//读取二维码
BufferedImage QR = ImageIO.read(new File(QRcodeImgPath));
int Qwight = QR.getWidth(); int Qheight = QR.getHeight();
//创建画布
BufferedImage img = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics g = img.createGraphics();
//载入模板
g.drawImage(template,0,0,width,height,null);
//二维码在模板上的位置
g.drawImage(QR, Qx, Qy,Qwight,Qheight, null);
g.setColor(Color.WHITE);
if (content != null){
//这个看自己具体情况了
Font font = new Font("黑体",Font.PLAIN,36);
Color color = Color.orange; g.setColor(color);
g.setFont(font);
g.drawString(content,fx,fy);
}
g.dispose();
ImageIO.write(img,"jpg",new File(outPathWithFileName));
}
}
代码是可以实现效果图的,就是需要多调试吧,我用IDEA写的,第一的小项目看的视频就是用的IDEA,所以用的比较顺手(勉强在慢慢摸索)感谢学校有学生邮箱,让我可以使用一年免费的,明年到期了再找找学弟学妹们了