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()); } }