Java获取照片EXIF信息——看看你的图片有没有ps?

package com.yc.image;


import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.wb.swt.SWTResourceManager;


import com.drew.imaging.ImageMetadataReader;
import com.drew.metadata.Directory;
import com.drew.metadata.Metadata;
import com.drew.metadata.Tag;


import org.eclipse.swt.widgets.Composite;


import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;


import org.eclipse.swt.SWT;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.custom.SashForm;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.ControlAdapter;
import org.eclipse.swt.events.ControlEvent;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.eclipse.swt.custom.ScrolledComposite;


public class ImageInfo {


protected Shell shell;


/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
ImageInfo window = new ImageInfo();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}


/**
* Open the window.
*/
public void open() {
Display display = Display.getDefault();
createContents();
shell.open();
shell.layout();
while (!shell.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}


/**
* Create contents of the window.
*/
protected void createContents() {
shell = new Shell();
shell.setImage(SWTResourceManager.getImage(ImageInfo.class, "/images/yc.ico"));
shell.setSize(825,700);
shell.setText("图片信息");
shell.setLayout(new FillLayout(SWT.HORIZONTAL));
shell.setLocation((Display.getDefault().getClientArea().width - shell.getSize().x) / 2,10);


Composite composite = new Composite(shell, SWT.NONE);
composite.setLayout(new FillLayout(SWT.HORIZONTAL));


final SashForm sashForm = new SashForm(composite, SWT.NONE);


sashForm.setOrientation(SWT.VERTICAL);


Composite composite_1 = new Composite(sashForm, SWT.NONE);


Button btnNewButton = new Button(composite_1, SWT.NONE);


btnNewButton.setBounds(491, 46, 80, 27);
btnNewButton.setText("点击选择图片");


final Label photo = new Label(composite_1, SWT.NONE);
photo.setBounds(360, 10, 100, 100);


ScrolledComposite scrolledComposite = new ScrolledComposite(sashForm, SWT.BORDER | SWT.H_SCROLL | SWT.V_SCROLL);
scrolledComposite.setExpandHorizontal(true);
scrolledComposite.setExpandVertical(true);


Composite composite_2 = new Composite(scrolledComposite, SWT.NONE);
composite_2.setLayout(new FillLayout(SWT.HORIZONTAL));


final Label info = new Label(composite_2, SWT.NONE);
scrolledComposite.setContent(composite_2);
sashForm.setWeights(new int[] {125, 458});
scrolledComposite.setMinSize(820,2000);


sashForm.addControlListener(new ControlAdapter() {
@Override
public void controlResized(ControlEvent e) {
sashForm.setWeights(new int[] {125, sashForm.getSize().y-125});
}
});


//图片选择操作
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
FileDialog fd = new FileDialog(shell);
fd.setFilterExtensions(new String[]{"*.jpg","*.gif","*.png" });
String filePath=fd.open();
if(filePath!=null && !"".equals(filePath)){
File file= new File(filePath);
if (file.exists()) {
photo.setImage( previewImage(photo,file) );
try {
showPicInfo(file,info);
} catch (Exception e1) {
e1.printStackTrace();
}
}
}
}
});
}


/**
* 图片缩放
* @param label 显示图片的label
* @param path 要显示的图片路径
* @return
*/
private Image previewImage(Label label,File file) {
Image image = null;
InputStream is=null;
try {
is = new FileInputStream(file);
ImageData imageData = new ImageData(is);
imageData = imageData.scaledTo(label.getSize().x, label.getSize().y);
image=new Image(null,imageData);
} catch (Exception e) {
e.printStackTrace();
} finally{
if(is!=null){
try {
is.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return image;
}


private void  showPicInfo(File fl,Label label) throws Exception{
//需要  metadata-extractor.jar包 和xmpcore.jar包  本案例使用的是metadata-extractor-2.8.1.jar和xmpcore-5.1.2.jar
Metadata metadata = ImageMetadataReader.readMetadata(fl); 
Iterable<Directory> iterable=metadata.getDirectories();
StringBuffer info=new StringBuffer();
for(Directory itr:iterable){
for(Tag tag:itr.getTags()){
info.append(tag.toString()+"\r\n");
}
}
label.setText(info.toString());
}
}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值