java获取文件exif信息-添加图片文字水印

需要添加jar:metadata-extractor-2.3.1.jar
代码:

public class ImageInfo {
private Integer id;
private String author;
private String desc;
private String date;
private String width;
private String height;
public ImageInfo(Integer id, String author, String desc,String date) {
super();
this.id = id;
this.author = author;
this.desc = desc;
this.date=date;
}
public ImageInfo(String author, String desc) {
super();
this.id = 0;
this.author = author;
this.desc = desc;
}
public ImageInfo() {
super();
// TODO Auto-generated constructor stub
}
/**
* @return the id
*/
public Integer getId() {
return id;
}
/**
* @param id the id to set
*/
public void setId(Integer id) {
this.id = id;
}
/**
* @return the author
*/
public String getAuthor() {
return author;
}
/**
* @param author the author to set
*/
public void setAuthor(String author) {
this.author = author;
}
/**
* @return the desc
*/
public String getDesc() {
return desc;
}
/**
* @param desc the desc to set
*/
public void setDesc(String desc) {
this.desc = desc;
}


/* (non-Javadoc)
* @see java.lang.Object#toString()
*/

/**
* @return the date
*/
public String getDate() {
return date;
}
/**
* @param date the date to set
*/
public void setDate(String date) {
this.date = date;
}

/**
* @return the width
*/
public String getWidth() {
return width;
}
/**
* @param width the width to set
*/
public void setWidth(String width) {
this.width = width;
}
/**
* @return the height
*/
public String getHeight() {
return height;
}
/**
* @param height the height to set
*/
public void setHeight(String height) {
this.height = height;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "ImageInfo [id=" + id + ", author=" + author + ", desc=" + desc
+ ", date=" + date + ", width=" + width + ", height=" + height
+ "]";
}



}




public static void createMark(String souchFilePath, String targetFilePath,
String markContent, Color markContentColor, float qualNum,
String fontType, int fontSize) {

markContentColor = Color.red;
ImageIcon imgIcon = new ImageIcon(souchFilePath);
Image theImg = imgIcon.getImage();
// Image可以获得 输入图片的信息
int width = theImg.getWidth(null);
int height = theImg.getHeight(null);

// 800 800 为画出图片的大小
BufferedImage bimage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
// 2d 画笔
Graphics2D g = bimage.createGraphics();
g.setColor(markContentColor);
g.setBackground(Color.white);

// 画出图片-----------------------------------
g.drawImage(theImg, 0, 0, null);
// 画出图片-----------------------------------

// --------对要显示的文字进行处理--------------
AttributedString ats = new AttributedString(markContent);
Font f = new Font(fontType, Font.BOLD, fontSize);
ats.addAttribute(TextAttribute.FONT, f, 0, markContent.length());
AttributedCharacterIterator iter = ats.getIterator();
// ----------------------
int wd = getLength(markContent) * fontSize;
g.drawString(iter, width-wd-fontSize-fontSize,
height - 40);
// 添加水印的文字和设置水印文字出现的内容 ----位置
g.dispose();// 画笔结束
try {
// 输出 文件 到指定的路径
FileOutputStream out = new FileOutputStream(targetFilePath);
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);

JPEGEncodeParam param = encoder.getDefaultJPEGEncodeParam(bimage);

param.setQuality(qualNum, true);
encoder.encode(bimage, param);
out.close();
} catch (Exception e) {
e.printStackTrace();
}
}


public static int getLength(String str){
int wd = 0;
int p = 0;
int s = 0;
for (int i = 0; i < str.length(); i++){
char c = str.charAt(i);
if ((c >= 0x4e00)&&(c <= 0x9fbb)){
s++;
}else{
p++;
}
}
wd = s + p/2;
return wd;
}


public static ImageInfo getImageInfo(String filePath){
ImageInfo imageInfo = new ImageInfo();
File jpegFile = new File(filePath);
Metadata metadata = null ;
try {
metadata = JpegMetadataReader.readMetadata(jpegFile);
Directory exif = metadata.getDirectory(ExifDirectory.class);
Iterator tags = exif.getTagIterator();
while (tags.hasNext()) {
Tag tag = (Tag)tags.next();
if(tag.getTagType()==ExifDirectory.TAG_WIN_COMMENT){
imageInfo.setDesc(tag.getDescription());
}
if(tag.getTagType()==ExifDirectory.TAG_WIN_AUTHOR){
imageInfo.setAuthor(tag.getDescription());
}
if(tag.getTagType()==ExifDirectory.TAG_DATETIME){
imageInfo.setDate(tag.getDescription().substring(0,10).replace(":","."));
}
if(tag.getTagType()==ExifDirectory.TAG_EXIF_IMAGE_WIDTH){
imageInfo.setWidth(tag.getDescription().substring(0,10).split(" ")[0]);
}
if(tag.getTagType()==ExifDirectory.TAG_EXIF_IMAGE_HEIGHT){
imageInfo.setHeight(tag.getDescription().substring(0,10).split(" ")[0]);
}
}
return imageInfo;
} catch (JpegProcessingException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (MetadataException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return null;
}

public static void main(String[] args) throws Exception {
String fileName = "DSC01221.JPG";
String inFilePath = "F:/images/nice/" + fileName;
String outFilePath = "F:/" + fileName;
ImageInfo imageInfo = getImageInfo(inFilePath);
System.out.println(imageInfo.toString());
int p = Integer.parseInt(imageInfo.getWidth())/52;
System.out.println(p);
ImageUtils.createMark(inFilePath, outFilePath, imageInfo.getAuthor()+" "+imageInfo.getDesc()+" "+imageInfo.getDate(), null, 1, "",p);
}




如有疑问,欢迎加入群:283948248 找群主
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值