Difficulty in retrieving Image with URL in a Java .JAR - java

I'm attempting to retrieve an image for a Java Swing icon. I followed some advice here on SO to use URLs to retrieve it from a jar, but I am getting a null URL:
URL iconUrl = this.getClass().getResource("/buildings/"+buildingEnum.getColor().toString()+".png");
Toolkit tk = this.getToolkit();
Image img = tk.getImage(iconUrl);
My .JAR structure looks like this. I built it using Gradle and ShadowJar.
Both running it through IntelliJ and as an actual .jar in Powershell failed to retrieve it properly.
Is there something I'm missing? Please let me know if there's any additional information I need to provide.

Related

Java with ImageJ

I am using ImageJ in java code for doing segmentation of objects in images using LevelSet.
I imported the jar file called "levelsets.ij.LevelSet". But I get an error as 'unidentified command' in the string field 'Level Sets' in the run command.
Can someone help me to identify the error in my program?
When executing the following code the string parameter "Level Sets" in line 3 is not identified as a command. Can you kindly help?
ImagePlus imp = new ImagePlus("image_plus", img);
imp.setRoi(new OvalRoi(54,51,11,7));
IJ.run(imp,"Level Sets", "method=[Active Contours] use_level_sets
grey_value_threshold=50 distance_threshold=0.50 advection=2.20
propagation=1 curvature=1 grayscale=20 convergence=0.0050
region=outside");
img = imp.getBufferedImage();
Manage your project dependencies using Maven. Add a dependency on the sc.fiji:level_sets artifact.
I identified this artifact by pressing L to bring up the Command Finder, typing in "level" and then looking at the File column to see which JAR file the levelsets.ij.LevelSet class came from.
See also the Development page of the ImageJ wiki.

Java: Cannot access images in resources

I've been trying to export my project as a Runnable Jar, but my resources would not load because I was directly trying to access the image from my project's path. For example:
ImageIcon icon = new ImageIcon("resources/icon.png");
This would work when I ran the project from Eclipse itself, but I noticed that the images were not being included in the jar file. So after researching, I found out that I need to create source folders and put the images/text files inside them, and then use getClass().getResource() in order to access them. However, when I do this, the URL is always returned as null.
For reference, this is what my project explorer looks like:
Test
---src
---resources
---icon.png
---config
---file.ini
And here is the code that is giving me a NullPointerException when trying to access icon.png:
ImageIcon icon = getClass().getResource("/resources/icon.png");
Alternatively, I have also tried:
ImageIcon icon = getClass().getClassLoader().getResource("resources/icon.png");
But that also ends up in a NullPointerException. I have checked many solutions online but none of them have seemed to work for me. Please note that I also need to be able to access the .ini file, so a solution that only works for images won't fully solve my problem. Any help would be greatly appreciated, thanks.
Your structure needs to be:
TEST
-SRC
-Resources
-icon.png
The way your code is right now is there is no image because your code is referencing /src/resources not test/resources

How to add and open html file in non webapp maven project

I created a maven project which is bundle in Apache ServiceMix now.
Now i want to add html file to this project and print somethink on it.
How to do it? I try do this but when i want to open index.html i got an error:
URI u = new URI("/MyCommands/src/main/webapp/index.html");
Couldn't find file - in karaf console.
i tried URI u = new URI("index.html"); to, but it didnt work.
Please help me :(
Error log
Ok I guess you only need to specify the path correctly and judging from your project structure you need:
URI u = new URI("src/main/webapp/index.html");
The path /MyCommands/src/main/webapp/index.html that gives you the error signifies an absolute path in your machine. Does this correspond to your actual folder structure?
If you wanted a relative path (beginning from your source root folder you should not use the first forward slash /, something like MyCommands/src/main/webapp/index.html but in your case I think MyCommands/ is unnecessary.
Edit:
Copying from the Class Desktop documentation for browse() method :
IOException - if the user default browser is not found, or it fails to
be launched, or the default handler application failed to be launched
Judging from the above maybe you should check if your browser has some problem launching.
Can you open anyother URI instead of the one specified for example?

How to use JavaCv's cvSaveImage in android

I have created an app using javaCv and
I'm trying to save an image in android using
cvSaveImage("/storage/sdcard0/watermarked/test.jpg", yCrCb);
cvSaveImage("/storage/extSdCard/test.jpg",yCrCb);
where yCrCb is an IplImage.
There is no exception error and the program runs smoothly but the files are not saved into the path as mentioned above.
I would like to ask what might be the possible problems ? is it the naming convention of the file name ?
If it helps, I have a java application counterpart of this app and the java version works fine when i use the line
cvSaveImage("C:\\testing123.jpg", yCrCb);
Hi I have used same thing for storing the image.
For getting the path to sdcard, I have used the Environment.getExternalStorageDirectory().toString();
append the folder name to this path where you want to store the image
try this and also make sure that you have written permission in the xml file.

Reading Assets in Eclipse

I am developing a little game where I use some pictures for the sprites etc etc.
It works just fine when I load it from the disc like this
Image image = new Image("C:\\AppleMan.png");
but how can I load it from a foloder within the project. I am using eclipse as IDE and the language is Java :)
I took a screenshot of a sample project so you can see how I have importet the picture
So I want to load the picture from that resource folder like this pseudo code
Image image = getResource("Resources/AppleMan.png");
but that simply doesn't work.
Any help appreciated :)
1) You should add Resources folder to classpath
2) You should locate file absolutely, i.e. "/Resources/AppleMan.png"
P.S.
3) Sorry, also note that getResource returns URL: http://docs.oracle.com/javase/6/docs/api/java/lang/Class.html#getResource%28java.lang.String
You're passing a relative path when you were passing a absolute path before. Add the path of the Eclipse workspace.
For example, if your workspace is at C:\Workspace, you need to put
Image image = getResource("C:\\Workspace\\HowToGetResources\\Resources\\AppleMan.png");

Categories

Resources