Reading Image Path in Java - java

I want to read the full path of an image located in my package to scale it so the image can fit my JLabel, however, everytime I compile my code the following error is produced:
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(Unknown Source)
Code:
public void loadImage(){
BufferedImage img = null;
try {
img = ImageIO.read(new File("/myPackage/image.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
BufferedImage dimg = (BufferedImage) img.getScaledInstance(lblImage.getWidth(), lblImage.getHeight(),
Image.SCALE_SMOOTH);
ImageIcon imageIcon = new ImageIcon(dimg);
lblImage.setIcon(imageIcon);
contentPane.add(lblImage);
}
Thanks in advance! :)

Related

How to resize image in JAVA to fit a jLabel?

Apparently, this is my code I have been using to get image from my disks to my program.
public void getVehicleImage(){
FileDialog fd = new FileDialog(this);
fd.setFile("*.jpg; *.jpg; *.png; *.gif");
fd.show();
vehicle_path = fd.getDirectory()+fd.getFile();
vehicleFileName.setText(vehicle_path = fd.getFile());
vehicleImagePath.setText(vehicle_path = fd.getDirectory()+fd.getFile());
image.setIcon(new ImageIcon(vehicle_path));
}
How do I fit the image to the size of my jLabel? I have tried using the
getScaledInstance()
but still no good. And also I want to ask if I am using the right code on how to get Image from my disk? I kinda feel it is wrong.
I faced similar problem and did below workaround:
Step1: Read the picture as a BufferedImage from your file system.
BufferedImage image = null;
try {
image = ImageIO.read(new File("fileName.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
Step2: Create a new BufferedImage that is the size of the JLabel
BufferedImage img= image.getScaledInstance(label.width, label.height,
Image.SCALE_SMOOTH);
Step3: Create new ImageIcon from the resized BufferedImage (Step 2)
ImageIcon imageIcon = new ImageIcon(img);
In your case, create a helper method and call it while creating ImageIcon as below:
public void getVehicleImage(){
...................
image.setIcon(new ImageIcon(getScaledImage(vehicle_path)));//call helper here
}
This can be helper function:
public BufferedImage getScaledImage(String imagePath){
BufferedImage image = null;
try {
image = ImageIO.read(new File("fileName.jpg"));
} catch (IOException e) {
e.printStackTrace();
}
BufferedImage img= image.getScaledInstance(label.width, label.height,
Image.SCALE_SMOOTH);
return img;
}

cvSaveImage() save image to a specific directory

I'm using JavaCV. My program required to make a webcam photo and save it to the folder which is on the desktop.
Here is the path to the folder :
public static String webcamPath = System.getProperty("user.home") + "/Desktop/folder/webcam.png"
That is how i save the image :
FrameGrabber grabber = new VideoInputFrameGrabber(0);
try {
grabber.start();
Thread.sleep(1000);
while (true) {
IplImage img = grabber.grab();
if (img != null) {
cvSaveImage(webcamPath, img);
grabber.stop();
}
}
} catch (Exception e) {
e.printStackTrace();
}
But when the webcam starts working, it can't save the image and i'm getting this exception :
com.googlecode.javacv.FrameGrabber$Exception: videoInput is null. (Has start() been called?)
So is there any way to save the IplImage to a folder on the desktop?
Thanks.
In your code, this is the flow:
start the FrameGrabber
start loop
grab
stop the grabber
trying to grab again <<< exception occurs because grabber is not opened again
My guess is to place the open inside the loop:
FrameGrabber grabber = new VideoInputFrameGrabber(0);
try {
while (true) {
grabber.start();
Thread.sleep(1000);
IplImage img = grabber.grab();
if (img != null) {
cvSaveImage(webcamPath, img);
grabber.stop();
}
}
} catch (Exception e) {
e.printStackTrace();
}
Save your file to specific directory by adding:
cvSaveImage(<path>\\<imagename>, img);

Can't define a path to an image

I imported an image into Eclipse, in the same package as this class:
public class mainWindow extends JFrame {
public mainWindow() {
Image bg = // \mainPackage\ShittyPlane.png;
Graphics2D g2d;
this.setSize(500,500);
this.setResizable(false);
this.setTitle("GameTest");
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
this.setVisible(true);
g2d.drawImage(bg, 0, 0, null);
}
}
How do I define the image path?
If the image is part of you source and is packed into jar later for distribution i would sucgest you get a stream to the image using getResourceAsStream.
ClassLoader cl = getClass().getClassLoader();
InputStream is = cl.getResourceAsStream("mainPackage/ShittyPlane.png");
BufferedImage image = ImageIO.read(is);
this aproache will also work in if you run the program from your IDE
If you plan to locate the image using a a File chooser then go with #Pescis's answer.
What you need to do to load an image from a specific file is:
BufferedImage img = null;
try {
img = ImageIO.read(new File("src/mainPackage/ShittyPlane.png")); //I'm guessing this is the path to your image..
} catch (IOException e) {
}
For more info you can read the javadoc on working with images.

Convert BufferedImage to ImageIcon

How can I convert a BufferedImage to an ImageIcon?
I can not find any documentation on this.
Use constructor: ImageIcon(Image image), BufferedImage extends Image.
BufferedImage extends Image, so it's simply:
new ImageIcon(myBufferedImage);
File img = new File("C:\\..\\image.jpg");
BufferedImage bufferedImage = ImageIO.read(img);
ImageIcon imageIcon = new ImageIcon(bufferedImage);
your bufferimage;
JLabel photo = new JLabel("Photo");
photo.setIcon(new ImageIcon(bufferimage));
here is a simple converter:
//Your icon
private ImageIcon icon;
//Your image
private BufferedImage image;
public void bufferImageTocon(){
File file = new File("test.jpg");
try {
this.image = ImageIO.read(file);
} catch (IOException ex) {
ex.printStackTrace();
}
icon = new ImageIcon(image);
}
Use just the constructor to make a new icon, setting the buffered image as a parameter.

How to read OS X *.icns file with Java

I want to read *.icns files in OS X into a BufferedImage. Help
Try this: http://code.google.com/p/appengine-awt/source/browse/trunk/apache-sanselan/src/main/java/org/apache/sanselan/formats/icns/IcnsDecoder.java?spec=svn8&r=8
Which is actually from: http://incubator.apache.org/sanselan/site/index.html
You need to convert ICNS to another image type first, and after load this image you can delete it. This is how to convert PNG to ICNS, so you just need to do in the opposite way:
public static void Png(File png, File icns) throws IOException{
ImageIcon image = new ImageIcon(ImageIO.read(png));
ImageIconAs(image, icns);
}
public static void ImageIconAs(ImageIcon ii, File icns) throws IOException{IconAs((Icon)ii,icns);}
public static void IconAs(Icon icon, File icns) throws IOException{
if (icon != null) {
BufferedImage bi = new BufferedImage(icon.getIconWidth(), icon.getIconHeight(), BufferedImage.TYPE_INT_ARGB );
Graphics2D g = bi.createGraphics();
icon.paintIcon(new Canvas(), g, 0, 0 );
g.dispose();
File outputfile = new File("temp000.png");
ImageIO.write(bi, "png", outputfile);
execTerminal(new String[]{ "sips", "-s", "format", "tiff",
"temp000.png","--out", "temp000.tiff" });
File apaga2 = new File("temp000.png");
apaga2.delete();
execTerminal(new String[]{ "tiff2icns", "-noLarge",
"temp000.tiff", icns.getAbsolutePath()});
File apaga = new File("temp000.tiff");
apaga.delete();
}
}
static void execTerminal(String[] cmd){
int exitCode = 0;
try {
exitCode = Runtime.getRuntime().exec(cmd).waitFor();
}
catch (InterruptedException e) {e.printStackTrace();}
catch (IOException e) {
if (exitCode != 0) System.out.println("ln signaled an error with exit code " + exitCode);
}
}
You just need to use this to call the action:
Png(png_file,icns_file);
You can youse IconManager. It works with following icons formats:
*.ico - Windows Icon
*.icl - Windows Icon Library
*.icns - Macintosh Icon

Categories

Resources