How to load an image to java application assets folder - java

I'm going to make a java application with nifty-gui[1] using java web start
When user click on upload image, a file chooser is showed (FileOpenService)
After that, image should be visible in image control.
Unfortunately, nifty-gui can use images only from assets.
How to download this image into asset? Maybe there is a way to modify actual image using data from FileContent class?

This is solution, created after discussion [1]
assetManager.registerLoader(AWTLoader.class, "jpg");
assetManager.registerLocator("/", FileLocator.class);
image(new ImageBuilder() {{
filename("/home/marek/photo2.jpg");
}});

Related

Java web application need to refresh to "see" resource files

I'm working on a web based application with Spring MVC and Thymeleaf. I have form which upload image. I decided to save the images in src/main/resources/static/images/ and that works perfect. But I found an issue with upload. When I save new image and try to display it in the HTML it's now working. When i refresh the images folder from STS and refresh the page it works fine. Can you give me some advice why this is happening and is there a way to fix it? I know i can use java code to get image content and display it as a resource but i would like to use thymeleaf's EL. I display the image with #{/images/imagename.jpg}
Best regards,
Peter
The thing is, if your resources are within your web app, you won't be able to display it and change it at will. When you refresh your images folder on STS, it "redeploys" the resources and you can see your images.
What you need to do is to put your static/images folder outside your web app, then you'll be able to easily upload a new image and have it displayed on your web app

Java isn't reading a JPG image where as a photo viewer can read it

I have a scraped few images from a website. The scraper did not download the image files with the correct extension.
I changed one of the file's extension to ".jpg" and it opened in Picasa. So I wrote a Java program to change all downloaded image file extensions to ".jpg".
Now when I tried to open images in Java, using Swing, it's not displaying the image. I am using this code to display. It is working fine with other images downloaded manually from web.
String path = "resources\\images\\"+gamePlayer.getName()+".jpg";
JLabel image = new JLabel(new ImageIcon(path));
I even tried converting image files to a different format by a third part software(Format Factory) but even it couldn't read the file.
I can open and view all the images in Picasa.
How do I display scraped images with incorrect extensions using Swing ?
Picassa is probably inspecting the file signature (https://en.wikipedia.org/wiki/List_of_file_signatures) rather than the file extension and therefore opens it correctly. You can't just rename a file and expect your code to understand how to open/display it.

How to use Image in JRadioButtons instead of text

I have a group of JRadioButtons. I want to use image instead of text in my JRadioButtons. So I believe i will have to use this:
JRadioButton(Icon icon, boolean selected)
Now the issue is that I am not sure about how to create this icon. I have the image that I want to use and I have copied the image in my source code folder. It is in .tiff format. i want to read that .tiff image (inputStream I believe) and convert that to icon so that i can have my JRadioButtons.
Please help in implementing this.
Thanks in advance.
Assuming you put the image in your source folder, in the package com.foo.bar, and that your build process copies this file with your classes so that it's in the classpath when running the application (that's what all IDEs do by default), you can just use
new ImageIcon(MyClass.class.getResource("/com/foo/bar/MyImage.png"))
to get you an icon.
I'm not sure that Java has native support for the tiff format, so you might have to convert the image to another supported format to load it (gif, JPEG and PNG will work fine).
If you're getting a NullPointerException, it probably means that the image is not at the path you're indicating.
You said you stuck it right into the src folder so this should work:
new ImageIcon(getClass().getResource("icon.jpg"))

java.lang.reflect.InvocationTargetException

I have an applet with some buttons in it, the buttons have image icons. I also have made an HTML file with this applet. Whenever I open this page from server (Apache tomcat) an exception occurs:
java.lang.reflect.InvocationTargetException.
But if I run without the icons, there are no problems. Can anyone help me, so that I can load the applet with the button icons?
Image myImage = getToolkit().createImage("image/REC1.jpg");
ImageIcon myIcon = new ImageIcon(myImage);
button.setIcon(myIcon);
Toolkit.createImage(String):
Returns an image which gets pixel data from the specified file. The returned Image is a new object which will not be shared with any other caller of this method or its getImage variant.
This method first checks if there is a security manager installed. If so, the method calls the security manager's checkRead method with the specified file to ensure that the image creation is allowed.
I made 2 parts bold:
File - only ever points to the local file-system of the computer on which the applet is running. So are unsuitable for icons in the applet - they must be on the server. And..
Security Manager - which will not allow a sand-boxed applets to read or write to the local file-system. That might be the important part of the stack trace that is missing.
Applet resource access
Applets need to load resources from the 'home server' if sand-boxed, by URL.
The icons might be an embedded resource, inside a Jar referenced in the archive attribute of the applet element, but if they are loaded to the server as images, they can be accessed relative to the document base or code base. Here is what it might look like for an image named REC1.jpg in the image sub-directory of the directory that contains the HTML.
URL url = new URL(getDocumentBase(), "image/REC1.jpg");
Image myImage = getToolkit().createImage(url);
you can use .PNG file format also. and used small letter for file name.

Cannot show local images

I am using GWT and I am using Image Widget to view an image. This image is located in my file system.
I wrote the following line of code:
String src = "file:///D:/myfolder/myfile.jpg";
Image image = new Image();
image.setUrl(src);
Please note I need to show only local images; not from the server. It may sound strange but I need to show from the client machine. Assume all clients will have same image and same path.
Thanks.
I believe that what are you trying is not possible due to "Same Origin Policy" in browsers: Same Origin Policy Wikipedia article.
It's very unusual that you have to specify full path with drive name.
There are many ways to get your context path and then make the path relative to it.
I've just tried to setUrl to the GWT's Image in your way. Everything was fine.
Try to check your path, image extension etc.
Maybe your do something wrong when add image to the layout?

Categories