org.eclipse.swt.graphics.ImageLoader Java Examples

The following examples show how to use org.eclipse.swt.graphics.ImageLoader. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: XYGraphToolbar.java    From nebula with Eclipse Public License 2.0 6 votes vote down vote up
private void addSnapshotButton() {
	Button snapShotButton = new Button(XYGraphMediaFactory.getInstance().getImage("images/camera.png"));
	snapShotButton.setToolTip(new Label("Save Snapshot to PNG file"));
	addButton(snapShotButton);
	snapShotButton.addActionListener(new ActionListener() {
		public void actionPerformed(ActionEvent event) {
			// Have valid name, so get image
			ImageLoader loader = new ImageLoader();
			Image image = xyGraph.getImage();
			loader.data = new ImageData[] { image.getImageData() };
			image.dispose();
			// Prompt for file name
			String path = SingleSourceHelper2.getImageSavePath();
			if (path == null || path.length() <= 0)
				return;
			// Assert *.png at end of file name
			if (!path.toLowerCase().endsWith(".png"))
				path = path + ".png";
			// Save
			loader.save(path, SWT.IMAGE_PNG);
		}
	});
}
 
Example #2
Source File: ExportToImageManager.java    From ermaster-b with Apache License 2.0 6 votes vote down vote up
private void writePNGorGIF(Image image, String saveFilePath,
		String formatName) throws IOException, InterruptedException {

	try {
		ImageLoader loader = new ImageLoader();
		loader.data = new ImageData[] { image.getImageData() };
		loader.save(saveFilePath, format);

	} catch (SWTException e) {
		// Eclipse 3.2 �ł́A PNG �� Unsupported or unrecognized format �ƂȂ邽�߁A
		// �ȉ��̑�֕��@���g�p����
		// �������A���̕��@�ł͏�肭�o�͂ł��Ȃ��‹�����

		e.printStackTrace();
		BufferedImage bufferedImage = new BufferedImage(
				image.getBounds().width, image.getBounds().height,
				BufferedImage.TYPE_INT_RGB);

		drawAtBufferedImage(bufferedImage, image, 0, 0);

		ImageIO.write(bufferedImage, formatName, new File(saveFilePath));
	}
}
 
Example #3
Source File: SaveImageAction.java    From olca-app with Mozilla Public License 2.0 6 votes vote down vote up
@Override
public void run() {
	if (file == null)
		return;
	log.trace("export product graph as image: {}", file);
	ScalableRootEditPart editPart = (ScalableRootEditPart) editor.getGraphicalViewer().getRootEditPart();
	IFigure rootFigure = editPart.getLayer(LayerConstants.PRINTABLE_LAYERS);
	Rectangle bounds = rootFigure.getBounds();
	Image img = new Image(null, bounds.width, bounds.height);
	GC imageGC = new GC(img);
	Graphics graphics = new SWTGraphics(imageGC);
	rootFigure.paint(graphics);
	ImageLoader imgLoader = new ImageLoader();
	imgLoader.data = new ImageData[] { img.getImageData() };
	imgLoader.save(file.getAbsolutePath(), SWT.IMAGE_PNG);
}
 
Example #4
Source File: ComponentStatusLabel.java    From arx with Apache License 2.0 6 votes vote down vote up
/**
 * Sets the GIF
 *
 * @param inputStream
 */
public void setGIF(InputStream inputStream) {
    checkWidget();
    if (thread != null) {
        thread.stop();
        this.getDisplay().timerExec(-1, thread);
    }

    ImageLoader loader = new ImageLoader();

    try {
        loader.load(inputStream);
    } catch (Exception e) {
        this.image = null;
        return;
    }

    if (loader.data[0] != null) this.image = new Image(this.getDisplay(), loader.data[0]);

    if (loader.data.length > 1) {
        thread = new ComponentStatusLabelGIFHandler(this, loader);
        thread.run();
    }

    redraw();
}
 
Example #5
Source File: AbstractGraphicalContentProvider.java    From eclipsegraphviz with Eclipse Public License 1.0 6 votes vote down vote up
/**
 * {@inheritDoc}
 * 
 * This default implementation relies on the
 * {@link #loadImage(Display, Point, Object)} method, subclasses are
 * encouraged to provide a more efficient implementation.
 */
public void saveImage(Display display, Point suggestedSize, Object input, IPath location, GraphicFileFormat fileFormat)
        throws CoreException {
	int swtFileFormat = getSWTFileFormat(fileFormat);
    Image toSave = loadImage(Display.getDefault(), new Point(0, 0), input);
    try {
        ImageLoader imageLoader = new ImageLoader();
        imageLoader.data = new ImageData[] { toSave.getImageData() };
        ByteArrayOutputStream buffer = new ByteArrayOutputStream(200 * 1024);
        imageLoader.save(buffer, swtFileFormat);
        try {
            FileUtils.writeByteArrayToFile(location.toFile(), buffer.toByteArray());
        } catch (IOException e) {
            throw new CoreException(new Status(IStatus.ERROR, Activator.PLUGIN_ID, "Error saving image", e));
        }
    } finally {
        toSave.dispose();
    }
}
 
Example #6
Source File: ImageWriter.java    From tuxguitar with GNU Lesser General Public License v2.1 6 votes vote down vote up
public static void write(ImageFormat format, String path, List<UIImage> pages) throws TGFileFormatException {
	try {
		for(int i = 0; i < pages.size() ; i ++ ) {
			OutputStream stream = new FileOutputStream(new File(path + File.separator + "page-" + i + format.getExtension() ));
			
			Image image = ((SWTImage) pages.get(i)).getHandle();
			ImageLoader imageLoader = new ImageLoader();
			imageLoader.data = new ImageData[] { image.getImageData() };
			imageLoader.save(stream, format.getFormat() );
			
			stream.flush();
			stream.close();
		}
	} catch (Throwable throwable) {
		throw new TGFileFormatException("Could not write song!.",throwable);
	}
}
 
Example #7
Source File: ExportImageAction.java    From gef with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void run(IAction action) {
	FileDialog dialog = new FileDialog(getShell(), SWT.SAVE);
	dialog.setFileName("Cloud.png");
	dialog.setText("Export PNG image to...");
	String destFile = dialog.open();
	if (destFile == null)
		return;
	File f = new File(destFile);
	if (f.exists()) {
		boolean confirmed = MessageDialog.openConfirm(getShell(), "File already exists",
				"The file '" + f.getName() + "' does already exist. Do you want to override it?");
		if (!confirmed)
			return;
	}
	ImageLoader il = new ImageLoader();
	try {
		il.data = new ImageData[] { getViewer().getCloud().getImageData() };
		il.save(destFile, SWT.IMAGE_PNG);
	} catch (Exception e) {
		e.printStackTrace();
	}
}
 
Example #8
Source File: ExportToFileAction.java    From xtext-eclipse with Eclipse Public License 2.0 6 votes vote down vote up
@Override
public void run() {
	IFigure contents = railroadView.getContents();
	if (contents != null) {
		FileDialog fileDialog = new FileDialog(this.railroadView.getSite().getShell(), SWT.SAVE);
		fileDialog.setFilterExtensions(new String[] { "*.png" });
		fileDialog.setText("Choose diagram file");
		String fileName = fileDialog.open();
		if (fileName == null) {
			return;
		}
		Dimension preferredSize = contents.getPreferredSize();
		Image image = new Image(Display.getDefault(), preferredSize.width + 2 * PADDING, preferredSize.height + 2
				* PADDING);
		GC gc = new GC(image);
		SWTGraphics graphics = new SWTGraphics(gc);
		graphics.translate(PADDING, PADDING);
		graphics.translate(contents.getBounds().getLocation().getNegated());
		contents.paint(graphics);
		ImageData imageData = image.getImageData();
		ImageLoader imageLoader = new ImageLoader();
		imageLoader.data = new ImageData[] { imageData };
		imageLoader.save(fileName, SWT.IMAGE_PNG);
	}
}
 
Example #9
Source File: Avatar.java    From jdt-codemining with Eclipse Public License 1.0 6 votes vote down vote up
public ImageData getData() {
	if (this.data != null)
		return this.data;

	try {
		ImageData[] images = new ImageLoader()
				.load(new ByteArrayInputStream(byteArray));
		if (images.length > 0)
			this.data = images[0];
		else
			this.data = ImageDescriptor.getMissingImageDescriptor()
					.getImageData();
	} catch (SWTException exception) {
		this.data = ImageDescriptor.getMissingImageDescriptor()
				.getImageData();
	}
	return this.data;
}
 
Example #10
Source File: SwtImage.java    From AppleCommander with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Save the image.
 */
public void save(OutputStream outputStream) throws IOException {
	ImageLoader imageLoader = new ImageLoader();
	imageLoader.data = new ImageData[] { imageData };
	int format = SWT.IMAGE_PNG;
	if ("BMP".equals(getFileExtension())) { //$NON-NLS-1$
		format = SWT.IMAGE_BMP;
	} else if ("RLE".equals(getFileExtension())) { //$NON-NLS-1$
		format = SWT.IMAGE_BMP_RLE;
	} else if ("GIF".equals(getFileExtension())) { //$NON-NLS-1$
		format = SWT.IMAGE_GIF;
	} else if ("ICO".equals(getFileExtension())) { //$NON-NLS-1$
		format = SWT.IMAGE_ICO;
	} else if ("JPEG".equals(getFileExtension())) { //$NON-NLS-1$
		format = SWT.IMAGE_JPEG;
	} else if ("PNG".equals(getFileExtension())) { //$NON-NLS-1$
		format = SWT.IMAGE_PNG;
	}
	imageLoader.save(outputStream, format);
}
 
Example #11
Source File: URLImageLabel.java    From neoscada with Eclipse Public License 1.0 6 votes vote down vote up
private void processLoad ( final String stringUrl ) throws Exception
{
    final ImageLoader loader = new ImageLoader ();

    final URL url = new URL ( stringUrl );

    final ImageData[] data;
    try ( InputStream is = url.openStream () )
    {
        data = loader.load ( is );
    }

    logger.debug ( "Image loaded" );

    Display.getDefault ().asyncExec ( new Runnable () {
        @Override
        public void run ()
        {
            showImage ( stringUrl, data );
        }
    } );
}
 
Example #12
Source File: ExportToImageManager.java    From erflute with Apache License 2.0 5 votes vote down vote up
private void writeJPGorBMP(Image image, String saveFilePath, int format) throws IOException {
    final ImageData[] imgData = new ImageData[1];
    imgData[0] = image.getImageData();

    final ImageLoader imgLoader = new ImageLoader();
    imgLoader.data = imgData;
    imgLoader.save(saveFilePath, format);
}
 
Example #13
Source File: ExportToImageManager.java    From ermaster-b with Apache License 2.0 5 votes vote down vote up
private void writeJPGorBMP(Image image, String saveFilePath, int format)
		throws IOException {
	ImageData[] imgData = new ImageData[1];
	imgData[0] = image.getImageData();

	ImageLoader imgLoader = new ImageLoader();
	imgLoader.data = imgData;
	imgLoader.save(saveFilePath, format);
}
 
Example #14
Source File: KettleXulLoader.java    From pentaho-kettle with Apache License 2.0 5 votes vote down vote up
@Override
public InputStream getResourceAsStream( String resource ) {
  int height = iconHeight;
  int width = iconWidth;
  if ( resource.contains( ":" ) ) {
    // we have height/width overrides
    width = Integer.parseInt( resource.substring( resource.indexOf( ":" ) + 1, resource.indexOf( "#" ) ) );
    height = Integer.parseInt( resource.substring( resource.indexOf( "#" ) + 1, resource.indexOf( "." ) ) );
    resource = resource.substring( 0, resource.indexOf( ":" ) ) + resource.substring( resource.indexOf( "." ) );
  }
  if ( SvgSupport.isSvgEnabled() && ( SvgSupport.isSvgName( resource ) || SvgSupport.isPngName( resource ) ) ) {
    InputStream in = null;
    try {
      in = super.getResourceAsStream( SvgSupport.toSvgName( resource ) );
      // load SVG
      SvgImage svg = SvgSupport.loadSvgImage( in );
      SwtUniversalImage image = new SwtUniversalImageSvg( svg );

      Display d = Display.getCurrent() != null ? Display.getCurrent() : Display.getDefault();
      // write to png
      Image result = image.getAsBitmapForSize( d, width, height );
      ImageLoader loader = new ImageLoader();
      loader.data = new ImageData[] { result.getImageData() };
      ByteArrayOutputStream out = new ByteArrayOutputStream();
      loader.save( out, SWT.IMAGE_PNG );

      image.dispose();

      return new ByteArrayInputStream( out.toByteArray() );
    } catch ( Throwable ignored ) {
      // any exception will result in falling back to PNG
    } finally {
      IOUtils.closeQuietly( in );
    }
    resource = SvgSupport.toPngName( resource );
  }
  return super.getResourceAsStream( resource );
}
 
Example #15
Source File: SankeyImageAction.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
@Override
public void run() {
	try {
		ImageLoader imgLoader = new ImageLoader();
		imgLoader.data = new ImageData[] { image.getImageData() };
		imgLoader.save(file.getAbsolutePath(), SWT.IMAGE_PNG);
	} catch (Exception e) {
		log.error("Failed to write image", e);
	}
}
 
Example #16
Source File: ImageExportAction.java    From olca-app with Mozilla Public License 2.0 5 votes vote down vote up
private void writeToFile(Image image) {
	File file = FileChooser.forExport("*.png", "openlca_chart.png");
	if (file == null)
		return;
	log.trace("Export image to {}", file);
	ImageLoader loader = new ImageLoader();
	loader.data = new ImageData[] { image.getImageData() };
	loader.save(file.getAbsolutePath(), SWT.IMAGE_PNG);
}
 
Example #17
Source File: UiDBImage.java    From elexis-3-core with Eclipse Public License 1.0 5 votes vote down vote up
public UiDBImage(String prefix, final String name, final InputStream source){
	this.dbImage = new DBImage(prefix, name);
	try {
		ImageLoader iml = new ImageLoader();
		iml.load(source);
		ByteArrayOutputStream baos = new ByteArrayOutputStream();
		iml.save(baos, SWT.IMAGE_PNG);
		dbImage.setBinary(DBImage.FLD_IMAGE, baos.toByteArray());
	} catch (IllegalArgumentException | SWTException e) {
		log.error("Error setting image object on DBImage " + dbImage.getLabel(), e);
	}
}
 
Example #18
Source File: ImageCaptureExample.java    From nebula with Eclipse Public License 2.0 5 votes vote down vote up
/**
 * Demonstrate capturing the pages of a print to in-memory images.
 * 
 * @param args
 *            command-line arguments (ignored)
 */
public static void main(String[] args) {
	Display display = new Display();
	Point displayDPI = display.getDPI();
	display.dispose();

	Printer printer = new Printer(new PrinterData());
	Point printerDPI = printer.getDPI();

	try {
		PrintJob job = new PrintJob("ImageCapture.java", createPrint());

		PrintPiece[] pages = PaperClips.getPages(job, printer);

		ImageLoader imageLoader = new ImageLoader();
		for (int i = 0; i < pages.length; i++) {
			PrintPiece page = pages[i];
			Point pageSize = page.getSize();
			pageSize.x = pageSize.x * displayDPI.x / printerDPI.x;
			pageSize.y = pageSize.y * displayDPI.y / printerDPI.y;
			ImageData pageImage = captureImageData(printer, page, pageSize);

			// Do something with the image
			pageImage.scanlinePad = 1;

			imageLoader.data = new ImageData[] { pageImage };
			imageLoader.save(getImageName(i), SWT.IMAGE_JPEG);
		}

	} finally {
		printer.dispose();
	}
}
 
Example #19
Source File: ComponentStatusLabelGIFHandler.java    From arx with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance
 * @param loader
 * @param label
 */
public ComponentStatusLabelGIFHandler(ComponentStatusLabel label, ImageLoader loader) {
    this.statusLabel = label;
    this.loader = loader;
    label.getDisplay().addListener(SWT.Dispose, new Listener() {
        public void handleEvent(Event arg0) {
            disposeCurrentImage();
        }
    });
}
 
Example #20
Source File: ChartLivePreviewThread.java    From birt with Eclipse Public License 1.0 5 votes vote down vote up
PreviewTimerTask( String taskName, Shell parentShell ) throws IOException
{
	this.taskName = taskName;
	this.parentShell = parentShell;
	if ( imageDatas == null )
	{
		loader = new ImageLoader();
		imageDatas = loader.load( UIHelper.getURL( "icons/obj16/progress_animation.gif" ).openStream( ) ); //$NON-NLS-1$
	}
}
 
Example #21
Source File: ImagesOnFileSystemRegistry.java    From Eclipse-Postfix-Code-Completion with Eclipse Public License 1.0 5 votes vote down vote up
public URL getImageURL(ImageDescriptor descriptor) {
	if (fTempDir == null)
		return null;

	URL url= fURLMap.get(descriptor);
	if (url != null)
		return url;

	File imageFile= getNewFile();
	ImageData imageData= descriptor.getImageData();
	if (imageData == null) {
		return null;
	}

	ImageLoader loader= new ImageLoader();
	loader.data= new ImageData[] { imageData };
	loader.save(imageFile.getAbsolutePath(), SWT.IMAGE_PNG);

	try {
		url= imageFile.toURI().toURL();
		fURLMap.put(descriptor, url);
		return url;
	} catch (MalformedURLException e) {
		JavaPlugin.log(e);
	}
	return null;
}
 
Example #22
Source File: ExportToImageManager.java    From erflute with Apache License 2.0 5 votes vote down vote up
private void writePNGorGIF(Image image, String saveFilePath, String formatName) throws IOException, InterruptedException {
    try {
        final ImageLoader loader = new ImageLoader();
        loader.data = new ImageData[] { image.getImageData() };
        loader.save(saveFilePath, format);
    } catch (final SWTException e) {
        e.printStackTrace();
        final BufferedImage bufferedImage =
                new BufferedImage(image.getBounds().width, image.getBounds().height, BufferedImage.TYPE_INT_RGB);
        drawAtBufferedImage(bufferedImage, image, 0, 0);
        ImageIO.write(bufferedImage, formatName, new File(saveFilePath));
    }
}
 
Example #23
Source File: ImageWriter.java    From logbook with MIT License 5 votes vote down vote up
/**
 * 指定されたimageを書き込みます。
 *
 * @param image
 *            書き込むImage
 * @throws IOException
 */
public void write(Image image) throws IOException {
    try (OutputStream out = new BufferedOutputStream(Files.newOutputStream(this.path, this.options))) {
        ImageLoader il = new ImageLoader();
        il.data = new ImageData[] { image.getImageData() };
        il.compression = this.compression;
        il.save(out, this.format);
    }
}
 
Example #24
Source File: AnimatedGIFRunner.java    From http4e with Apache License 2.0 5 votes vote down vote up
AnimatedGIFRunner(
      Control control, 
      ImageLoader imageLoaderGIF, 
      final Image backgroundImage){
   
   this.control = control;
   controlGC = new GC(control);
   shellBackground = control.getBackground();
   this.imageLoader = imageLoaderGIF;
   this.imageDataArray = imageLoader.data;
   if(imageDataArray == null || imageDataArray.length < 2){
      throw new RuntimeException("Illegal ImageLoader.");
   }
   isStopped = false;
   this.backgroundImage = backgroundImage;
   Rectangle ctrR = control.getBounds();
   positionX = (ctrR.width - CoreConstants.IMAGES_HEIGHT)/2;
   positionY = (ctrR.height - CoreConstants.IMAGES_HEIGHT)/2;

   control.addPaintListener(new PaintListener() {
      public void paintControl( PaintEvent e){
         GC graphics = e.gc;
         if(!graphics.isDisposed()) {
            graphics.drawImage(backgroundImage, positionX, positionY);
            graphics.dispose();
         }
      }
   });
}
 
Example #25
Source File: Animator.java    From http4e with Apache License 2.0 5 votes vote down vote up
void start(){
   if(gifRunner != null) gifRunner.stop();
   try {
      ImageLoader imageLoader = new ImageLoader();
      imageLoader.load( ResourceUtils.getBundleResourceStream(CoreConstants.PLUGIN_CORE, CoreImages.LOADING_ON));
      gifRunner = new AnimatedGIFRunner(parent, imageLoader, backgroundImage);
   } catch (Exception e) {
      throw CoreException.getInstance(CoreException.GENERAL, e);
   }
   final Thread animeThread = new Thread(gifRunner);
   animeThread.setDaemon(true);
   animeThread.start();
}
 
Example #26
Source File: MImageSWTImpl.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Gets the {@link BufferedImage} from the given {@link MImage}.
 * 
 * @param image
 *            the {@link MImage}
 * @return the {@link BufferedImage} from the given {@link MImage}
 * @throws IOException
 *             if the image can't be read
 */
public static ImageData getImage(MImage image) throws IOException {
    final ImageData res;

    final ImageLoader imageLoader = new ImageLoader();
    if (image instanceof MImageSWTImpl) {
        res = ((MImageSWTImpl) image).image;
    } else {
        try (InputStream input = image.getInputStream()) {
            res = imageLoader.load(input)[0];
        }
    }

    return res;
}
 
Example #27
Source File: CloudioUiBundle.java    From gef with Eclipse Public License 2.0 5 votes vote down vote up
public void start(BundleContext context) throws Exception {
	super.start(context);
	plugin = this;
	ImageLoader il = new ImageLoader();
	loadImage(il, ADD);
	loadImage(il, REMOVE);
	loadImage(il, TOGGLE_COLORS);
}
 
Example #28
Source File: MImageSWTImpl.java    From M2Doc with Eclipse Public License 1.0 5 votes vote down vote up
@Override
public InputStream getInputStream() throws IOException {
    try (ByteArrayOutputStream output = new ByteArrayOutputStream();) {
        final ImageLoader imageLoader = new ImageLoader();
        imageLoader.data = new ImageData[] {image, };
        imageLoader.save(output, SWT.IMAGE_PNG);
        return new ByteArrayInputStream(output.toByteArray());
    }
}
 
Example #29
Source File: ImageInfo.java    From ermasterr with Apache License 2.0 5 votes vote down vote up
public void toImageData() throws InterruptedException, IOException {
    final ByteArrayOutputStream out = new ByteArrayOutputStream();

    // try {
    format = SWT.IMAGE_PNG;
    //
    final ImageLoader imgLoader = new ImageLoader();
    imgLoader.data = new ImageData[] {image.getImageData()};
    imgLoader.save(out, format);
    //
    // } catch (SWTException e) {
    // if (this.format == SWT.IMAGE_PNG) {
    // BufferedImage bufferedImage = new BufferedImage(
    // image.getBounds().width, image.getBounds().height,
    // BufferedImage.TYPE_INT_RGB);
    //
    // ImageUtils.drawAtBufferedImage(bufferedImage, image, 0, 0);

    // BufferedImage bufferedImage =
    // ImageUtils.convertToBufferedImage(image);
    //
    // String formatName = "png";
    //
    // ImageIO.write(bufferedImage, formatName, out);

    // } else {
    // throw e;
    // }
    // }

    imageData = out.toByteArray();
}
 
Example #30
Source File: ImageDataStorage.java    From eclipse-extras with Eclipse Public License 1.0 5 votes vote down vote up
private byte[] getImageDataBytes( int imageFormat ) {
  ImageLoader imageLoader = new ImageLoader();
  imageLoader.data = imageDatas;
  ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
  imageLoader.save( outputStream, imageFormat );
  return outputStream.toByteArray();
}