File[] array to imageicon java - java

I am using a JFilehooser to load multiple images to a File[].
I then want to load the File[] to multiple ImageIcons. For example:
if (returnValue == JFileChooser.APPROVE_OPTION) {
File[] files = fileChooser.getSelectedFiles();
ImageIcon MyImage = new ImageIcon();
MyImage = files[0];
}
Of course that code doesn't work, but that's what I want to do. How do I do it?

As I understand you want to create array of ImageIcon for selected files:
ImageIcon[] imageIcon = Arrays.stream(files).map(file -> new ImageIcon(file.getAbsolutePath())).toArray(ImageIcon[]::new);

Related

How to change a code of putting image within a label from desktop to project file

So I have this code It's work when the image at my desktop, I added the image at src file put I couldn't convert it can you tell me what is the problem? this the code it set the image to fit the label too:
public void ScalImage() {
ImageIcon image = new ImageIcon("C:\\Users\\HP\\Desktop\\ath3.png");
Image img = image.getImage();
Image imgScale = img.getScaledInstance(jLabel2.getWidth(), jLabel2.getHeight(), Image.SCALE_SMOOTH);
ImageIcon scaliedicon = new ImageIcon(imgScale);
jLabel2.setIcon(scaliedicon);
}
I tried to say: ImageIcon image = new ImageIcon("ath3.png");
didn't work
So I have solved it I wrote like that
ImageIcon image = new ImageIcon(getClass().getResource("ath3.png"));
It's work for me. I wish I'm right

Inserting a file into Jlabel with JFileChooser

Am trying to get a file(image) to fit into a Jlabel with JFileChooser. But it enlarges the Jlabel when i insert the file.
This is a sample of my codes...
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f = chooser.getSelectedFile();
String filename = f.getAbsolutePath();
btnInsert.setText(filename);
ImageIcon icon = new ImageIcon(filename);
lblPic.setIcon(icon);
Try this code, it may helpful to you.
Read the picture as a BufferedImage
BufferedImage img = null;
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File file = chooser.getSelectedFile();
try {
img = ImageIO.read(file );
} catch (IOException e) {
e.printStackTrace();
}
Resize the BufferedImage
Image dimg = img.getScaledInstance(label.getWidth(), label.getHeight(), Image.SCALE_SMOOTH);
Make sure that the label width and height are the same proportions as the original image width and height. In other words, if the picture is 600 x 900 pixels, scale to 100 X 150. Otherwise, your picture will be distorted.
ImageIcon imageIcon = new ImageIcon(dimg)

How to show an image in ImageView in Netbeans?

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.

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

Convert Image to SVG

I use Apache Batik to preview svg documents in a jframe.
What I want to do is the user to give some Images (e.g. png or jpeg)
and then my program to turn them into svg documents.
Is this SVGCreator any possible?
FileChooser
JFileChooser fc = new JFileChooser(".");
int choice = fc.showOpenDialog(panel);
if(choice == JFileChooser.APPROVE_OPTION){
File f = fc.getSelectedFile();
String filepath = f.toURL().toString();
SVGDocument document = SVGCreator(filepath);
SVGViewer(document);
}
}
SVGViewer (extends JFrame)
public SVGViewer(SVGDocument document){
JSVCanvas canvas = new JSVCanvas();
this.getContentPane().add(canvas);
canvas.setSVGDocument(document);
this.pack();
this.setVisible(true);
}

Categories