I want to create a Java class that I use to export images. This class will be exported in .JAR file which will allow me to integrate it directly into another project by passing as input parameter the name of the file and in return I would have this image in PNG format.
That's what I tried:
package militarySymbolsLibrary;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class MainEntry {
public static void main(String agrs[]) {
}
public static BufferedImage generateImage(String symbolCode) {
File imageFile = new File("/pictures/SSGPU-------.png");
BufferedImage image;
try {
image = ImageIO.read(imageFile);
} catch (IOException e) {
image = null;
e.printStackTrace();
}
return image;
}
}
Pictures are stocked in folder pictures like this treeview below:
src
MainEntry.java
pictures
SSGPU-------.png
Then I export this class in .JAR file, I add it to my other program, I pass a parameter (picture's name except for this test) and normally my class return PNG file. But I have an error telling me that the file path is not the rigth one.
How can I solve this problem?
Thank you
I'm new to java.
I am trying to use Java Advanced Imaging to read an Image file as explained in here http://www.oracle.com/technetwork/java/iio-141084.html
import java.awt.image.RenderedImage;
import javax.media.jai.JAI;
public class ImageGetterJAI {
public static void main(String[] args)
{
//image source
String imagedir = "C:\\Users\\Emre\\Desktop\\Image\\Grass.tif";
//get the image
RenderedImage image = JAI.create("imageload", imagedir);
}
}
But I get JAI cannot be resolved error. Am I doing a fundamental mistake.
Screenshot of the project is below, hope this helps.
I have a requirement to capture photos using system webcam. For this, I'm using webcam capture library. It contained three jar files. I wanted to package every thing including dependency jars in single jar application.
So, I sorted to unpack the dependent jar files place them in the src folder of netbeans project.
So, I extracted them. Two of them start with the same package name org, so I extracted and merged them. Important third package starts with com package. I've placed these three directories in src folder of my netbeans project.
Now the problem is that netbeans, says that package doesn't exist. But, the same project when compiled from command line in windows. It compiled and ran successfully, everything was working fine.
But, when I added the problematic library as a jar to the project, it worked fine, only extracting and placing it in src folder causing the problem.
Why is this error occurring and why in netbeans only, how to resolve this problem?
My code:
package screen;
import com.github.sarxos.webcam.*; **//Error here**
import com.github.sarxos.webcam.log.*; **// Error here**
import java.io.*;
import javax.imageio.ImageIO;
import java.awt.image.BufferedImage;
import java.util.Date;
import java.util.Timer;
import java.util.TimerTask;
public class Util extends TimerTask{
private String filePath;
private String type;
private Timer timer;
private Webcam webcam;
public Util(String path,String type){
this.timer=new Timer("Capture Timer");
this.filePath=path;
this.type=type;
this.webcam=Webcam.getDefault();
}
public void capture() throws IOException{
if(webcam!=null){
webcam.open();
BufferedImage image=webcam.getImage();
Date date=new Date();
ImageIO.write(image,type,new File(filePath+"_"+date.toString().replace(' ','_').replace(':','-')+"."+type.toLowerCase()));
webcam.close();
}
else{
webcam=Webcam.getDefault();
}
}
public void startCapturing(int period){
this.timer.schedule(this,0,period);
}
public void stopCapturing(){
timer.cancel();
}
public void run(){
try{
capture();
System.out.println("Doing");
}catch(Exception e){
e.printStackTrace();
}
}
public static void main(String args[]){
Util u=new Util("n082430_","PNG");
u.startCapturing(60000);
}
}
Problem: Java applet can't load resources located inside its jar when run locally on windows platforms. The same applet can load the resources if it is launched from a web server instead of launched locally or if it's launched locally on a linux system. In all cases the applet is launched using an applet tag.
Steps to reproduce
1) Build applet class code below and create a jar containing the following:
TestApplet.class
iconimg.png
test.html
META-INF folder (standard manifest with one line: "Manifest-Version: 1.0")
Here's a link to the image png file I used:
http://flexibleretirementplanner.com/java/java-test/iconimg.png
The file test.html has one line:
<h1>Text from test.html file</h1>
2) create launch.html in same folder as test.jar as follows:
<html><center><title>Test Applet</title><applet
archive = "test.jar"
code = "TestApplet.class"
name = "Test Applet"
width = "250"
height = "150"
hspace = "0"
vspace = "0"
align = "middle"
mayscript = "true"
></applet></center></html>
3) With test.jar in the same local folder as launch.html, click on launch.html
4) Notice that getResource() calls for imgicon.png and test.html both return null.
5) Upload launch.html and test.jar to a web server and load launch.html and notice that the resources are found.
TestApplet.java
import java.applet.AppletContext;
import java.io.IOException;
import java.net.URL;
import javax.swing.ImageIcon;
import javax.swing.JApplet;
import javax.swing.JEditorPane;
import javax.swing.JLabel;
import javax.swing.JPanel;
public class TestApplet extends JApplet {
public TestApplet() {
try {
jbInit();
} catch (Exception ex) {
ex.printStackTrace();
}
}
public void init() {
JPanel topPanel = new JPanel();
JLabel iconLabel;
URL url = TestApplet.class.getClassLoader().getResource("iconimg.png");
if (url != null)
iconLabel = new JLabel(new ImageIcon(url));
else
iconLabel = new JLabel("getResource(iconimg.png)==null");
topPanel.add(iconLabel);
URL url2;
url2 = TestApplet.class.getClassLoader().getResource("test.html");
if (url2 == null) {
JLabel errorLabel = new JLabel("getResource(test.html) == null");
topPanel.add(errorLabel);
} else {
try {
JEditorPane htmlPane = new JEditorPane(url2);
topPanel.add(htmlPane);
} catch (IOException ioe) {
System.err.println("Error displaying " + url2);
}
}
getContentPane().add(topPanel);
}
private void jbInit() throws Exception { }
}
Oracle has decided to modify the behavior of getDocumentBase(), getCodeBase() and getResource() because security reasons since 1.7.0_25 on Windows: http://www.duckware.com/tech/java-security-clusterfuck.html
It seems there is a lot of discussion about this change because it breaks some important valid and secure use cases.
After further research and discovering that this is a windows-only problem, I'm calling this one answered.
It's almost certainly a java 1.7.0.25 bug. The applet runs fine from a web server and also runs fine locally on a virtual Ubuntu system (using VirtualBox on windows). Hopefully the bug report i submitted will be helpful to the java folks.
Thanks for the responses. Btw, it was Joop's comment about case sensitivity that spurred me to test a linux system just for kicks. Thanks for that!
I tried this way, but it didnt changed?
ImageIcon icon = new ImageIcon("C:\\Documents and Settings\\Desktop\\favicon(1).ico");
frame.setIconImage(icon.getImage());
Better use a .png file; .ico is Windows specific. And better to not use a file, but a class resource (can be packed in the jar of the application).
URL iconURL = getClass().getResource("/some/package/favicon.png");
// iconURL is null when not found
ImageIcon icon = new ImageIcon(iconURL);
frame.setIconImage(icon.getImage());
Though you might even think of using setIconImages for the icon in several sizes.
Try putting your images in a separate folder outside of your src folder. Then, use ImageIO to load your images. It should look like this:
frame.setIconImage(ImageIO.read(new File("res/icon.png")));
Finally I found the main issue in setting the jframe icon. Here is my code. It is similar to other codes but here are few things to mind the game.
this.setIconImage(new ImageIcon(getClass().getResource("Icon.png")).getImage());
1) Put this code in jframe WindowOpened event
2) Put Image in main folder where all of your form and java files are created e.g.
src\ myproject\ myFrame.form
src\ myproject\ myFrame.java
src\ myproject\ OtherFrame.form
src\ myproject\ OtherFrame.java
src\ myproject\ Icon.png
3) And most important that name of file is case sensitive that is icon.png won't work but Icon.png.
this way your icon will be there even after finally building your project.
This works for me.
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(".\\res\\icon.png"));
For the export jar file, you need to configure the build path to include the res folder and use the following codes.
URL url = Main.class.getResource("/icon.png");
frame.setIconImage(Toolkit.getDefaultToolkit().getImage(url));
Yon can try following way,
myFrame.setIconImage(Toolkit.getDefaultToolkit().getImage("Icon.png"));
Here is the code I use to set the Icon of a JFrame
import javax.imageio.ImageIO;
import java.io.File;
import java.io.IOException;
try{
setIconImage(ImageIO.read(new File("res/images/icons/appIcon_Black.png")));
}
catch (IOException e){
e.printStackTrace();
}
Just copy these few lines of code in your code and replace "imgURL" with Image(you want to set as jframe icon) location.
JFrame.setDefaultLookAndFeelDecorated(true);
//Create the frame.
JFrame frame = new JFrame("A window");
//Set the frame icon to an image loaded from a file.
frame.setIconImage(new ImageIcon(imgURL).getImage());
I'm using the following utility class to set the icon for JFrame and JDialog instances:
import java.awt.*;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Scanner;
public class WindowUtilities
{
public static void setIconImage(Window window)
{
window.setIconImage(Toolkit.getDefaultToolkit().getImage(WindowUtilities.class.getResource("/Icon.jpg")));
}
public static String resourceToString(String filePath) throws IOException, URISyntaxException
{
InputStream inputStream = WindowUtilities.class.getClassLoader().getResourceAsStream(filePath);
return toString(inputStream);
}
// http://stackoverflow.com/a/5445161/3764804
private static String toString(InputStream inputStream)
{
try (Scanner scanner = new Scanner(inputStream, "UTF-8").useDelimiter("\\A"))
{
return scanner.hasNext() ? scanner.next() : "";
}
}
}
So using this becomes as simple as calling
WindowUtilities.setIconImage(this);
somewhere inside your class extending a JFrame.
The Icon.jpg has to be located in myproject\src\main\resources when using Maven for instance.
I use Maven and have the structure of the project, which was created by entering the command:
mvn archetype:generate
The required file icon.png must be put in the src/main/resources folder of your maven project.
Then you can use the next lines inside your project:
ImageIcon img = new ImageIcon(getClass().getClassLoader().getResource("./icon.png"));
setIconImage(img.getImage());
My project code is as below:
private void setIcon() {
setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/slip/images/cage_settings.png")));
}
frame.setIconImage(new ImageIcon(URL).getImage());
/*
frame is JFrame
setIcon method, set a new icon at your frame
new ImageIcon make a new instance of class (so you can get a new icon from the url that you give)
at last getImage returns the icon you need
it is a "fast" way to make an icon, for me it is helpful because it is one line of code
*/
public FaceDetection() {
initComponents();
//Adding Frame Icon
try {
this.setIconImage(ImageIO.read(new File("WASP.png")));
} catch (IOException ex) {
Logger.getLogger(FaceDetection.class.getName()).log(Level.SEVERE, null, ex);
}
}'
this works for me.