Java获取照片EXIF信息——看看你的图片有没有ps?
2017-05-18 12:58
901 查看
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());
}
}
相关文章推荐
- Java获取照片EXIF信息
- Java获取照片EXIF信息
- Java获取照片EXIF信息
- java获取文件exif信息-添加图片文字水印
- Java获取照片EXIF信息
- Java获取照片EXIF信息
- Java读取图片和EXIF信息
- IOS 如何获取与修改图片的EXIF信息
- JAVA获取JPEG文件Exif相关信息
- Android -- 加载大图片到内存,从gallery获取图片,获取图片exif信息
- 利用php获取图片完整Exif信息类 获取图片详细完整信息类
- (四十三)获取图片exif信息
- C# 如何获取照片中Exif信息里GPS信息与经纬度数
- java有没有开源框架可以获取svn信息的
- java读取图片的(尺寸、拍摄日期、标记)等EXIF信息
- C# 获取图片的EXIF 信息
- IOS: 使用imageIO获取和修改图片的exif信息
- 获取照片Exif信息
- C#图片处理之: 获取数码相片的EXIF信息(二)
- Java获取图像Exif信息