save a number of images in one png file - javafx

I have a number of images that are displayed in my application. They are generated by the data I enter. Now I want to save the images in one png file (would prefer jpg but png is also doing the job). Right now the last image will be overwritten by the next image, so that only the last image in stored in the png file. I thought about creating one image that consists of all the images I want to save in the png file. Does anyone have an idea of how to do that? Or is there a simpler way of how to save the images in one file?
private final ObservableList<Image> pdfFilePages = FXCollections.observableArrayList();
pngButton.setOnAction(e -> {
if (pdfFilePages.size() > 0) {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle(I18N.get("key.save_to_png"));
File file = fileChooser.showSaveDialog(dialogStage);
if (file != null) {
for (Image img : pdfFilePages) {
ImageView imgView = new ImageView(img);
try {
BufferedImage bi = SwingFXUtils.fromFXImage(img, null);
ImageIO.write(bi, "png", file);
} catch (IOException e1) {
LOGGER.error(e1.getMessage(), e1);
}
}
}
}
});

Related

Load file from Sample File Directory named "images"

I have this simple structure
Now I should load 11.png to ImageView from URI.
How do I do this?
imageView = (ImageView) findViewById(R.id.imageView);
imageView.setImageURI(Uri.fromFile(new File("What should be here???")));
I think you Are New To Android?
Its Better To move the Image File to Drawable Folder and and Set Image View From the Drawable. You Can Use BatchDrawableImport Plugin in Android Studio to Import multiple Drawable Files
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setImageResource(R.drawable.yourImageName);
Or Move the File to Assets Folder and Using Assets Manager You Can Achieve the Solution
AssetManager manager = getAssets();
// Read a Bitmap from Assets
try {
InputStream open = manager.open("icon.png");
Bitmap bitmap = BitmapFactory.decodeStream(open);
ImageView view = (ImageView) findViewById(R.id.ImageView01);
view.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}

how to load image name from database and load it into my imageview

i have a project in which i have in my db images stored with their names only so i want to load those image with javafx
and i have no idea how to load thos images
public void initialize(URL url, ResourceBundle rb) {
// help me here to retreive my images names
}
and this is my button that allows me to change my cover picture
here is the code
changecover.setOnAction(new EventHandler<ActionEvent>() {
#Override
public void handle(ActionEvent event) {
try {
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.jpg)", "*.JPG");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.png)", "*.PNG");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterPNG);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
BufferedImage bufferedImage = ImageIO.read(file);
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
coverpic.setImage(image);
Groupe g = gs.getGroupe(5);
g.setPhoto_couverture(coverpic.toString());
gs.update(g);
//myImageView.setImage(image);
} catch (IOException ex) {
Logger.getLogger(Groupe_homeController.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Why load the image data to a BufferedImage and then convert this BufferedImage to a JavaFX Image when you can avoid storing the image data in memory twice by directly loading the data to a Image:
Image image = new Image(file.toURI().toURL().toExternalForm());
I'm not sure how a database is involved here though, since FileChooser allows you to choose the image from storage devices available to the client only, which means you'd need to implement some way of retrieving the data from the server... (unless the database is for use on the local machine only)

JavaFX : How to make a mark where between two imageview in tilepane

I try to make a slide frame looks like powerpoint using imageview and tilepane.
Let's suppose we are in powerpoint. In photo, there are blue background panel(tilepane) and slide(imageview).
I want make a mark(line) that is red arrow pointed in photo, when I locate mouse cursor where between slide5 and slide6. Can I make?
And how to get the each hgap area's information?
ex) Which hgap clicked?
I can get the each slide(imageview) value, but I can't find how to get the hgap's information.
This is my partial code.
String[] imageName = {"slide1.png", "slide2.png", "slide3.png", "slide4.png",
"slide5.png", "slide6.png", "slide7.png", "slide8.png"};
Image img = null;
for (int i = 0; i < imageName.length; i++) {
try {
img = new Image(getClass().getResourceAsStream(imageName[i]));
} catch (Exception e) {
System.out.println("exception : " + e);
}
ImageView imageview = new ImageView(img);
imageview.setUserData(imageName[i]);
imageview.setFitWidth(widthImageView);
imageview.setFitHeight(heightImageView);
HBox hbox= new HBox();
hbox.getChildren().add(imageview);
tile1.getChildren().add(hbox); }
The hgap and vgap parameters are used for calculating the space between the nodes. You can't put a node in there. You should evaluate your requirements and consider using a different layout like e. g. GridPane.

how open image with url in javafx

I have banner an I wont to put it in my javaFX application.
And when user click on the image open default browser.
try {
String path = "http://developer.am/webservice/banner728x90.gif";
URL url = new URL(path);
BufferedImage image = ImageIO.read(url);
label = new JLabel(new ImageIcon(image));
} catch (Exception exp) {
exp.printStackTrace();
}
also I am trying to convert above code from awt in JavaFX
Lets see. First the ingredients:
Image
Button
ImageView
Open Link in System Browser with JavaFX
Putting this together:
String path = "http://...";
String pathToOpen = "http://...";
Image image = new Image(path);
ImageView imageView = new ImageView(image);
Button button = new Button("clickMe!", imageView);
button.setOnAction(ev -> getHostServices().showDocument(pathToOpen));

Print Stage with JavaFX 8

I found some basic example how to print web page with JavaFX but I'm looking for examplewhich can print the complete Scene with app content. Is there any example?
You can convert your scene to an image using
WritableImage snapshot = scene.snapshot(null);
This will return a WritableImage, which can be converted to an Image File or BufferedImage, and print using the Printing API of JavaFX8 (there are not lot of examples available for this, but the new API has quite a resemblance to the old Printing API, so it won't be a problem)
Converting WritableImage to Png File
WritableImage snapshot = scene.snapshot(null);
File file = new File("image.png");
try {
ImageIO.write(SwingFXUtils.fromFXImage(snapshot, null), "png", file);
} catch (IOException e) {
e.printStackTrace();
}
Converting WritableImage to BufferedImage (used for printing)
WritableImage snapshot = scene.snapshot(null);
BufferedImage bufferedImage = SwingFXUtils.fromFXImage(snapshot, null);
For just a small example of how to print an Image using java, please go through
Proper way of printing a BufferedImage in Java
How to print image in java

Resources