Image from clipboard not correctly displayed in JavaFX 8 application

ⅰ亾dé卋堺 提交于 2019-12-04 04:39:14

问题


This program shall paste an image from clipboard into an ImageView (on Windows 10). Unfortunately the image is not correctly displayed.

public class PasteImageFromClipboard extends Application {

ImageView imageView = new ImageView();
Button bnPaste = new Button("Paste");

public static void main(String[] args) {
    Application.launch(args);
}

@Override
public void start(Stage stage) throws Exception {

    bnPaste.setOnAction(new EventHandler<ActionEvent>() {
        public void handle(ActionEvent event) {
            Clipboard cb = Clipboard.getSystemClipboard();
            if (cb.hasImage()) {
                Image image = cb.getImage();
                imageView.setImage(image);
            }
        }
    });

    VBox vbox = new VBox();
    vbox.getChildren().addAll(bnPaste, imageView);
    Scene scene = new Scene(vbox);
    stage.setScene(scene);
    stage.setWidth(400);
    stage.setHeight(400);
    stage.show();
}
}

Steps to reproduce:

  • Start cmd.exe
  • Press ALT-Print to copy the cmd window into the clipboard
  • Start program PasteImageFromClipboard
  • Press "Paste" button in PasteImageFromClipboard

This result is displayed on my computer:

It should be like this:

Is there more code required to draw the image correctly?


回答1:


found this solution by the help of https://community.oracle.com/thread/2238566

    package com.wilutions.jiraddin;

    import java.awt.Graphics;
    import java.awt.Toolkit;
    import java.awt.datatransfer.DataFlavor;
    import java.awt.datatransfer.Transferable;
    import java.awt.image.BufferedImage;
    import java.awt.image.RenderedImage;
    import java.io.ByteArrayInputStream;
    import java.io.ByteArrayOutputStream;

    import javax.imageio.ImageIO;

    import javafx.application.Application;
    import javafx.event.ActionEvent;
    import javafx.event.EventHandler;
    import javafx.scene.Scene;
    import javafx.scene.control.Button;
    import javafx.scene.image.ImageView;
    import javafx.scene.layout.VBox;
    import javafx.stage.Stage;

    public class PasteImageFromClipboard extends Application {

        ImageView imageView = new ImageView();
        Button bnPaste = new Button("Paste");

        public static void main(String[] args) {
            Application.launch(args);
        }

        @Override
        public void start(Stage stage) throws Exception {

            bnPaste.setOnAction(new EventHandler<ActionEvent>() {
                public void handle(ActionEvent event) {
                    try {
                        java.awt.Image image = getImageFromClipboard();
                        if (image != null) {
                            javafx.scene.image.Image fimage = awtImageToFX(image);
                            imageView.setImage(fimage);
                        }
                    }
                    catch (Exception e) {
                        e.printStackTrace();
                    }
                }
            });

            VBox vbox = new VBox();
            vbox.getChildren().addAll(bnPaste, imageView);
            Scene scene = new Scene(vbox);
            stage.setScene(scene);
            stage.setWidth(400);
            stage.setHeight(400);
            stage.show();
        }

        private java.awt.Image getImageFromClipboard() {
            Transferable transferable = Toolkit.getDefaultToolkit().getSystemClipboard().getContents(null);
            if (transferable != null && transferable.isDataFlavorSupported(DataFlavor.imageFlavor)) {
                try {
                    return (java.awt.Image) transferable.getTransferData(DataFlavor.imageFlavor);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
            return null;
        }

        private static javafx.scene.image.Image awtImageToFX(java.awt.Image image) throws Exception {
            if (!(image instanceof RenderedImage)) {
                BufferedImage bufferedImage = new BufferedImage(image.getWidth(null), image.getHeight(null),
                        BufferedImage.TYPE_INT_ARGB);
                Graphics g = bufferedImage.createGraphics();
                g.drawImage(image, 0, 0, null);
                g.dispose();

                image = bufferedImage;
            }
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            ImageIO.write((RenderedImage) image, "png", out);
            out.flush();
            ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
            return new javafx.scene.image.Image(in);
        }

    }


来源:https://stackoverflow.com/questions/34642602/image-from-clipboard-not-correctly-displayed-in-javafx-8-application

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!