How to show an image in ImageView in Netbeans? - java

I have a list of movies and information about them that's saved in a mysql database. When I click on a movie on the list, it shows me information about the movie, but I have trouble loading the image for each movie. One of the columns in the database is "ImageName". All images are saved in the same folder, so I'm trying to load them like this:
#FXML
ImageView sl;
...
String imgName = this.film.getImageName();
i = new Image("/movies/images/" + imgName);
sl.setImage(i)
It doesn't work, but when I try to load a specific image like this, it works:
i = new Image("/movies/images/Lotr.jpg");
I have tried printing the path to the image to see what the problem is:
String msg = ("/movies/images/" + imgName);
System.out.print(msg);
Result: /movies/images/ L o t r
It prints three spaces between the letters of image name, so I'm assuming that's why my code isn't working, but I don't know how to fix this.
Edit:
Here's how I choose an image and assign the name to imageName:
FileChooser fileChooser = new FileChooser();
fileChooser.setTitle("Select Image File");
fileChooser.getExtensionFilters().addAll( new FileChooser.ExtensionFilter("Image Files",
"*.bmp", "*.png", "*.jpg", "*.gif"));
File selectedFile = fileChooser.showOpenDialog(null);
filePath = selectedFile.getAbsolutePath();
File sourceFile = new File(filePath);
File destinationFile = new File("C:\\Users\\t\\Documents\\NetBeansProjects\\Movies\\src\\movies\\images\\" + sourceFile.getName());
imageName = sourceFile.getName();
copyFile(sourceFile, destinationFile);
Btw, the names of images look correct in the database.

Related

Multiple images to pdf. How do i string multiple images so bitmapfactory can decode

My user inputs a folder name. Then takes pictures which are saved in the folder. I want to take all the files in the folder which will all be jpg files and create one pdf. There wouldnt be more than 5 images in the folder.
How do i extract all of the files out of the folder so i can pass the strings to the bitmapfacory.decodeFile
So far i have tried the following code.
To test the pdfcreater i named the jpg something in the code. Then took a pic and renamed it the same. It created the pdf with my image.
I have also tried the currentPhotoPath and that works for the one current photo.
The folder that holds all the JPG's is folderName1
private void buttonCreatePDF() {
Intent folInt = getIntent();
String folderName1 = folInt.getStringExtra("Value");
String file1 = directoryPDF + folderName1 ;
Bitmap bitmap = BitmapFactory.decodeFile(file1);
PdfDocument pdfDocument = new PdfDocument();
PdfDocument.PageInfo pageInfo = new PdfDocument.PageInfo.Builder(bitmap.getWidth(), bitmap.getHeight(), 1).create();
PdfDocument.Page page = pdfDocument.startPage(pageInfo);
page.getCanvas().drawBitmap(bitmap, 0, 0, null);
pdfDocument.finishPage(page);
String pdfFile = directoryPDF + "/" + folderName1 + ".pdf";
File myPDFfile = new File(pdfFile);
try {
pdfDocument.writeTo(new FileOutputStream(myPDFfile));
Toast.makeText(this, "PDF file generated successfully.", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
pdfDocument.close();
}
}

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

FileNotFoundException when reading a BufferedImage

I'm trying to crop an image received from a form upload. Before I crop it I save it, then I retrieve it again as a BufferedImage (because I don't know how to turn a part into a buffered Image). I then crop this image, but when I try to save it again I get a java.io.FileNotFoundException (access denied)
The first image gets saved correctly, I get the exception when I try to pull it back.
Is it possible to turn my part into a buffered image and then save it? Instead of doing double work. or else is there some fix to my below code.
String savePath = "path";
File fileSaveDir = new File(savePath);
if (!fileSaveDir.exists()) {
fileSaveDir.mkdir();
}
for (Part part : request.getParts()) {
//functionality to ormit non images
String fileName = extractFileName(part);
part.write(savePath + "/" + fileName);
String imagePath = savePath + "/" + fileName;
BufferedImage img = null;
try {
img = ImageIO.read(new File(imagePath));
img = img.getSubimage(0, 0, 55, 55);
ImageIO.write(img, "jpg", fileSaveDir);
} catch (IOException e) {
System.out.println(e);
}
}
ImageIO.write((RenderedImage im, String formatName, File output));
Parameters:
im a RenderedImage to be written.
formatName a String containg the informal name of the format.
output a File to be written to.
As per documentation output file parameter is the file object where it would be image written where you have passed the parent directory file object.

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);
}
}

Categories