How to upload an image for viewing in RCP?

I am developing an RCP plugin project that includes specific views. details of employees, such as name , address , etc. It is possible to upload an employee image using the browse button. The second view shows the details that were introduced in the first view. All data except the photo is displayed in order.

It shows a red square instead of a photo tag. My code for setting the photo is shown as follows:

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

where photoUploadPath is a string variable containing the path to the loaded photo. How can I solve this problem?

0
source share
3 answers

The following code segment helped me solve the above problem.

 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); 
+1
source

If this is an RCP application, I would go with a scalable solution.

Create an ImageCache object that you create at the beginning of the application life cycle (preferably in the Activator class of the application).

This ImageCache can receive images (and, of course, cache them) from the path relative to the plugin (for example, the plugin has an icons folder, and then when you need an icon, you simply call Activator.getDefault().getImage("icons/random.png"); - where getDefault() is an instance of Activator singleton).

There are two of them in 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 } } 

and

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

Since these images need to be removed, use the dispose() method in ImageCache , which is called in the stop() method of the Activator .

There are many approaches to this. In my opinion, this is the best option for RCP applications.

0
source

Java SWT Download and resize images for viewing or editing with dynamic

! [enter image description here


Click the button to open the FileDialog window and select any image to display on a specific label.

The ImageLoader class is used to load images and save images to a file or stream.

ImageData strong> class - device-independent image descriptions

The SWT Image class can be used to display images in a graphical interface.

 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 class provides some additional features Label class. This class can display both a text label and an image label.

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

Source: https://habr.com/ru/post/916890/


All Articles