List of usage examples for javafx.scene.image WritableImage WritableImage
public WritableImage(@NamedArg("width") int width, @NamedArg("height") int height)
From source file:Main.java
/** * Snapshots the specified {@link BufferedImage} and stores a copy of * its pixels into a JavaFX {@link Image} object, creating a new * object if needed./* w ww . j a v a2 s . c om*/ * The returned {@code Image} will be a static snapshot of the state * of the pixels in the {@code BufferedImage} at the time the method * completes. Further changes to the {@code BufferedImage} will not * be reflected in the {@code Image}. * <p> * The optional JavaFX {@link WritableImage} parameter may be reused * to store the copy of the pixels. * A new {@code Image} will be created if the supplied object is null, * is too small or of a type which the image pixels cannot be easily * converted into. * * @param bimg the {@code BufferedImage} object to be converted * @param wimg an optional {@code WritableImage} object that can be * used to store the returned pixel data * @return an {@code Image} object representing a snapshot of the * current pixels in the {@code BufferedImage}. * @since JavaFX 2.2 */ public static WritableImage toFXImage(BufferedImage bimg, WritableImage wimg) { int bw = bimg.getWidth(); int bh = bimg.getHeight(); switch (bimg.getType()) { case BufferedImage.TYPE_INT_ARGB: case BufferedImage.TYPE_INT_ARGB_PRE: break; default: BufferedImage converted = new BufferedImage(bw, bh, BufferedImage.TYPE_INT_ARGB_PRE); Graphics2D g2d = converted.createGraphics(); g2d.drawImage(bimg, 0, 0, null); g2d.dispose(); bimg = converted; break; } // assert(bimg.getType == TYPE_INT_ARGB[_PRE]); if (wimg != null) { int iw = (int) wimg.getWidth(); int ih = (int) wimg.getHeight(); if (iw < bw || ih < bh) { wimg = null; } else if (bw < iw || bh < ih) { int empty[] = new int[iw]; PixelWriter pw = wimg.getPixelWriter(); PixelFormat<IntBuffer> pf = PixelFormat.getIntArgbPreInstance(); if (bw < iw) { pw.setPixels(bw, 0, iw - bw, bh, pf, empty, 0, 0); } if (bh < ih) { pw.setPixels(0, bh, iw, ih - bh, pf, empty, 0, 0); } } } if (wimg == null) { wimg = new WritableImage(bw, bh); } PixelWriter pw = wimg.getPixelWriter(); IntegerComponentRaster icr = (IntegerComponentRaster) bimg.getRaster(); int data[] = icr.getDataStorage(); int offset = icr.getDataOffset(0); int scan = icr.getScanlineStride(); PixelFormat<IntBuffer> pf = (bimg.isAlphaPremultiplied() ? PixelFormat.getIntArgbPreInstance() : PixelFormat.getIntArgbInstance()); pw.setPixels(0, 0, bw, bh, pf, data, offset, scan); return wimg; }
From source file:io.github.bluemarlin.util.ImageCache.java
public Image get(String key) { Image image = new WritableImage(50, 50); if (StringUtils.isNotBlank(key)) { try {//from w ww .j a va 2 s .c o m image = imageCache.get(key); } catch (UncheckedExecutionException | ExecutionException e) { logger.warn("Exception in loading image: " + key + ". Returning default image.", e); } } else { // logger.warn("Key for image to load is empty. Returning default image."); } return image; }
From source file:io.github.mzmine.util.jfreechart.JFreeChartUtils.java
public static void exportToClipboard(ChartViewer chartNode) { final Clipboard clipboard = Clipboard.getSystemClipboard(); final ClipboardContent content = new ClipboardContent(); final int width = (int) chartNode.getWidth(); final int height = (int) chartNode.getHeight(); WritableImage img = new WritableImage(width, height); SnapshotParameters params = new SnapshotParameters(); chartNode.snapshot(params, img);/*from w w w .j av a2s. c o m*/ content.putImage(img); clipboard.setContent(content); }
From source file:net.rptools.tokentool.util.ImageUtil.java
public static Image resizeCanvas(Image imageSource, int newWidth, int newHeight, int offsetX, int offsetY) { int sourceWidth = (int) imageSource.getWidth(); int sourceHeight = (int) imageSource.getHeight(); // No work needed here... if (sourceWidth == newWidth && sourceHeight == newHeight) return imageSource; WritableImage outputImage = new WritableImage(newWidth, newHeight); PixelReader pixelReader = imageSource.getPixelReader(); PixelWriter pixelWriter = outputImage.getPixelWriter(); WritablePixelFormat<IntBuffer> format = WritablePixelFormat.getIntArgbInstance(); int[] buffer = new int[sourceWidth * sourceHeight]; pixelReader.getPixels(0, 0, sourceWidth, sourceHeight, format, buffer, 0, sourceWidth); pixelWriter.setPixels(offsetX, offsetY, sourceWidth, sourceHeight, format, buffer, 0, sourceWidth); return outputImage; }
From source file:net.rptools.tokentool.util.ImageUtil.java
private static Image clipImageWithMask(Image imageSource, Image imageMask) { int imageWidth = (int) imageMask.getWidth(); int imageHeight = (int) imageMask.getHeight(); WritableImage outputImage = new WritableImage(imageWidth, imageHeight); PixelReader pixelReader_Mask = imageMask.getPixelReader(); PixelReader pixelReader_Source = imageSource.getPixelReader(); PixelWriter pixelWriter = outputImage.getPixelWriter(); for (int readY = 0; readY < imageHeight; readY++) { for (int readX = 0; readX < imageWidth; readX++) { Color pixelColor = pixelReader_Mask.getColor(readX, readY); if (pixelColor.equals(Color.TRANSPARENT)) pixelWriter.setColor(readX, readY, pixelReader_Source.getColor(readX, readY)); }/*from www . j ava2s . com*/ } return outputImage; }
From source file:net.rptools.tokentool.util.ImageUtil.java
private static Image autoCropImage(Image imageSource) { ImageView croppedImageView = new ImageView(imageSource); PixelReader pixelReader = imageSource.getPixelReader(); int imageWidth = (int) imageSource.getWidth(); int imageHeight = (int) imageSource.getHeight(); int minX = imageWidth, minY = imageHeight, maxX = 0, maxY = 0; // Find the first and last pixels that are not transparent to create a bounding viewport for (int readY = 0; readY < imageHeight; readY++) { for (int readX = 0; readX < imageWidth; readX++) { Color pixelColor = pixelReader.getColor(readX, readY); if (!pixelColor.equals(Color.TRANSPARENT)) { if (readX < minX) minX = readX;// w w w. j a va2 s .c o m if (readX > maxX) maxX = readX; if (readY < minY) minY = readY; if (readY > maxY) maxY = readY; } } } if (maxX - minX <= 0 || maxY - minY <= 0) return new WritableImage(1, 1); // Create a viewport to clip the image using snapshot Rectangle2D viewPort = new Rectangle2D(minX, minY, maxX - minX, maxY - minY); SnapshotParameters parameter = new SnapshotParameters(); parameter.setViewport(viewPort); parameter.setFill(Color.TRANSPARENT); return croppedImageView.snapshot(parameter, null); }
From source file:net.rptools.tokentool.util.ImageUtil.java
public static Image composePreview(StackPane compositeTokenPane, Color bgColor, ImageView portraitImageView, ImageView maskImageView, ImageView overlayImageView, boolean useAsBase, boolean clipImage) { // Process layout as maskImage may have changed size if the overlay was changed compositeTokenPane.layout();/*from ww w. j av a2 s.co m*/ SnapshotParameters parameter = new SnapshotParameters(); Image finalImage = null; Group blend; if (clipImage) { // We need to clip the portrait image first then blend the overlay image over it // We will first get a snapshot of the portrait equal to the mask overlay image width/height double x, y, width, height; x = maskImageView.getParent().getLayoutX(); y = maskImageView.getParent().getLayoutY(); width = maskImageView.getFitWidth(); height = maskImageView.getFitHeight(); Rectangle2D viewPort = new Rectangle2D(x, y, width, height); Rectangle2D maskViewPort = new Rectangle2D(1, 1, width, height); WritableImage newImage = new WritableImage((int) width, (int) height); WritableImage newMaskImage = new WritableImage((int) width, (int) height); ImageView overlayCopyImageView = new ImageView(); ImageView clippedImageView = new ImageView(); parameter.setViewport(viewPort); parameter.setFill(bgColor); portraitImageView.snapshot(parameter, newImage); parameter.setViewport(maskViewPort); parameter.setFill(Color.TRANSPARENT); maskImageView.setVisible(true); maskImageView.snapshot(parameter, newMaskImage); maskImageView.setVisible(false); clippedImageView.setFitWidth(width); clippedImageView.setFitHeight(height); clippedImageView.setImage(clipImageWithMask(newImage, newMaskImage)); // Our masked portrait image is now stored in clippedImageView, lets now blend the overlay image over it // We'll create a temporary group to hold our temporary ImageViews's and blend them and take a snapshot overlayCopyImageView.setImage(overlayImageView.getImage()); overlayCopyImageView.setFitWidth(overlayImageView.getFitWidth()); overlayCopyImageView.setFitHeight(overlayImageView.getFitHeight()); overlayCopyImageView.setOpacity(overlayImageView.getOpacity()); if (useAsBase) { blend = new Group(overlayCopyImageView, clippedImageView); } else { blend = new Group(clippedImageView, overlayCopyImageView); } // Last, we'll clean up any excess transparent edges by cropping it finalImage = autoCropImage(blend.snapshot(parameter, null)); } else { parameter.setFill(Color.TRANSPARENT); finalImage = autoCropImage(compositeTokenPane.snapshot(parameter, null)); } return finalImage; }
From source file:eu.mihosoft.vrl.fxscad.MainController.java
@FXML private void onExportAsPngFile(ActionEvent e) { if (csgObject == null) { Action response = Dialogs.create().title("Error").message("Cannot export PNG. There is no geometry :(") .lightweight().showError(); return;// w ww .j a v a2s.co m } FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("Export PNG File"); fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Image files (*.png)", "*.png")); File f = fileChooser.showSaveDialog(null); if (f == null) { return; } String fName = f.getAbsolutePath(); if (!fName.toLowerCase().endsWith(".png")) { fName += ".png"; } int snWidth = 1024; int snHeight = 1024; double realWidth = viewGroup.getBoundsInLocal().getWidth(); double realHeight = viewGroup.getBoundsInLocal().getHeight(); double scaleX = snWidth / realWidth; double scaleY = snHeight / realHeight; double scale = Math.min(scaleX, scaleY); PerspectiveCamera snCam = new PerspectiveCamera(false); snCam.setTranslateZ(-200); SnapshotParameters snapshotParameters = new SnapshotParameters(); snapshotParameters.setTransform(new Scale(scale, scale)); snapshotParameters.setCamera(snCam); snapshotParameters.setDepthBuffer(true); snapshotParameters.setFill(Color.TRANSPARENT); WritableImage snapshot = new WritableImage(snWidth, (int) (realHeight * scale)); viewGroup.snapshot(snapshotParameters, snapshot); try { ImageIO.write(SwingFXUtils.fromFXImage(snapshot, null), "png", new File(fName)); } catch (IOException ex) { Logger.getLogger(MainController.class.getName()).log(Level.SEVERE, null, ex); } }
From source file:net.rptools.tokentool.util.ImageUtil.java
private static Image processMagenta(Image inputImage, int colorThreshold, boolean overlayWanted) { int imageWidth = (int) inputImage.getWidth(); int imageHeight = (int) inputImage.getHeight(); WritableImage outputImage = new WritableImage(imageWidth, imageHeight); PixelReader pixelReader = inputImage.getPixelReader(); PixelWriter pixelWriter = outputImage.getPixelWriter(); for (int readY = 0; readY < imageHeight; readY++) { for (int readX = 0; readX < imageWidth; readX++) { Color pixelColor = pixelReader.getColor(readX, readY); if (isMagenta(pixelColor, COLOR_THRESHOLD) == overlayWanted) pixelWriter.setColor(readX, readY, Color.TRANSPARENT); else//from ww w.j a v a 2s.c om pixelWriter.setColor(readX, readY, pixelColor); } } return outputImage; }
From source file:be.makercafe.apps.makerbench.editors.JFXMillEditor.java
private void handleExportAsPngFile(ActionEvent e) { if (millObject == null) { Alert alert = new Alert(AlertType.ERROR); alert.setTitle("Oeps an error occured"); alert.setHeaderText("Cannot export PNG. There is no geometry !"); alert.setContentText("Please verify that your code generates a valid CSG object."); alert.showAndWait();//w w w. j a v a 2 s .co m return; } FileChooser fileChooser = new FileChooser(); fileChooser.setTitle("Export PNG File"); fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("Image files (*.png)", "*.png")); File f = fileChooser.showSaveDialog(null); if (f == null) { return; } String fName = f.getAbsolutePath(); if (!fName.toLowerCase().endsWith(".png")) { fName += ".png"; } int snWidth = 1024; int snHeight = 1024; double realWidth = viewGroup.getBoundsInLocal().getWidth(); double realHeight = viewGroup.getBoundsInLocal().getHeight(); double scaleX = snWidth / realWidth; double scaleY = snHeight / realHeight; double scale = Math.min(scaleX, scaleY); PerspectiveCamera snCam = new PerspectiveCamera(false); snCam.setTranslateZ(-200); SnapshotParameters snapshotParameters = new SnapshotParameters(); snapshotParameters.setTransform(new Scale(scale, scale)); snapshotParameters.setCamera(snCam); snapshotParameters.setDepthBuffer(true); snapshotParameters.setFill(Color.TRANSPARENT); WritableImage snapshot = new WritableImage(snWidth, (int) (realHeight * scale)); viewGroup.snapshot(snapshotParameters, snapshot); try { ImageIO.write(SwingFXUtils.fromFXImage(snapshot, null), "png", new File(fName)); } catch (IOException ex) { Logger.getLogger(this.getClass().getName()).log(Level.SEVERE, null, ex); Alert alert = new Alert(AlertType.ERROR); alert.setTitle("Oeps an error occured"); alert.setHeaderText("Cannot export PNG. There went something wrong writing the file."); alert.setContentText( "Please verify that your file is not read only, is not locked by other user or program, you have enough diskspace."); alert.showAndWait(); } }