Adding frame to picture in Javafx - java

i would like to add a frame to a picture using Java and Javafx and then save the framed picture. What would be the best way to do that?
For example say I have a photo of a landscape and want to add a frame to it. The framed photo should look like this:

You could add two images, first the frame, then the image, to the same canvas like this:
GraphicsContext gc1 = canvas.getGraphicsContext2D();
gc1.drawImage(frameimage,0,0,image.getFitWidth()+20,image.getFitHeight()+20);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.drawImage(i,10,10,image.getFitWidth(),image.getFitHeight());
and then save them as png (or whatever format you like) using the canvas.snapshot function:
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter =new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
fileChooser.getExtensionFilters().add(extFilter);
Stage primaryStage = (Stage) canvas.getScene().getWindow();
File file = fileChooser.showSaveDialog(primaryStage);
if(file != null){
try {
WritableImage writableImage = new WritableImage((int)canvas.getWidth(), (int)canvas.getHeight());
canvas.snapshot(null, writableImage);
RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
File file1 = new File(file.getAbsolutePath()+".png");
file.renameTo(file1);
ImageIO.write(renderedImage, "png", file1);
} catch (IOException ex) {
ex.printStackTrace();
}

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.

How do I display an image using javaFX by selecting a file from file explorer

1.I have a code that opens a pane and I can open file explorer, but I do not know how to open said image when I select it from file explorer. Also this code is meant to be used in different #Overrride statements but I have only managed to get this far using one statement. Is there a way for me to call some of these events from another Override statement?
#Override
public void start(Stage primaryStage) {
//Stage
primaryStage.setTitle("title");
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane);
Button load = new Button("Load");
load.setOnAction(loadEventListener);
ImageView myImageView = new ImageView();
HBox rootBox = new HBox();
rootBox.getChildren().addAll(load, myImageView);
//Toolbar
HBox toolbarArea = new HBox( 10 );
toolbarArea.setPadding( new Insets( 10 ) );
primaryStage.setScene(scene);
primaryStage.show();
//Puts buttons on bottom bar
toolbarArea.getChildren().addAll( load );
pane.setBottom( toolbarArea );
}
EventHandler<ActionEvent> loadEventListener
= t -> {
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);
try {
BufferedImage bufferedImage = ImageIO.read(file);
Image image = SwingFXUtils.toFXImage(bufferedImage, null);
ImageView myImageView = new ImageView();
myImageView.setImage(image);
} catch (IOException ex) {
Logger.getLogger(JavaFXPixel.class.getName()).log(Level.SEVERE, null, ex);
}
};
Since you already have an ImageView and have shown it in the scene, simply set its image to the image you load.
For this you need to make the ImageView an instance variable:
private ImageView myImageView ;
#Override
public void start(Stage primaryStage) {
//Stage
primaryStage.setTitle("title");
BorderPane pane = new BorderPane();
Scene scene = new Scene(pane);
Button load = new Button("Load");
load.setOnAction(loadEventListener);
myImageView = new ImageView();
HBox rootBox = new HBox();
rootBox.getChildren().addAll(load, myImageView);
// presumably you intended this somewhere?
pane.setCenter(rootBox);
//Toolbar
HBox toolbarArea = new HBox( 10 );
toolbarArea.setPadding( new Insets( 10 ) );
primaryStage.setScene(scene);
primaryStage.show();
//Puts buttons on bottom bar
toolbarArea.getChildren().addAll( load );
pane.setBottom( toolbarArea );
}
Note there is no need to load a buffered image and then convert it. So all you need in the event handler is
loadEventListener = t -> {
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);
if (file != null) {
Image image = new Image(file.toURI().toString());
myImageView.setImage(image);
}
};

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.

Using a variable outside of an lambda expression block in JavaFX

So, I have a program that loads an image with a FileChooser. I use a lambda expression to define this loading action so the function LoadImage() returns the chosen image inside the event block but I can't use it outside this block.
Can someone help me?
Here is my code so far:
public class Main extends Application{
Stage window;
BorderPane layout;
ImageView imageView;
Image currentImage;
public static void main(String[] args){
launch(args);
}
#Override
public void start(Stage primaryStage) throws Exception{
window = primaryStage;
window.setTitle("Image Viewer v.1.0");
//MENU
Menu fileMenu = new Menu("_File");
MenuItem openFile = new MenuItem("Open file...");
openFile.setOnAction(e -> currentImage = LoadImage());
fileMenu.getItems().add(openFile);
//Main menu bar
MenuBar menuBar = new MenuBar();
menuBar.getMenus().addAll(fileMenu);
imageView = new ImageView();
imageView.setImage(currentImage); //here the currentImage is null
layout = new BorderPane();
layout.setTop(menuBar);
layout.setCenter(imageView);
Scene scene = new Scene(layout, 900, 600);
window.setScene(scene);
window.show();
}
public Image LoadImage()
{
final Image image;
FileChooser fileChooser = new FileChooser();
//Set extension filter
FileChooser.ExtensionFilter extFilterJPG = new FileChooser.ExtensionFilter("JPG files (*.JPG)", "*.JPG");
FileChooser.ExtensionFilter extFilterjpg = new FileChooser.ExtensionFilter("jpg files (*.jpg)", "*.jpg");
FileChooser.ExtensionFilter extFilterPNG = new FileChooser.ExtensionFilter("PNG files (*.PNG)", "*.PNG");
FileChooser.ExtensionFilter extFilterpng = new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
fileChooser.getExtensionFilters().addAll(extFilterJPG, extFilterjpg, extFilterPNG, extFilterpng);
//Show open file dialog
File file = fileChooser.showOpenDialog(null);
return new Image(file.toURI().toString());
}
} // end of Main class
So forth, nothing is displayed in the ImageView.
How can I use the value returned by the LoadImage() method?
The value of currentImage is indeed changed, but when you set the value of imageView's image property, it is not yet set. You have to change the property of your ImageView, not the variable (which you use only before setting the image).
Try changing your lambda to:
openFile.setOnAction(e -> imageView.setImage(LoadImage()) )

Open, edit and save image canvas on file.pgm in JavaFX

this drawing area to draw image with canvas
Canvas canvas= new Canvas();
canvas.setHeight(500);
canvas.setWidth(700);
GraphicsContext gc = canvas.getGraphicsContext2D();
gc.setFill(Color.BLACK);
gc.fillRect(0, 0, canvas.getWidth(), canvas.getHeight());
gc.setFill(Color.WHITE);
gc.fillRect(1, 1, canvas.getWidth() - 2, canvas.getHeight() - 2);
and I made this method to export drawing canvas to imagem.pgn or snapshot and its correctly
public void snapshotCanvasImageToPNG(Stage primaryStage) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter
= new FileChooser.ExtensionFilter("png files (*.png)", "*.png");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showSaveDialog(primaryStage);
if (file != null) {
try {
WritableImage writableImage = new WritableImage((int) canvas.getWidth(), (int) canvas.getHeight());
canvas.snapshot(null, writableImage);
RenderedImage renderedImage = SwingFXUtils.fromFXImage(writableImage, null);
ImageIO.write(renderedImage, "png", file);
} catch (IOException ex) {
Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
and I have many doubts in making these methods to open edit and save the canvas drawing to file.pgm
I made this but it complicate to finish
public void openFilePGM(Stage primaryStage) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter
= new FileChooser.ExtensionFilter("PGM files (*.pgm)", "*.pgm");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showOpenDialog(primaryStage);
if (file != null) {
//doubt here
}
}
public void saveFilePGM(Stage primaryStage) {
FileChooser fileChooser = new FileChooser();
FileChooser.ExtensionFilter extFilter
= new FileChooser.ExtensionFilter("PGM files (*.pgm)", "*.pgm");
fileChooser.getExtensionFilters().add(extFilter);
File file = fileChooser.showSaveDialog(primaryStage);
if (file != null) {
//doubt here
}
}
The only way you will get to work with PGM images in JavaFX is through ImageJ or Java Advanced Imaging as mentioned by #VGR or another third party plugin.
Please check this plugin that contains code for such IO functions.
And you might also want to look at this answer which can show you the formats you can easily handle with JavaFX.

Categories

Resources