如何加载图像以在RCP中查看?

我正在开发一个包含某些视图的RCP plugin project一个视图获取员工详细信息,如nameaddress等。有一个选项可以使用浏览按钮上传员工图像。第二个视图显示在第一个视图中输入的详细信息。所有详细信息除了照片显示正常。

它在照片标签的位置显示一个红色正方形。 我设置照片的代码如下所示:

  Label photoLabel = new Label(parent, SWT.NONE); photoLabel.setBounds(420, 233, 100, 106); photoLabel.setImage(SWTResourceManager.getImage(FormDataViewClass.class,photoUploadPath)); 

其中photoUploadPath是包含上传照片路径的字符串变量。 我该如何解决这个问题?

以下代码段帮助我解决了上述问题。

 byte[] uploadedImg = null; try { File f1 = new File(photoUploadPath); double fileLen = f1.length(); uploadedImg = new byte[(int) fileLen]; FileInputStream inputStream = new FileInputStream(photoUploadPath); int nRead = 0; while ((nRead = inputStream.read(uploadedImg)) != -1) { System.out.println("!!!!!!!!!!!!!!!!!" + new String(uploadedImg)); } inputStream.close(); } catch (Exception e2) { // TODO: handle exception } BufferedInputStream inputStreamReader = new BufferedInputStream(new ByteArrayInputStream(uploadedImg)); ImageData imageData = new ImageData(inputStreamReader); Image image = new Image(Display.getCurrent(), imageData); photoLabel.setImage(image); 

如果它是RCP应用程序,我会选择可扩展的解决方案。

创建一个ImageCache对象,您可以在应用程序生命周期的开始实例化该对象(最好是在应用程序的Activator类中)。

这个ImageCache可以从相对于插件的路径获取图像(当然也可以缓存它们)(例如插件有一个文件夹icons ;然后,当你需要一个图标时,你只需要调用Activator.getDefault().getImage("icons/random.png"); – 其中getDefault()Activator单例实例)。

ImageCache有两个:

 public ImageDescriptor getImageDescriptor(final String path) { ImageDescriptor imgD = AbstractUIPlugin.imageDescriptorFromPlugin(PLUGIN_ID, path); if (imgD == null) { return null; // OR a "missing icon", eg a red flag } } 

 public Image getImage(final String path) { Image image = imageCacheMap.get(path); if (image == null) { image = getImageDescripto(path).createImage(); imageCacheMap.put(path, image); } return image; } 

由于需要处理这些图像,因此在ImageCache有一个dispose() stop()方法,该方法在Activatorstop()方法中调用。

有很多方法可以解决这个问题。 在我看来,这是RCP应用程序中最好的一个。

Java SWT加载和调整图像以动态查看或编辑

![在此处输入图像说明


按钮单击以打开FileDialog Box并选择要在特定标签上显示的任何图像。

ImageLoader类用于从文件或流中加载图像并将图像保存到文件或流中

ImageData类是与设备无关的图像描述

SWT的Image类可用于在GUI中显示图像

 package rcp_demo.Editor; import org.eclipse.swt.widgets.FileDialog; import org.eclipse.swt.custom.CLabel; import org.eclipse.swt.graphics.Image; import org.eclipse.swt.graphics.ImageData; import org.eclipse.swt.graphics.ImageLoader; public class ProductEditor extends EditorPart { public static final String ID="rcp_demo.Editor.product"; private Text text; private CLabel lbl_image_text; private static final String[] FILTER_NAMES = { "Images(*.jpg)","Images(*.jpeg)","Images(*.png)","All Files (*.*)"}; // These filter extensions are used to filter which files are displayed. private static final String[] FILTER_EXTS = { "*.jpg", "*.jpeg", "*.png", "*.*"}; public void createPartControl(final Composite parent) { parent.setLayout(null); //Layout with absolute positioning components. text = new Text(parent, SWT.BORDER); text.setBounds(25, 57, 169, 19); Button btnOpen = new Button(parent, SWT.NONE); btnOpen.setText("open"); btnOpen.addSelectionListener(new SelectionAdapter() { @Override public void widgetSelected(SelectionEvent e) { FileDialog dialog = new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.OPEN); dialog.setFilterNames(FILTER_NAMES); dialog.setFilterExtensions(FILTER_EXTS); String result = dialog.open(); if(result!=null) { text.setText(result); Image image=SWTResourceManager.getImage(result); ImageData imgData = image.getImageData(); imgData=imgData.scaledTo(200, 200); ImageLoader imageLoader = new ImageLoader(); imageLoader.data = new ImageData[] {imgData}; imageLoader.save(result, SWT.IMAGE_COPY); System.out.println(imgData.width+"....."+imgData.height); lbl_image_text.setBounds(25,88,imgData.width+10,imgData.height+10); //Image size set to Label //lbl_image_text.setBounds(25,88,image.getBounds().width+10,image.getBounds().height+10); lbl_image_text.setImage(SWTResourceManager.getImage(result)); } } }); btnOpen.setText("open"); lbl_image_text = new CLabel(parent, SWT.Resize); } } 

CLabel类提供了Label类的一些高级function。 该类可以同时显示其文本标签和图像标签。

  lbl_image_text.setText("Welcome"); lbl_image_text.setImage(SWTResourceManager.getImage("Image Path"));