Java Code Examples for javafx.scene.image.Image#getWidth()

The following examples show how to use javafx.scene.image.Image#getWidth() . 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: ImageUtil.java    From Augendiagnose with GNU General Public License v2.0 7 votes vote down vote up
/**
 * Resize an image to the given size.
 *
 * @param baseImage The original image.
 * @param targetSize The target size.
 * @param allowGrowing flag indicating if the image is allowed to grow.
 * @return the resized image.
 */
public static Image resizeImage(final Image baseImage, final int targetSize, final boolean allowGrowing) {
	if (baseImage == null || baseImage.getWidth() == 0 || baseImage.getHeight() == 0) {
		return baseImage;
	}
	if (baseImage.getWidth() <= targetSize && baseImage.getHeight() <= targetSize && !allowGrowing) {
		return baseImage;
	}
	int targetWidth;
	int targetHeight;
	if (baseImage.getWidth() > baseImage.getHeight()) {
		targetWidth = targetSize;
		targetHeight = (int) (targetSize * baseImage.getHeight() / baseImage.getWidth());
	}
	else {
		targetWidth = (int) (targetSize * baseImage.getWidth() / baseImage.getHeight());
		targetHeight = targetSize;
	}

	Canvas canvas = new Canvas(targetWidth, targetHeight);
	canvas.getGraphicsContext2D().drawImage(baseImage, 0, 0, targetWidth, targetHeight);
	return canvas.snapshot(null, null);
}
 
Example 2
Source File: RectangularImageViewSkin.java    From WorkbenchFX with Apache License 2.0 6 votes vote down vote up
@Override
protected void layoutChildren(double contentX, double contentY, double contentWidth, double contentHeight) {
  Image currentImage = imageView.getImage();

  if (imageView.getImage() != null && currentImage.getWidth() > 0 &&
      currentImage.getHeight() > 0) {
    Dimension2D bestFit =
        shouldFitIn(currentImage.getWidth(), currentImage.getHeight(), getWidthToFit(),
            getHeightToFit());

    imageView.setFitWidth(bestFit.getWidth());
    imageView.setFitHeight(bestFit.getHeight());
    imageView.relocate(contentX + (contentWidth - bestFit.getWidth()) * 0.5,
        contentY + (contentHeight - bestFit.getHeight()) * 0.5);

    clip.relocate((bestFit.getWidth() - clip.getWidth()) * 0.5,
        (bestFit.getHeight() - clip.getHeight()) * 0.5);
  }
}
 
Example 3
Source File: ZoneSelector.java    From AnchorFX with GNU Lesser General Public License v3.0 6 votes vote down vote up
private void buildUI(Image image, double x, double y)
{    
    setPrefWidth(image.getWidth());
    setPrefHeight(image.getHeight());
    
    iconView = new ImageView(image);  
    setStyle("-fx-background-color:rgba(0,0,0,0);");
    
    iconCircle = new Circle(image.getWidth()/2+10);
    iconCircle.setCenterX(getPrefWidth() / 2);
    iconCircle.setCenterY(getPrefHeight() / 2);
    iconCircle.getStyleClass().add("dockzone-circle-selector");
    
    iconView.relocate((getPrefWidth()-image.getWidth()) / 2, (getPrefWidth()-image.getHeight()) / 2);
    
    getChildren().addAll(iconCircle,iconView);
    
    parent.getChildren().add(this);
    relocate(x, y); 
}
 
Example 4
Source File: PictureRepresentation.java    From phoebus with Eclipse Public License 1.0 6 votes vote down vote up
public static Dimension2D computeSize(final PictureWidget widget)
{
    final String imageFile = widget.propFile().getValue();

    try
    {
        final String filename = ModelResourceUtil.resolveResource(widget.getTopDisplayModel(), imageFile);
        final Image image = new Image(ModelResourceUtil.openResourceStream(filename));
        return new Dimension2D(image.getWidth(), image.getHeight());

    }
    catch (Exception ex)
    {
        return new Dimension2D(0.0, 0.0);
    }
}
 
Example 5
Source File: WriteFxImage.java    From chart-fx with Apache License 2.0 6 votes vote down vote up
public static PaletteQuantizer estimatePalette(final Image image, final boolean alpha, final int nColors) {
    if (image == null) {
        throw new IllegalArgumentException(IMAGE_MUST_NOT_BE_NULL);
    }
    // get meta info
    final PixelReader pr = image.getPixelReader();
    if (pr == null) {
        throw new IllegalStateException(IMAGE_PIXEL_READER_NOT_AVAILABLE);
    }
    final int w = (int) image.getWidth();
    final int h = (int) image.getHeight();

    PaletteQuantizerNeuQuant cuant = new PaletteQuantizerNeuQuant(w, h, (x, y) -> pr.getArgb(y, x));
    cuant.setParReserveAlphaColor(alpha);
    cuant.setParNcolors(nColors);
    cuant.run();

    return cuant;
}
 
Example 6
Source File: ImageScope.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public Rectangle(Image image) {
    this.image = image;
    this.scopeType = ScopeType.Rectangle;
    if (image != null) {
        rectangle = new DoubleRectangle(image.getWidth() / 4, image.getHeight() / 4,
                image.getWidth() * 3 / 4, image.getHeight() * 3 / 4);
    } else {
        rectangle = new DoubleRectangle();
    }
}
 
Example 7
Source File: ImageScope.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public CircleRed(Image image) {
    this.image = image;
    this.scopeType = ScopeType.Circle;
    this.colorScopeType = ColorScopeType.Red;
    if (image != null) {
        circle = new DoubleCircle(image.getWidth() / 2, image.getHeight() / 2,
                image.getHeight() / 4);
    } else {
        circle = new DoubleCircle();
    }
}
 
Example 8
Source File: ImageScope.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public EllipseBrightness(Image image) {
    this.image = image;
    this.scopeType = ScopeType.Ellipse;
    this.colorScopeType = ColorScopeType.Brightness;
    if (image != null) {
        rectangle = new DoubleRectangle(image.getWidth() / 4, image.getHeight() / 4,
                image.getWidth() * 3 / 4, image.getHeight() * 3 / 4);
        ellipse = new DoubleEllipse(rectangle);
    } else {
        rectangle = new DoubleRectangle();
        ellipse = new DoubleEllipse();
    }
}
 
Example 9
Source File: ImageScope.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public CircleHue(Image image) {
    this.image = image;
    this.scopeType = ScopeType.Circle;
    this.colorScopeType = ColorScopeType.Hue;
    if (image != null) {
        circle = new DoubleCircle(image.getWidth() / 2, image.getHeight() / 2,
                image.getHeight() / 4);
    } else {
        circle = new DoubleCircle();
    }
}
 
Example 10
Source File: ImageScope.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public RectangleRed(Image image) {
    this.image = image;
    this.scopeType = ScopeType.Rectangle;
    this.colorScopeType = ColorScopeType.Red;
    if (image != null) {
        rectangle = new DoubleRectangle(image.getWidth() / 4, image.getHeight() / 4,
                image.getWidth() * 3 / 4, image.getHeight() * 3 / 4);
    } else {
        rectangle = new DoubleRectangle();
    }
}
 
Example 11
Source File: OverlayImageView.java    From Augendiagnose with GNU General Public License v2.0 5 votes vote down vote up
/**
 * Redisplay. (Can be used to switch between non-thumbnail and thumbnail view.
 *
 * @param resolution
 *            Indicator of the resolution of the image.
 */
public final void redisplay(final Resolution resolution) {
	Image newImage = ImageUtil.getImageForDisplay(getEyePhoto(), mOverlayType, mOverlayColor,
			mBrightness, mContrast, mSaturation, mColorTemperature, resolution);
	if (resolution != mCurrentResolution) {
		multiplyZoomProperty(mCurrentImageWidth / newImage.getWidth());
		mCurrentImageWidth = newImage.getWidth();
		mCurrentResolution = resolution;
	}

	getImageView().setImage(newImage);
}
 
Example 12
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public static Image addShadowFx(Image image, int shadow, Color color) {
        try {
            if (image == null || shadow <= 0) {
                return null;
            }
            double imageWidth = image.getWidth(), imageHeight = image.getHeight();
            Group group = new Group();
            Scene s = new Scene(group);

            ImageView view = new ImageView(image);
            view.setPreserveRatio(true);
            view.setFitWidth(imageWidth);
            view.setFitHeight(imageHeight);

            DropShadow dropShadow = new DropShadow();
            dropShadow.setOffsetX(shadow);
            dropShadow.setOffsetY(shadow);
            dropShadow.setColor(color);
            view.setEffect(dropShadow);

            group.getChildren().add(view);

            SnapshotParameters parameters = new SnapshotParameters();
            parameters.setFill(Color.TRANSPARENT);
            WritableImage newImage = group.snapshot(parameters, null);
            return newImage;
        } catch (Exception e) {
//            logger.error(e.toString());
            return null;
        }

    }
 
Example 13
Source File: ImageSprite.java    From DeskChan with GNU Lesser General Public License v3.0 5 votes vote down vote up
public static boolean canRead(File path){
    try {
        Image image = new Image(new FileInputStream(path));
        return image.getHeight() > 0 && image.getWidth() > 0;
    } catch (Exception e){
        Main.log(e);
        return false;
    }
}
 
Example 14
Source File: ImageOCRController.java    From MyBox with Apache License 2.0 5 votes vote down vote up
protected void setPreprocessImage(Image image) {
    imageView.setImage(image);
    FxmlControl.paneSize(originalScrollPane, imageView);
    String s = (int) image.getWidth() + " x " + (int) image.getHeight();
    if (maskRectangleLine != null && maskRectangleLine.isVisible() && maskRectangleData != null) {
        s += "  " + message("SelectedSize") + ": "
                + (int) maskRectangleData.getWidth() + "x" + (int) maskRectangleData.getHeight();
    }
    imageLabel.setText(s);
    if (startCheck.isSelected()) {
        startAction();
    }
}
 
Example 15
Source File: ImageScope.java    From MyBox with Apache License 2.0 5 votes vote down vote up
public RectangleColor(Image image) {
    this.image = image;
    this.scopeType = ScopeType.Rectangle;
    this.colorScopeType = ColorScopeType.Color;
    if (image != null) {
        rectangle = new DoubleRectangle(image.getWidth() / 4, image.getHeight() / 4,
                image.getWidth() * 3 / 4, image.getHeight() * 3 / 4);
    } else {
        rectangle = new DoubleRectangle();
    }
}
 
Example 16
Source File: OverlayImageView.java    From Augendiagnose with GNU General Public License v2.0 4 votes vote down vote up
@Override
public final void setImage(final JpegMetadata metadata, final Image image) {
	super.setImage(metadata, image);
	mCurrentResolution = Resolution.NORMAL;
	mCurrentImageWidth = image.getWidth();
}
 
Example 17
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 4 votes vote down vote up
public static Image indicateSplitFx(Image image,
            List<Integer> rows, List<Integer> cols,
            Color lineColor, int lineWidth, boolean showSize) {
        try {
            if (rows == null || cols == null) {
                return image;
            }
            Group group = new Group();
            int width = (int) image.getWidth();
            int height = (int) image.getHeight();
            int row;
            for (int i = 0; i < rows.size(); ++i) {
                row = rows.get(i);
                if (row <= 0 || row >= height - 1) {
                    continue;
                }
                Line rowLine = new Line(0, row, width, row);
                rowLine.setStroke(lineColor);
                rowLine.setStrokeWidth(lineWidth);
                group.getChildren().add(rowLine);
            }
            int col;
            for (int i = 0; i < cols.size(); ++i) {
                col = cols.get(i);
                if (col <= 0 || col >= width - 1) {
                    continue;
                }
                Line colLine = new Line(col, 0, col, height);
                colLine.setStroke(lineColor);
                colLine.setStrokeWidth(lineWidth);
                group.getChildren().add(colLine);
            }

            if (showSize) {
                for (int i = 0; i < rows.size() - 1; ++i) {
                    int h = rows.get(i + 1) - rows.get(i) + 1;
                    for (int j = 0; j < cols.size() - 1; ++j) {
                        int w = cols.get(j + 1) - cols.get(j) + 1;
                        Text text = new Text();
                        text.setX(cols.get(j) + w / 3);
                        text.setY(rows.get(i) + h / 3);
                        text.setFill(lineColor);
                        text.setText(w + "x" + h);
                        text.setFont(new javafx.scene.text.Font(lineWidth * 3.0));
                        group.getChildren().add(text);
                    }
                }
            }

            Blend blend = new Blend(BlendMode.SRC_OVER);
            blend.setBottomInput(new ImageInput(image));
            group.setEffect(blend);

            SnapshotParameters parameters = new SnapshotParameters();
            parameters.setFill(Color.TRANSPARENT);
            WritableImage newImage = group.snapshot(parameters, null);

            return newImage;
        } catch (Exception e) {
//            logger.error(e.toString());
            return null;
        }
    }
 
Example 18
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 4 votes vote down vote up
public static Image addMarginsFx(Image image, Color color, int MarginWidth,
            boolean addTop, boolean addBottom, boolean addLeft, boolean addRight) {
        try {
            if (image == null || MarginWidth <= 0) {
                return image;
            }
            int width = (int) image.getWidth();
            int height = (int) image.getHeight();
            int totalWidth = width, totalHegiht = height;
            int x1 = 0, y1 = 0, x2 = width, y2 = height;
            if (addLeft) {
                totalWidth += MarginWidth;
                x1 = MarginWidth;
                x2 = width + MarginWidth;
            }
            if (addRight) {
                totalWidth += MarginWidth;
            }
            if (addTop) {
                totalHegiht += MarginWidth;
                y1 = MarginWidth;
                y2 = height + MarginWidth;
            }
            if (addBottom) {
                totalHegiht += MarginWidth;
            }
//            logger.debug(width + "  " + totalWidth);

            PixelReader pixelReader = image.getPixelReader();
            WritableImage newImage = new WritableImage(totalWidth, totalHegiht);
            PixelWriter pixelWriter = newImage.getPixelWriter();
            pixelWriter.setPixels(x1, y1, width, height, pixelReader, 0, 0);

//            logger.debug(x1 + "  " + y1);
//            logger.debug(totalWidth + "  " + totalHegiht);
            if (addLeft) {
                for (int x = 0; x < MarginWidth; x++) {
                    for (int y = 0; y < totalHegiht; y++) {
                        pixelWriter.setColor(x, y, color);
                    }
                }
            }
            if (addRight) {
                for (int x = totalWidth - 1; x > totalWidth - MarginWidth - 1; x--) {
                    for (int y = 0; y < totalHegiht; y++) {
                        pixelWriter.setColor(x, y, color);
                    }
                }
            }
            if (addTop) {
                for (int y = 0; y < MarginWidth; y++) {
                    for (int x = 0; x < totalWidth; x++) {
                        pixelWriter.setColor(x, y, color);
                    }
                }
            }
            if (addBottom) {
                for (int y = totalHegiht - 1; y > totalHegiht - MarginWidth - 1; y--) {
                    for (int x = 0; x < totalWidth; x++) {
                        pixelWriter.setColor(x, y, color);
                    }
                }
            }

            return newImage;

        } catch (Exception e) {
            logger.error(e.toString());
            return image;
        }

    }
 
Example 19
Source File: SpaceFXView.java    From SpaceFX with Apache License 2.0 4 votes vote down vote up
public LevelBossRocket(final SpaceShip spaceShip, final Image image, final double x, final double y) {
    super(image, x - image.getWidth() / 2.0, y, 0, 1);
    this.spaceShip = spaceShip;
    this.born      = System.nanoTime();
}
 
Example 20
Source File: FxmlImageManufacture.java    From MyBox with Apache License 2.0 4 votes vote down vote up
public static Image combineImagesColumnFx(List<Image> images,
        Color bgColor, int interval, int Margin) {
    try {
        if (images == null || images.isEmpty()) {
            return null;
        }
        Group group = new Group();

        int x = Margin, y = Margin, width = 0, height = 0;
        for (int i = 0; i < images.size(); ++i) {
            Image image = images.get(i);
            ImageView view = new ImageView(image);
            view.setPreserveRatio(true);
            view.setFitWidth(image.getWidth());
            view.setFitHeight(image.getHeight());
            view.setX(x);
            view.setY(y);
            group.getChildren().add(view);

            x = Margin;
            y += image.getHeight() + interval;

            if (image.getWidth() > width) {
                width = (int) image.getWidth();
            }
        }

        width += 2 * Margin;
        height = y + Margin - interval;
        Blend blend = new Blend(BlendMode.SRC_OVER);
        blend.setBottomInput(new ColorInput(0, 0, width, height, bgColor));
        group.setEffect(blend);

        SnapshotParameters parameters = new SnapshotParameters();
        parameters.setFill(Color.TRANSPARENT);
        WritableImage newImage = group.snapshot(parameters, null);

        return newImage;
    } catch (Exception e) {
        logger.error(e.toString());
        return null;
    }
}