【问题标题】:How to load image to view in RCP?如何加载图像以在 RCP 中查看?
【发布时间】:2014-07-08 06:43:31
【问题描述】:

我正在开发一个包含某些视图的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 是包含上传照片路径的字符串变量。 我该如何解决这个问题?

【问题讨论】:

  • 路径是绝对路径吗? SWTResourceManager.getImage(...) 是返回图片还是 null?
  • 是的。 SWTResourceManager.getImage(...) 返回类似 Image {140719108178432} 的图像
  • CLabel 类可能会对您有所帮助。它可以设置文本和图像。几年前,我在我的项目中完美地使用了它。
  • SWTResourceManager.getImage 如果找不到图像,则返回红色方形图像。对于这种形式的 getImage 调用,图像路径必须相对于 FormDataViewClass。

标签: java swt rcp


【解决方案1】:

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

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

【讨论】:

  • 当您不再需要它时,请记住dispose() Image。否则你会泄露系统句柄。
【解决方案2】:

如果它是一个 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", e.g. 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(),该方法在Activatorstop() 方法中调用。

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

【讨论】:

    【解决方案3】:

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


    按钮单击打开文件对话框并选择要在特定标签上显示的任何图像。

    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 类提供了一些高级功能。 这个类可以同时显示它的文本标签和图片标签。

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

    【讨论】:

      猜你喜欢
      • 2019-03-10
      • 2023-03-16
      • 1970-01-01
      • 2013-04-04
      • 1970-01-01
      • 1970-01-01
      • 2012-11-10
      • 2020-07-13
      • 2013-09-08
      相关资源
      最近更新 更多