我的需求是:在照片下方加上照片描述.
思路:
1,先创建与照片宽度一样的白底图片,
2,在图片上写上对应照片的描述.
3.创建的白底图片与照片合成一张图片
一:创建图片
package tuPian;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
public class DrawPic {
public static void main(String[] args) throws IOException {
//照片的路径
String bigImg = "C:\\Users\\sclf\\Desktop\\开发临时文档\\任务状态\\正在开发\\新建文件夹\\";
File SJfile = new File(bigImg);
File[] listFiles = SJfile.listFiles();
//绘图.
for (File ffile : listFiles) {
int width = getImgWidth(ffile);//获取照片的宽度
int height = 60;//描述的高度自己定义
String substring = ffile.toString().substring(bigImg.length());
/*
* 正则表达式获取图片的名字.
* 当然你的名字如果不是 中文名+.jpg 用我这个正则表达式 是不生效的.需要你自己处理你的名字
*
*/
String re = "[r'-?/.jpg']";
String name = substring.replaceAll(re, "");
//设置图片的高度与宽度.
BufferedImage bi = new BufferedImage(width, height, BufferedImage.TYPE_INT_BGR);
//创建出来的图片存储的路径
final File file = new File("C:\\Users\\sclf\\Desktop\\开发临时文档\\任务状态\\正在开发\\描述图片\\"+name+".jpg");
if(file.exists()) {
file.delete();
file.createNewFile();
}
writeImage(bi, "jpg", file,width,height);
}
System.out.println("绘图成功");
}
/** 通过指定参数写一个图片 */
public static boolean writeImage(BufferedImage bi, String picType, File file,int width,int height) {
Graphics g = bi.createGraphics();
//设置底片的颜色
Color color = new Color(255,255,255);
g.setColor(color);
//底片的宽高为多少.如果不写宽高你的图片创建出来是黑色的
g.fillRect(0, 0, width, height);
g.dispose();
bi.flush();
boolean val = false;
try {
val = ImageIO.write(bi, picType, file);
} catch (IOException e) {
e.printStackTrace();
}
return val;
}
/**
* 获取图片宽度
* @param file 图片文件
* @return 宽度
*/
public static int getImgWidth(File file) {
InputStream is = null;
BufferedImage src = null;
int ret = -1;
try {
is = new FileInputStream(file);
src = javax.imageio.ImageIO.read(is);
ret = src.getWidth(null); // 得到源图宽
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
}
二:图片上写文字/或者 图片上插入图片
package tuPian;
import com.sun.image.codec.jpeg.JPEGCodec;
import com.sun.image.codec.jpeg.JPEGImageDecoder;
import com.sun.image.codec.jpeg.JPEGImageEncoder;
import javax.swing.*;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.text.ParseException;
public class aa2 {
public static void main(String[] args) throws ParseException {
try {
String content = null;
String outPath = null;
String bigImg = "C:\\Users\\sclf\\Desktop\\开发临时文档\\任务状态\\正在开发\\描述图片\\";
File SJfile = new File(bigImg);
File[] listFiles = SJfile.listFiles();
for (File file : listFiles) {
System.out.println(file);
int imgWidth = getImgWidth(file);
int imgHeight =45; //图片的高度.我给固定死了,你们随意变
String substring = file.toString().substring(bigImg.length());
String re = "[r'-?/.jpg']";
String name = substring.replaceAll(re, "");
//图片上需要写的内容
content = "院士肖像来源于国家出版基金项目 院士风采-中国工程院院士数字影像集";
//图片需要保存的位置
outPath = "C:\\Users\\sclf\\Desktop\\开发临时文档\\任务状态\\正在开发\\完整的描述\\" + name + ".jpg";
/*
* 第一个参数:String类型的图片路径
* 第二个参数:在图片上插入的图片--------------不用本功能传null就行
* 第三个参数:你要在图片上插入图片的 x 坐标(x,y)
* 第四个参数:你要在图片上插入图片的 y 坐标(x,y)
* 第五个参数:在图片上插入文本内容--------------不用本功能传null就行
* 第六个参数:你要在图片上插入图片的 x 坐标(x,y)
* 第七个参数:你要在图片上插入图片的 y 坐标(x,y)
* 第八个参数:需要把图片保存的路径
*/
bigImgAddSmallImgAndText(file.toString(), null, 222, 222, content, imgWidth / 12, imgHeight, outPath);
}
} catch (IOException e) {
e.printStackTrace();
}
}
/***
* 在一张大图张添加小图和文字
*
* @param bigImgPath
* 大图的路径
* @param smallImgPath
* 小图的路径
* @param sx
* 小图在大图上x抽位置
* @param sy
* 小图在大图上y抽位置
* @param content
* 文字内容
* @param cx
* 文字在大图上y抽位置
* @param cy
* 文字在大图上y抽位置
* @param outPathWithFileName
* 结果输出路径
*/
public static void bigImgAddSmallImgAndText(String bigImgPath, String smallImgPath, int sx, int sy, String content,
int cx, int cy, String outPathWithFileName) throws IOException {
// 主图片的路径
InputStream is = new FileInputStream(bigImgPath);
// 通过JPEG图象流创建JPEG数据流解码器
JPEGImageDecoder jpegDecoder = JPEGCodec.createJPEGDecoder(is);
// 解码当前JPEG数据流,返回BufferedImage对象
BufferedImage buffImg = jpegDecoder.decodeAsBufferedImage();
// 得到画笔对象
Graphics g = buffImg.getGraphics();
// 小图片的路径
if (smallImgPath != null) {
ImageIcon imgIcon = new ImageIcon(smallImgPath);
// 得到Image对象。
Image img = imgIcon.getImage();
// 将小图片绘到大图片上,5,300 .表示你的小图片在大图片上的位置。
g.drawImage(img, sx, sy, null);
// 设置颜色。
g.setColor(Color.WHITE);
}
// 院士肖像来源于国家出版基金项目 院士风采-中国工程院院士数字影像集
// 最后一个参数用来设置字体的大小
if (content != null) {
Font f = new Font("微软雅黑", Font.PLAIN, 24);
Color mycolor = Color.black;
g.setColor(mycolor);
g.setFont(f);
g.drawString(content, cx, cy); // 表示这段文字在图片上的位置(cx,cy) .第一个是你设置的内容。
}
g.dispose();
OutputStream os = new FileOutputStream(outPathWithFileName);
// 创键编码器,用于编码内存中的图象数据。
JPEGImageEncoder en = JPEGCodec.createJPEGEncoder(os);
en.encode(buffImg);
is.close();
os.close();
}
/**
* 获取图片宽度
* @param file 图片文件
* @return 宽度
*/
public static int getImgWidth(File file) {
InputStream is = null;
BufferedImage src = null;
int ret = -1;
try {
is = new FileInputStream(file);
src = javax.imageio.ImageIO.read(is);
ret = src.getWidth(null); // 得到源图宽
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
/**
* 获取图片宽度
* @param file 图片文件
* @return 高度
*/
public static int getImgHeight(File file) {
InputStream is = null;
BufferedImage src = null;
int ret = -1;
try {
is = new FileInputStream(file);
src = javax.imageio.ImageIO.read(is);
ret = src.getHeight();// 得到源图宽
is.close();
} catch (Exception e) {
e.printStackTrace();
}
return ret;
}
}
三:图片合成
点击下边连接.已经写过一片图片合成的文章.参考以下连接