Search in sources :

Example 26 with WritableImage

use of javafx.scene.image.WritableImage in project jvarkit by lindenb.

the class CaseControlJfx method saveImageAs.

private void saveImageAs(final ScatterChart<Number, Number> chart, File file) throws IOException {
    WritableImage image = chart.snapshot(new SnapshotParameters(), null);
    String format = "png";
    if (file.getName().toLowerCase().endsWith(".jpg") || file.getName().toLowerCase().endsWith(".jpeg")) {
        format = "jpg";
    }
    ImageIO.write(SwingFXUtils.fromFXImage(image, null), format, file);
}
Also used : WritableImage(javafx.scene.image.WritableImage) SnapshotParameters(javafx.scene.SnapshotParameters)

Example 27 with WritableImage

use of javafx.scene.image.WritableImage in project latexdraw by arnobl.

the class FxRobotCaptureScreenshot method captureScreenshot.

/**
 * Takes a screenshot using a file name.
 * @param dir
 * @param name
 * @param node
 * @return The screenshot
 */
default File captureScreenshot(final Path dir, final String name, final Node node) {
    File captureFile = new File(dir + File.separator + name + new Date().getTime() + ".png");
    Platform.runLater(() -> {
        WritableImage img = node.snapshot(null, null);
        try {
            ImageIO.write(SwingFXUtils.fromFXImage(img, null), "png", captureFile);
        } catch (Exception e) {
            e.printStackTrace();
        }
    });
    return captureFile;
}
Also used : WritableImage(javafx.scene.image.WritableImage) File(java.io.File) Date(java.util.Date) IOException(java.io.IOException)

Example 28 with WritableImage

use of javafx.scene.image.WritableImage in project MAVSlam by ecmnet.

the class StreamRealSenseTest method start.

@Override
public void start(Stage primaryStage) {
    primaryStage.setTitle("BoofCV RealSense Demo");
    FlowPane root = new FlowPane();
    root.getChildren().add(ivrgb);
    ivrgb.setOnMouseMoved(event -> {
        MouseEvent ev = event;
        mouse_x = (int) ev.getX();
        mouse_y = (int) ev.getY();
    });
    // RealSenseInfo info = new RealSenseInfo(320,240, RealSenseInfo.MODE_RGB);
    RealSenseInfo info = new RealSenseInfo(640, 480, RealSenseInfo.MODE_RGB);
    try {
        realsense = new StreamRealSenseVisDepth(0, info);
    } catch (Exception e) {
        System.out.println("REALSENSE:" + e.getMessage());
        return;
    }
    mouse_x = info.width / 2;
    mouse_y = info.height / 2;
    primaryStage.setScene(new Scene(root, info.width, info.height));
    primaryStage.show();
    PkltConfig configKlt = new PkltConfig();
    configKlt.pyramidScaling = new int[] { 1, 2, 4, 8 };
    configKlt.templateRadius = 3;
    PointTrackerTwoPass<GrayU8> tracker = FactoryPointTrackerTwoPass.klt(configKlt, new ConfigGeneralDetector(900, 2, 1), GrayU8.class, GrayS16.class);
    DepthSparse3D<GrayU16> sparseDepth = new DepthSparse3D.I<GrayU16>(1e-3);
    // declares the algorithm
    MAVDepthVisualOdometry<GrayU8, GrayU16> visualOdometry = FactoryMAVOdometry.depthDepthPnP(1.2, 120, 2, 200, 50, true, sparseDepth, tracker, GrayU8.class, GrayU16.class);
    visualOdometry.setCalibration(realsense.getIntrinsics(), new DoNothingPixelTransform_F32());
    output = new BufferedImage(info.width, info.height, BufferedImage.TYPE_3BYTE_BGR);
    wirgb = new WritableImage(info.width, info.height);
    ivrgb.setImage(wirgb);
    realsense.registerListener(new Listener() {

        int fps;

        float mouse_depth;

        float md;

        int mc;

        int mf = 0;

        int fpm;

        @Override
        public void process(Planar<GrayU8> rgb, GrayU16 depth, long timeRgb, long timeDepth) {
            if ((System.currentTimeMillis() - tms) > 250) {
                tms = System.currentTimeMillis();
                if (mf > 0)
                    fps = fpm / mf;
                if (mc > 0)
                    mouse_depth = md / mc;
                mc = 0;
                md = 0;
                mf = 0;
                fpm = 0;
            }
            mf++;
            fpm += (int) (1f / ((timeRgb - oldTimeDepth) / 1000f) + 0.5f);
            oldTimeDepth = timeRgb;
            if (!visualOdometry.process(rgb.getBand(0), depth)) {
                bus1.writeObject(position);
                System.out.println("VO Failed!");
                visualOdometry.reset();
                return;
            }
            Se3_F64 leftToWorld = visualOdometry.getCameraToWorld();
            Vector3D_F64 T = leftToWorld.getT();
            AccessPointTracks3D points = (AccessPointTracks3D) visualOdometry;
            ConvertBufferedImage.convertTo(rgb, output, false);
            Graphics c = output.getGraphics();
            int count = 0;
            float total = 0;
            int dx = 0, dy = 0;
            int dist = 999;
            int x, y;
            int index = -1;
            for (int i = 0; i < points.getAllTracks().size(); i++) {
                if (points.isInlier(i)) {
                    c.setColor(Color.BLUE);
                    x = (int) points.getAllTracks().get(i).x;
                    y = (int) points.getAllTracks().get(i).y;
                    int d = depth.get(x, y);
                    if (d > 0) {
                        int di = (int) Math.sqrt((x - mouse_x) * (x - mouse_x) + (y - mouse_y) * (y - mouse_y));
                        if (di < dist) {
                            index = i;
                            dx = x;
                            dy = y;
                            dist = di;
                        }
                        total++;
                        if (d < 500) {
                            c.setColor(Color.RED);
                            count++;
                        }
                        c.drawRect(x, y, 1, 1);
                    }
                }
            }
            if (depth != null) {
                if (index > -1)
                    System.out.println(visualOdometry.getTrackLocation(index));
                mc++;
                md = md + depth.get(dx, dy) / 1000f;
                c.setColor(Color.GREEN);
                c.drawOval(dx - 3, dy - 3, 6, 6);
            }
            c.setColor(Color.CYAN);
            c.drawString("Fps:" + fps, 10, 20);
            c.drawString(String.format("Loc: %4.2f %4.2f %4.2f", T.x, T.y, T.z), 10, info.height - 10);
            c.drawString(String.format("Depth: %3.2f", mouse_depth), info.width - 85, info.height - 10);
            position.x = T.x;
            position.y = T.y;
            position.z = T.z;
            position.tms = timeRgb;
            bus1.writeObject(position);
            if ((count / total) > 0.6f) {
                c.setColor(Color.RED);
                c.drawString("WARNING!", info.width - 70, 20);
            }
            c.dispose();
            Platform.runLater(() -> {
                SwingFXUtils.toFXImage(output, wirgb);
            });
        }
    }).start();
}
Also used : Listener(com.comino.realsense.boofcv.StreamRealSenseVisDepth.Listener) PkltConfig(boofcv.alg.tracker.klt.PkltConfig) ConfigGeneralDetector(boofcv.abst.feature.detect.interest.ConfigGeneralDetector) AccessPointTracks3D(boofcv.abst.sfm.AccessPointTracks3D) BufferedImage(java.awt.image.BufferedImage) ConvertBufferedImage(boofcv.io.image.ConvertBufferedImage) FlowPane(javafx.scene.layout.FlowPane) Planar(boofcv.struct.image.Planar) GrayU8(boofcv.struct.image.GrayU8) MouseEvent(javafx.scene.input.MouseEvent) GrayU16(boofcv.struct.image.GrayU16) Scene(javafx.scene.Scene) Graphics(java.awt.Graphics) WritableImage(javafx.scene.image.WritableImage) DoNothingPixelTransform_F32(boofcv.alg.distort.DoNothingPixelTransform_F32) Vector3D_F64(georegression.struct.point.Vector3D_F64) Se3_F64(georegression.struct.se.Se3_F64)

Example 29 with WritableImage

use of javafx.scene.image.WritableImage in project JavaFXLibrary by eficode.

the class FxImageComparison method assertSnapshotsEqual.

/**
 * Asserts that the node under test produces the same snapshot than the reference one, using a tolerance thresold.
 * @param referenceSnapshot The path of the reference snapshot (a png picture).
 * @param nodeUnderTest The node under test.
 * @param tolerance The tolerance threshold: the percentage (in [0;100]) of the pixels that can differ.
 * @throws NullPointerException if the reference snapshot is null.
 * @throws IllegalArgumentException if the reference snapshot is invalid or unsupported.
 */
default void assertSnapshotsEqual(final String referenceSnapshot, final Node nodeUnderTest, final double tolerance) throws IOException, URISyntaxException {
    final WritableImage observedImage = new WritableImage((int) nodeUnderTest.getScene().getWidth(), (int) nodeUnderTest.getScene().getHeight());
    Platform.runLater(() -> nodeUnderTest.snapshot(new SnapshotParameters(), observedImage));
    WaitForAsyncUtils.waitForFxEvents();
    final Image oracleImage = new Image(new File(referenceSnapshot).toURI().toURL().toExternalForm());
    assertEquals("The two snapshots differ", 100d, computeSnapshotSimilarity(observedImage, oracleImage), tolerance);
}
Also used : WritableImage(javafx.scene.image.WritableImage) SnapshotParameters(javafx.scene.SnapshotParameters) WritableImage(javafx.scene.image.WritableImage) Image(javafx.scene.image.Image) File(java.io.File)

Example 30 with WritableImage

use of javafx.scene.image.WritableImage in project FXyzLib by Birdasaur.

the class Palette method createPalette.

public Image createPalette(boolean save) {
    if (numColors < 1) {
        return null;
    }
    // try to create a square image
    width = (int) Math.sqrt(numColors);
    height = numColors / width;
    imgPalette = new WritableImage(width, height);
    PixelWriter pw = ((WritableImage) imgPalette).getPixelWriter();
    AtomicInteger count = new AtomicInteger();
    IntStream.range(0, height).boxed().forEach(y -> IntStream.range(0, width).boxed().forEach(x -> pw.setColor(x, y, getColor(count.getAndIncrement()))));
    if (save) {
        saveImage();
    }
    return imgPalette;
}
Also used : IntStream(java.util.stream.IntStream) Color(javafx.scene.paint.Color) Arrays(java.util.Arrays) WritableImage(javafx.scene.image.WritableImage) IOException(java.io.IOException) File(java.io.File) DoubleStream(java.util.stream.DoubleStream) List(java.util.List) PixelWriter(javafx.scene.image.PixelWriter) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) SwingFXUtils(javafx.embed.swing.SwingFXUtils) ImageIO(javax.imageio.ImageIO) Image(javafx.scene.image.Image) WritableImage(javafx.scene.image.WritableImage) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) PixelWriter(javafx.scene.image.PixelWriter)

Aggregations

WritableImage (javafx.scene.image.WritableImage)70 SnapshotParameters (javafx.scene.SnapshotParameters)24 PixelWriter (javafx.scene.image.PixelWriter)21 PixelReader (javafx.scene.image.PixelReader)13 IOException (java.io.IOException)12 Color (javafx.scene.paint.Color)11 BufferedImage (java.awt.image.BufferedImage)9 File (java.io.File)8 Image (javafx.scene.image.Image)6 ImageView (javafx.scene.image.ImageView)6 Group (javafx.scene.Group)5 Random (java.util.Random)4 Canvas (javafx.scene.canvas.Canvas)4 GraphicsContext (javafx.scene.canvas.GraphicsContext)4 Texture (com.almasb.fxgl.texture.Texture)3 OutputStream (java.io.OutputStream)3 Point2D (javafx.geometry.Point2D)3 Scene (javafx.scene.Scene)3 Background (javafx.scene.layout.Background)3 Scale (javafx.scene.transform.Scale)3