Loading pictures from Filechooser - java

My code:
try {
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Open Resource File");
fileChooser.getExtensionFilters().add(new FileChooser.ExtensionFilter("PNG", "*.png"));
File file = fileChooser.showOpenDialog(null);
BufferedImage originalImage = ImageIO.read(new File(memberView.selectedPicturePath.getText()));
ImageIO.write(originalImage, ".png", file.getAbsoluteFile());
} catch (IOException e) {
System.out.println(e.getMessage());
}
i want to save an image from a random directory to the one specified here but every time i get a Can't read input file! message.
The image i am trying to load actually exists since i am choosing it with the file loader.
Where is the problem in this code?

Check that what method memberView.selectedPicturePath.getText()) is return, perhaps it is not valid path to your image.

Related

How can i make .png my default file extension in java

Here is the snippet where i save the image with a .png extension:
WritableImage img = rectCanvas.snapshot(new SnapshotParameters(), null);
JFileChooser chooser = new JFileChooser();
chooser.setCurrentDirectory(new File(System.getProperty("user.home")));
int result = chooser.showSaveDialog(null);
if (result == JFileChooser.APPROVE_OPTION) {
try {
File fileToSave = chooser.getSelectedFile();
ImageIO.write(SwingFXUtils.fromFXImage(img, null), "png", fileToSave);
} catch (IOException ex) {
Logger.getLogger(GuiClass.class.getName()).log(Level.SEVERE, null, ex);
}
}
I want .png to be my default file extension. However when i want to save an image to file and open the save dialog i have to enter the file name in the following format: filename.png. How can i get rid of the .png part and make it default?
What are you setting as the value of "fileToSave"? If that isn't .png, it shouldn't be a png extension.
See the following from oracle's documentation:
try {
// retrieve image
BufferedImage bi = getMyImage();
File outputfile = new File("saved.png");
ImageIO.write(bi, "png", outputfile);
} catch (IOException e) {
...
}

how to upload image in form using code netbeans

Am writing an ID CARD processing app in java but am having problem with the code that will upload and display a client image in a label i created for picture. the only thing am getting with the code below is the image path but not the image it self.
This is the code i have attempted so far.
FileFilter ff = new FileNameExtensionFilter("images","jpeg");
fc.addChoosableFileFilter(ff);
int open = fc.showOpenDialog(this);
if (open == javax.swing.JFileChooser.APPROVE_OPTION){
java.io.File path = fc.getSelectedFile();
String file_name = path.toString();
pathe.setText(file_name);
java.io.File image = fc.getSelectedFile();
ImageIcon photo = new ImageIcon(image.getAbsolutePath());
The code that does the magic is below
FileFilter ff = new FileNameExtensionFilter("images","jpeg");
fc.addChoosableFileFilter(ff);
int open = fc.showOpenDialog(this);
if (open == javax.swing.JFileChooser.APPROVE_OPTION){
java.io.File path = fc.getSelectedFile();
String file_name = path.toString();
pathe.setText(file_name);
BufferedImage bi; // bi is the object of the class BufferedImage
// Now you use try and catch `enter code here`
try{
bi = ImageIO.read(path); // path is your file or image path
jlabel.setIcon( new ImageIcon(bi));
}catch(IOException e){ }

Image IO Write not writing

basically I am trying to save an image I have edited in a JFrame, so I have a menu with the save item and I have an action listener to set up for the save item and everything works fine, the file chooser comes up and I can select where I would like to save it, only thing is when I hit save, its not there. Here is my code, am I missing something?
if(e.getSource().equals(Save)){
JFileChooser keep = new JFileChooser();
keep.setSelectedFile(new File ("newImage.jpg"));
FileNameExtensionFilter filters = new FileNameExtensionFilter("jpeg", "jpg");
keep.setFileFilter(filters);
File output = keep.getSelectedFile();
int count = keep.showSaveDialog(keep);
BufferedImage out = filteredImage;
if (count == JFileChooser.CANCEL_OPTION){
}
else{
try{
ImageIO.write(out, "jpg", output);
//I put this here to see if I was even reaching the method
System.out.println("writing method");
}catch(Exception d){
}
}
}
So, you get a reference to the selectedFile...
File output = keep.getSelectedFile();
The you show the dialog...
int count = keep.showSaveDialog(keep);
BufferedImage out = filteredImage;
Then you try and save the image...
ImageIO.write(out, "jpg", output);
...wait, what?! Assuming that getSelectedFile isn't null, how do you know where you're actually saving the image?
That process should be reversed slightly...
showSaveDialog
if (accepted) {
saveFile = getSelectedFile
ImageIO.write(img, "jpg", saveFile);
}
as a basic psudo code example

Save image inside specific folder and store path in mysql to display later

For my project, I am creating an expense management system. There is a place where the user can select an image file from local disk and add it as an attachment when adding a new expense.
The uploaded image should be displayed in a jLabel and then should be saved inside the project folder (say for e.g. /src/accounts/media) when clicking the save button, and the path should be saved inside a varchar column in mysql database for later retrieval.
For now, I can upload the image and display the image in jLabel.
Can anyone help me out on how save that image file inside a folder and to store the path in database?
JFileChooser chooser = new JFileChooser();
FileFilter ft = new FileNameExtensionFilter("Image Files", "jpg", "png", "jpeg");
//FileFilter ft2 = new FileNameExtensionFilter("PDF Files", "pdf");
chooser.addChoosableFileFilter(ft);
//chooser.addChoosableFileFilter(ft2);
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
filePath = f.getAbsolutePath().toString();
BufferedImage img = null;
try {
img = ImageIO.read(new File(filePath));
} catch (IOException e) {
e.printStackTrace();
}
Image dimg = img.getScaledInstance(lblAttachment.getWidth(), lblAttachment.getHeight(), Image.SCALE_SMOOTH);
ImageIcon icon = new ImageIcon(dimg);
lblAttachment.setText("");
lblAttachment.setIcon(icon);
To copy file to another location, you can choose any
Way1
Way2
To store the path, make a file-path column write insert query (SQL) :
prepareStatement ps = conn.prepareStatement("INSERT INTO filePath VALUES(?, ?, ?)" );
ps.setString(1,filePath);
ps.set..
// conn, connection object
Full tutorial Here
Edit:
To save it in you some default Resources folder, You can obtain path, for EG:
public class ClassDemo {
public static void main(String[] args) throws Exception {
ClassDemo c = new ClassDemo();
Class cls = c.getClass();
// finds resource relative to the class location
URL url = cls.getResource("file.txt");
System.out.println("Value = " + url);
// finds resource relative to the class location
url = cls.getResource("newfolder/a.txt");
System.out.println("Value = " + url);
}
}

Display dicom in JavaFX button

I have many dicom (dcm) images that I want to display as thumbnails in my program. I can load it and draw in a separate window with ImageJ plugin:
try {
FileInputStream fis = new FileInputStream(dcms);
DICOM d = new DICOM(fis);
d.run(dcms);
d.setTitle(title);
d.show();
d.draw();
} catch (Exception e) {e.printStackTrace();}
}
But I want to be able to load it as a Image in a JavaFX button (as thumbnails) as I can do with pngs:
try{
Image picture = new Image(getClass().getResourceAsStream(dcms));
bt.setGraphic(new ImageView(picture));
}
I couldn't find any similar example on Google (most of the results lead to programs to convert the dicom to another thing through a program). But I don't want to convert and then display, I just want to display it.
Do you know if it's possible? Or will I have to convert it before loading the thumbnails?
EDIT : I know that I can save each picture somewhere e.g temp folder and then load it as a Image, but I still think that it's a unnecessary workaround. And I would like to avoid it if possible.
I found a way to use it:
FileInputStream fis;
try {
fis = new FileInputStream(path);
DICOM d = new DICOM(fis);
d.run(path);
Image picture = SwingFXUtils.toFXImage(d.getBufferedImage(), null);
Button bt = new Button();
bt.setGraphic(new ImageView(picture));
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Posted because it might be useful for someone else.

Categories