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?
Related
I'm implementing a frame processor plugin (react-native-vision-camera). I would like to be able to load an image from a file and return it instead of the ImageProxy type frame so I can controll frames content.
For that, I have to load an image (no worries so far) and create an Image or an ImageProxy from the loaded image.
(I also tried to replace the bytes of the ImageProxy type image with those of the loaded image but I didn't succeed either. So I am also interested in this topic.)
#Override
public Object callback(#NotNull ImageProxy image, #NotNull Object[] params) {
try {
// Get path of image to load
String param = params[0].toString();
Bitmap bmImg = BitmapFactory.decodeFile(param);
ImageView img = new ImageView(this);
img.setImageBitmap(bmImg);
return img;
//return image;
} catch (Exception e) {
System.out.println("CRASHED");
e.printStackTrace();
}
return null;
}
To do this I tried the code above but I am creating an ImageView and not an Image or an ImageProxy.
Any idea or sample code so I can control frame content?
Thanks in advance
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.
In my JavaFX application I use a TreeView. The single TreeItems contain an image. Unfortunately, the image is shown only once.
The code for loading the image looks like the following. It is called every time the TreeView changes. The Images are cached in a Map ("icons"). Thus, a new ImageView is created every time.
public final ImageView getImage(final String imageConstant) {
final Image img;
if (icons.containsKey(imageConstant)) {
img = icons.get(imageConstant);
}
else {
if (isRunFromJAR) {
img = new Image("/path/" + imageConstant, iconSizeWidth,
iconSizeHeight, false, false);
}
else {
img = new Image(getClass().getResourceAsStream(imageConstant), iconSizeWidth,
iconSizeHeight, false, false);
}
icons.put(imageConstant, img);
}
return new ImageView(img);
In the updateItem method of my TreeCells, the ImageView returned above is stored into a field called icon. The field is used in the following code, also from updateItem:
if (icon == null) {
setGraphic(label);
}
else {
final HBox hbox = new HBox(ICON_SPACING);
final Label iconLabel = new Label("", icon);
hbox.getChildren().addAll(iconLabel, label);
setGraphic(hbox);
}
But for some reason, the problem shown in the gif occurs.
Update
Actually, the ImageViews were cached internally, which led to de-duplication. Using multiple ImageViews is the solution.
the ImageView returned above is stored into a field called icon.The field is used in the following code...
The ImageView is type of Node. A Node can only have one parent at a time!
So don't store/cache a ImageView(-Node). Create new ImageView-Nodes.
Edit:
As pointed out in the comments by James_D:
...you can use the same Image(-Object) for every ImageView...
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.
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();
}