I have created code that makes print screens and saves them as an imiage, but I don't really know how to change the path of saving the file for other folder in my main project folder. Any ideas?
private static void print(JPanel comp, String nazwa) {
// Create a `BufferedImage` and create the its `Graphics`
BufferedImage image = GraphicsEnvironment.getLocalGraphicsEnvironment()
.getDefaultScreenDevice().getDefaultConfiguration()
.createCompatibleImage(comp.getWidth(), comp.getHeight());
Graphics graphics = image.createGraphics();
// Print to BufferedImage
comp.paint(graphics);
graphics.dispose();
// Output the `BufferedImage` via `ImageIO`
try {
ImageIO.write(image, "png", new File(nazwa+".png"));
} catch (IOException e) {
e.printStackTrace();
}
}
Write the full path in the File constructor:
new File("/home/cipek/images/filename.png")
However I have still got a problem. I am not sure that udnerstand it properly.
cipek- is my project name
images- folder where i want to keep images
But what about home?? I rewrite "home" however it doesn't work.I don't want to give whole path because I will use this proram on other computers so the path will be diffrent every time.
Related
I am making a program where the user chooses an image file on their computer and it displays it in a window. I have the line of code
image = ImageIO.read(getClass().getResourceAsStream(path));
where path is the absolute path to the image. The error it returns is
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
What am I doing wrong or is it not possible to get an Image outside of the project file?
So, getClass().getResourceAsStream() reads the image from default class path(either in the folder of your classes or the contents of the jar file)!
So you would want :
BufferedImage img = null;
try {
img = ImageIO.read(new File(path));
} catch (IOException e) {
}
I've looked around and have tried many fixes, but I can't seem to find one that works.
I'm making a simple frogger game, and I originally had all of my sprites loaded in the project directory like img/spritename.png:
image = ImageIO.read(getClass().getResourceAsStream("img/map.png"));
or
ImageIO.read(getClass().getResource(sprite));
Everything worked fine and dandy, but I went to move all of my images to an external directory so I can create an installer for it.
"C:\Program Files\DIR\img\" and all of the sprites no longer load.
The code I thought would fix it but didn't
BufferedImage image = null;
File dir = new File(Config.imgDir + "map.png");
try {
image = ImageIO.read(dir);
} catch (IOException e) {
e.printStackTrace();
}
if (image == null) {
return;
}
}
Config code:
public static String imgDir = "C:/Program Files/EmulousSoft/Frogger/img/";
Does anyone have any ideas? I'm sure it's simple, but every time I try different methods, I get "Value cannot be a null", "cannot find directory" or this.
In the code sample below, when I test the code in Eclipse it works just fine. However, when I export the jar file and test it via the command line, it throws an error: IIOException: Can't read input file!
private BufferedImage img = null;
private String imgSource;
if (img == null)
{
try {
URL url = getClass().getResource("Images/questionMark.png");
System.out.println(url.getPath());
/* This prints: file:/C:/Users/Keno/Documents/javaFile.jar!/javaFile/Images/questionMark.png */
File file = new File(url.getPath());
img = ImageIO.read(file);
imgSource = file.getName();
} catch (IOException e) {
e.printStackTrace();
}
}
The file I want to get is located inside the Images folder which is inside the javaFile package. I've noticed one thing that may indicate the problem.
In the print statement I have, I notice an exclamation sign at the end of the javaFile.jar section. Is that correct? Could that indicate an issue with the file or structure?
Also, just in case someone has a better suggestion as to how I should load the file, I'll tell you my intentions. I would like to load the file from a relative location (Images folder) in the jar. I would like to display it (Already done in my actual code) and also store the location to be passed later on to another function (also done).
try this
public void test() {
try(InputStream is = getClass().getResourceAsStream("Images/questionMark.png")) {
ImageIO.read(is);
} catch (IOException e) {
e.printStackTrace();
}
}
You should try to check if your class is in the same directory than Images inside your jar.
|
|- Your class
|- Images
|- questionMark.png
Also, have you tried using directly your url object ?
File file = new File(url);
Currently I have code like this in my program:
BufferedImage ReadPicture = null;
try {
ReadPicture = ImageIO.read(new File("C:/Users/John/Documents/NetBeansProjects/Program5/build/classes/Program5/Pictures/TestPicture.png"));
} catch (IOException e) {
}
If I compile my file to a jar and give it to someone else, the program does not work as the classpath is specific to my computer. How can I change how I access files/images so that it works on all computers?
For ImageIO in particular, if you always want to read an image from the classpath, without regard to what the classpath actually is, then you can do this:
BufferedImage readPicture = null;
URL imageUrl = getClass().getClassLoader().getResource(
"/Program5/files/Pictures/TestPicture.png");
// Or
// InputStream imageStream = getClass().getClassLoader().getResourceAsStream(
// "/Program5/files/Pictures/TestPicture.png");
// null if not found
try {
readPicture = ImageIO.read(imageUrl);
// null if the image format is unrecognized
} catch (IOException e) {
// ...
}
That relies on the fact that ImageIO can obtain images via URLs. This approach can be used even if the image is packaged in a Jar file, along side your classes (or not).
You can add a folder in your project named files or anything you want.You can make sub-directories in it and arrange files in that.They will be available when you will share it with others.In the code below,"." represents working directory.So make sure the directory structure you are providing,is correct.Try something like this.
BufferedImage ReadPicture = null;
try {
ReadPicture = ImageIO.read(new File("./files/Pictures/TestPicture.png"));
} catch (IOException e) {
}
See Also
Java Project Folder Structure
I'm using a simple way to get my resources for the project. I'm using Eclipse, and I have a 'res' folder to hold the needed files. This is how I load stuff, for example a 'puppy.png' just in my res folder (no subfolders):
String path = "/puppy.png";
try {
BufferedImage image = ImageIO.read(getClass().getResourceAsStream(path));
} catch(Exception ex) { ex.printStackTrace(); }
And sometimes I get an input==null error, and sometiomes not! Not like this time puppy.png loaded but next time it won't. For some classes it always loads correctly, and for the other classes I always get this error. Can anyone explain why can this happen, and how can I fix it, but still use the getResourceAsStream() method?
Please have a look at How to retrieve image from project folder?.
I have mentioned no of ways to read image from different paths.
You can try any one
// Read from same package
ImageIO.read(getClass().getResourceAsStream("c.png"));
// Read from absolute path
ImageIO.read(new File("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\c.png"));
// Read from images folder parallel to src in your project
ImageIO.read(new File("images\\c.jpg"));
In your case the image must be in the same package where is the class and don't prefix /.
Note that if the resource returns null (meaning it doesn't exist), you will get this error.
Check the input returned like so:
String path = "/puppy.png";
try {
InputStream is = getClass().getResourceAsStream(path);
if (is == null) {
//resource doesn't exist
} else {
BufferedImage image = ImageIO.read(is);
}
} catch(Exception ex) { ex.printStackTrace(); }
Note that you most likely should be using String path = "puppy.png", seeing as you will already be in the content of the project folder.