Copy image from JavaFX application and paste it using Windows - java

I am working on a JavaFX application. I want to copy image from application using context menu and paste it using Windows feature of paste.
File file = new File("C:\\Users\\Admin\\Desktop\\my\\mysql.gif");
Image image = new Image(file.toURI().toString());
ImageView ive =new ImageView(image);
cm = new ContextMenu();
MenuItem copy = new MenuItem("Copy");
cm.getItems().add(copy);
copy.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent t) {
//Paste Image at location
Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
content.putImage(image); // the image you want, as javafx.scene.image.Image
clipboard.setContent(content);
}
});
For example, like shown below in images.
And want to paste at location from using of Windows features menu.

Use the Clipboard and ClipboardContent, e.g. as:
Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent content = new ClipboardContent();
// for paste as image, e.g. in GIMP
content.putImage(image); // the image you want, as javafx.scene.image.Image
// for paste as file, e.g. in Windows Explorer
content.putFiles(java.util.Collections.singletonList(new File("C:\\Users\\Admin\\Desktop\\my\\mysql.gif")));
clipboard.setContent(content);
For the "Paste" operation of Windows context menus to work, the clipboard content has to be File. In the case demonstrated above, this is easy, otherwise a temporary file should be created.

Related

Image show up in JavaFX for GridPane? [duplicate]

I tested this code in order to create dialog with image.
final int xSize = 400;
final int ySize = 280;
final Color backgroundColor = Color.WHITE;
final String text = "SQL Browser";
final String version = "Product Version: 1.0";
final Stage aboutDialog = new Stage();
aboutDialog.initModality(Modality.WINDOW_MODAL);
Button closeButton = new Button("Close");
closeButton.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent arg0) {
aboutDialog.close();
}
});
GridPane grid = new GridPane();
grid.setAlignment(Pos.CENTER);
grid.setHgap(10);
grid.setVgap(10);
grid.setPadding(new Insets(25, 25, 25, 25));
Image img = new Image("logo.png");
ImageView imgView = new ImageView(img);
grid.add(imgView, 0, 0);
grid.add(new Text(text), 0, 1);
grid.add(new Text(version), 0, 2);
grid.add(closeButton, 14, 18);
Scene aboutDialogScene = new Scene(grid, xSize, ySize, backgroundColor);
aboutDialog.setScene(aboutDialogScene);
aboutDialog.show();
I placed the image file into the directory /src.
But for some reason the image is not displayed. Can you help me to correct my mistake?
Simply replace this code:
Image img = new Image("logo.png");
with this
Image img = new Image("file:logo.png");
Docu reference.
https://docs.oracle.com/javase/8/javafx/api/javafx/scene/image/Image.html
When you pass a String to the Image class it can be handled in four different ways (copied from docu):
// The image is located in default package of the classpath
Image image1 = new Image("/flower.png");
// The image is located in my.res package of the classpath
Image image2 = new Image("my/res/flower.png");
// The image is downloaded from the supplied URL through http protocol
Image image3 = new Image("http://sample.com/res/flower.png");
// The image is located in the current working directory
Image image4 = new Image("file:flower.png");
The file: prefix is simply an URI scheme, or in other words the counterpart to the http: protocol classifier. This also works in the file browser, or in the web browser... ;)
For further reference, you can take a look at the wiki page of the file URI scheme: https://en.wikipedia.org/wiki/File_URI_scheme
Happy Coding,
Kalasch
Try this:
img = new Image("/logo.png");
If no protocol part indicating a URL (as http: or file:) is given, the file is supposed to reside in the default package. If you want it to put in a different package say com.my.images you add this information in a path like manner:
img = new Image("/com/my/images/logo.png");
Image img = new Image("file:/logo.png");
or way with path:
Image img = new Image("file:c:/logo.png");
or
File f = new File("c:\\logo.png");
Image img = new Image(f.toURI().toString());
also can use:
new Image(file:src/logo.png) //root of project
This functions:
Image image = new Image(getClass()
.getResourceAsStream("ChimpHumanHand.jpg"));
copy and paste the image into folder where source package(source packages in NetBeans IDE) is present. Then
Image image = new Image("a1.jpg");
Image image = new Image("File:a1.jpg");
both will work.

Javafx ImageView with scene Builder

I'm trying to preview different images (depends on the file name) using ImageView, but for some reason it loads the image without previewing it.
public void imageRec(){ //method called upon when the file is changed
String imfichier = resources[track].getName(); //gets the name of the current song
String imName = imfichier.substring(0, imfichier.lastIndexOf(".")); //removes the ".mp3" extension from the current song
File ff = new File("cover/" + imName + ".png"); //loads the image
image = new Image(ff.toURI().toString());
cover.setImage(image); //Should preview the image but nothing happens
cover.setCache(true); //cover is the fx:id of ImageView
}
I know that I can load the url in the .fxml file directly, but then I won't be able to change the image if I do that. Any advice?

JavaFX 8 files drag and drop works but copy and paste doesn't

I am writing JavaFX 8 application.
I can successfully drag one or more files from my application and drop them into a file manager (Naitilus).
Here is how I put the files to the dragboard:
private void dragDetected(MouseEvent mouseEvent,Thumbnail thumbnail) {
Dragboard dragboard = thumbnail.startDragAndDrop(TransferMode.COPY);
ClipboardContent clipboardContent = new ClipboardContent();
List<File> lstFileSelected = getListFileSelected();
clipboardContent.putFiles(lstFileSelected);
dragboard.setContent(clipboardContent);
mouseEvent.consume();
}
I can successfully copy a string from my application and paste it in an outer editor (gEdit).
Here is how I put the string to the system clipboard:
private void copyToClipboard() {
Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent clipboardContent = new ClipboardContent();
clipboardContent.putString("Dummy");
clipboard.setContent(clipboardContent);
}
But when I try to copy one or more files from my application and to paste them into a file manger (Nautilus), into a folder with permissions==0777, I can't do this as a 'paste' option in the file manager's menu is disabled.
Here is how i put the files to the system clipboard:
private void copyToClipboard() {
Clipboard clipboard = Clipboard.getSystemClipboard();
ClipboardContent clipboardContent = new ClipboardContent();
List<File> lstFileSelected = getListFileSelected();
// lstFileSelected DOES HAVE all the selected files (one or more)
clipboardContent.putFiles(lstFileSelected);
clipboard.setContent(clipboardContent);
}
My environment:
Debian GNU/Linux 9 (stretch) 64-bit with Gnome 3.22.2
Java jdk-8u152-linux-x64
Eclipse SDK Oxygen.1a (4.7.1a)
Can anybody help me to understand what is wrong.
Thank you, Mikhail.

codename one signatueComponent image upload

i have be trying to upload the signature captured from the codename one signature to my php server.the problem is that the image uploaded is a black image.Below is my code.how can i fix this
SignatureComponent sig = new SignatureComponent();
sig.addActionListener((evt)-> {
try{
img = sig.getSignatureImage();
}catch(Exception ex){
ex.printStackTrace();
}
// Now we can do whatever we want with the image of this signature.
});
Button sv = new Button("save");
sv.addActionListener(new ActionListener(){
#Override
public void actionPerformed(ActionEvent evt) {
try {
Label it = new Label();
it.setIcon(img);
orderHome.add(it);
ImageIO imgIO= ImageIO.getImageIO();
ByteArrayOutputStream out = new ByteArrayOutputStream();
imgIO.save(img, out,ImageIO.FORMAT_JPEG, 1);
byte[] ba = out.toByteArray();
MultipartRequest request = new MultipartRequest();
String url = Global.url1 + "upload_photo.php";
request.setUrl(url);
request.addData("file",ba,"image/jpeg");
request.addArgument("order_id", order_id);
request.addArgument("customer_id", customer_id);
NetworkManager.getInstance().addToQueue(request);
and the php code
[![image uploaded][1]][1]
<?php
#SESSION_START();
require_once("../includes/functions.php");
$target_path="../uploads/";
$customer_id=$_REQUEST['customer_id'];
$order_id=$_REQUEST['order_id'];
$uid = uniqid();
$file =$uid.".jpg";
$sucess=move_uploaded_file($_FILES["file"]["tmp_name"], $target_path.$file);
the black img is the file which is uploaded to the server.the other shows the screenshot of the running app.i would like to upload the signature as shown in the screenshot
The signature generates a translucent image. JavaSE has some issues with saving translucent images as JPEGs and thus PNG works well. Another alternative would be to create an opaque image and save that as a JPEG e.g.:
Image myImage = Image.create(img.getWidth(), img.getHeight());
myImage.getGraphics().drawImage(img, 0, 0);
The new myImage will be opaque with the white color background.

Zest: export diagram to an image/pdf

I have created a network view diagram using zest framework, this uses SWT display/shell to display the UI. I want to export the UI to an image/pdf.
How to do it? Any ideas?
You can use the SWT GC.copyArea() method to copy the contents of a Control to an Image, then save the Image to file. For example, if you have a Zest GraphViewer, viewer, the following code will copy its contents to a PNG file named out.png.
GC gc = new GC(viewer.getControl());
Rectangle bounds = viewer.getControl().getBounds();
Image image = new Image(viewer.getControl().getDisplay(), bounds);
try {
gc.copyArea(image, 0, 0);
ImageLoader imageLoader = new ImageLoader();
imageLoader.data = new ImageData[] { image.getImageData() };
imageLoader.save("c:\\out.png", SWT.IMAGE_PNG);
} finally {
image.dispose();
gc.dispose();
}

Categories

Resources