Error while loading image - java

Here is my code:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.*;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import model.Map;
public class MyView {
private BufferedImage img = null;
private static MyPanel panel;
//init image
try{
img = ImageIO.read(new File("/src/minepic/start.png"));
} catch (IOException e){
System.out.println(e.getMessage());
}
}
I want to load an PNG image from the src directory but I don't know why it doesn't work, anyone can help me?
Error in command "try catch" and NetBeans say "unreported exception IOexception; must be caught or declared to be thrown"
One more, even i declared img as a BufferedImage before, but in command "try catch" img just like didn't declared because in NetBeans it doesn't become green, still black.

There are few issues with the code:
It is incomplete. The braces are not matched.
you have written code outside main method (possible but not recommended)
To read the image from src folder (which is a part of your class path) use the below snippet:
Inputstream is = MyView.class.getResourceAsStream("minepic/start.png");
if(is==null){
is = MyView.class.getClassLoader().getResourceAsStream("minepic/start.png");
}
img = ImageIO.read(is);

Related

ImageIO.read() infinite loop

ImageIO.read() just seems to be stuck in an infinite loop.
My code:
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
public class Texture {
BufferedImage _img;
public Texture(String path) {
try {
_img = ImageIO.read(new File(path));
} catch (final IOException e) {
e.printStackTrace();
}
}
}
Other class:
private Texture _tex;
_tex = new Texture("/res/img.png");
I have tried loading the image this URL and File, none works.
I am using eclipse, on a mac, and I am using LWJGL 3 if that has anything to do with anything.
Hope you can help me! :-)
Instead of using a File, try using an input stream like this:
InputStream stream = Texture.class.getResourceAsStream(path);
_img = ImageIO.read(stream);
I'm actually experiencing a similar issue, but it's related to the creation of BufferedImages.

Java jar File error message Exception in thread "AWT-EventQueue-0"

I'm trying to learn how to program frames in Java and I ran into a problem. When I create a jar file of my code I come up with a blank screen. My previous jar files don't have the same problem. When I ran the jar file through command prompt I got the following error message:
Exception in thread "AWT-EventQueue-0" java.lang.IllegalArgumentException: input == null!
at javax.imageio.ImageIO.read(Unknown Source)
at getResources.getImg(getResources.java:31)
at MyCanvas.paint(gameLoop.java:99)
at javax.swing.JComponent.paintChildren(Unknown Source)
at javax.swing.JComponent.paint(Unknown Source)
…
Here is the code for the main class and MyCanvas class:
class MyCanvas extends JComponent{
public void paint(Graphics g){
getResources get = new getResources();
BufferedImage titleImg = get.getImg("Title");
BufferedImage testImg = get.getImage("");
g.drawImage(titleImg, 0, 0, null);
g.drawImage(testImg, 0 , 0, null);
}
}
Here is the code for my "getResources" class:
import java.awt.Graphics;
import javax.swing.JComponent;
import javax.swing.JFrame;
import java.awt.Color;
import java.awt.Dimension;
import javax.swing.JPanel;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.Font;
import sun.audio.AudioData;
import sun.audio.AudioPlayer;
import sun.audio.AudioStream;
/**
* Write a description of class getResources here.
*
* #author (your name)
* #version (a version number or a date)
*/
public class getResources{
public void getResources(){
}
public BufferedImage getImg(String path){
BufferedImage img = null;
try {
img = ImageIO.read(this.getClass().getResource("/Sprites/" + path + ".png"));
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
public BufferedImage getImage(String path){
BufferedImage img = null;
try {
img = ImageIO.read(this.getClass().getResource("/Sprites/test.png"));
} catch (IOException e) {
e.printStackTrace();
}
return img;
}
public AudioStream getSeven(){
InputStream in;
AudioStream as = null;
try{
in = this.getClass().getResourceAsStream("/Music/seven.wav");
as = new AudioStream (in);
}catch(Exception e){
e.printStackTrace();
}
return as;
}
}
I am pretty sure that the error is in the "getImg" method and the way it retrieves images from the source folder. I removed all the lines of codes that had references to that method and the jar file ran just fine. However, the "getImage" class runs just fine in the jar file. Maybe something about passing a string to get the path of the image creates the error? I'd like to know if anyone knows what is causing the error and if I should even be concerned by not being able to run jar files, I do like using them.

Can not process the image on unix from opencv but same program works on windows

I have tested my program on windows platform, works perfectly. But when I try to run it on Raspbian, Unix, it doesn't go further after iplcvLoadimage.
What could be the problem?
Here is my code
mport com.googlecode.javacv.cpp.opencv_core.CvPoint;
import com.googlecode.javacv.cpp.opencv_core.IplImage;
import static com.googlecode.javacv.cpp.opencv_core.cvSize;
import static com.googlecode.javacv.cpp.opencv_core.cvZero;
import static com.googlecode.javacv.cpp.opencv_core.cvMinMaxLoc;
import static com.googlecode.javacv.cpp.opencv_core.IPL_DEPTH_32F;
import static com.googlecode.javacv.cpp.opencv_core.cvCreateImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.cvMatchTemplate;
import static com.googlecode.javacv.cpp.opencv_highgui.cvLoadImage;
import static com.googlecode.javacv.cpp.opencv_imgproc.CV_TM_SQDIFF;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import org.opencv.core.Core;
public class MatchTemplateTest {
public static HashMap<String,Double> getPatternMatch(String img)
{
ArrayList<String>names=new ArrayList<>();
HashMap<String,Double> MatchMap=new HashMap<>();
File []f=null;
try
{
String path=new File(new File(".").getCanonicalPath()+"/Output"+"/Logo").getAbsolutePath();
f=new File(path).listFiles();
} catch (Exception e) {
}
IplImage src=null;
try {
// this is where the problem is
src= cvLoadImage(img,0);
// can not process further
} catch (Exception e) {
System.out.println("ERROR "+e);
}
If the problem is with reading the image, then there are two possiblities
The image does not exist, or the path to the image is wrong. In *nix the path is separated with a slash '/', e.g. /home/asharma/data/myimage.pgm, but in windows it is separated with a backslash, e.g. C:\User\Data\My Image.pgm
The image decoder for the image does not exist in the version of the library. For example, if the image is in PNG format, but opencv was not compiled with PNG, then you cannot read the image.
what is the output of System.out.println("ERROR "+e);?

Netbeans cannot find java.io.InputStream

Actually, he cannot find java.io.
It recognizes import java.io.*;
import java.io.IOException;
but when I try InputStream = this.getClass().getResourceAsStream("users.xml");, i get the following error:
C:\Users\cpantaziu\Documents\NetBeansProjects\url\src\main\java\com\mkyong\common\controller\FailRegisterController.java:[120,12] cannot find symbol
symbol : variable InputStream.
Tools -> Java Platforms ses my jdk 1.6.
Why am I getting this?
you have to assign a reference variable:
InputStream ref= this.getClass().getResourceAsStream("users.xml");
You must name a variable:
InputStream is = this.getClas()...
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.io.InputStream;
import javax.imageio.ImageIO;
/**
*
* #author mbarb
*/
public class LoadSave {
public static BufferedImage GetPlayerAtlas(){
BufferedImage img = null;
InputStream is =
LoadSave.class.getResourceAsStream("Res/player2.png");
try {
img = ImageIO.read(is);
} catch (IOException e) {
e.printStackTrace();
}//try catch .close()
return img;
}
}
I'm have the same issue here in the code above.
i just get this:
Exception in thread "main" java.lang.IllegalArgumentException: input == null!
at java.desktop/javax.imageio.ImageIO.read(ImageIO.java:1358)
at Utilities.LoadSave.GetPlayerAtlas(LoadSave.java:19)
at Entities.Player.loadAnimations(Player.java:102)
at Entities.Player.<init>(Player.java:29)
at com.mycompany.platformgame.Game.initClasses(Game.java:32)
at com.mycompany.platformgame.Game.<init>(Game.java:21)
at com.mycompany.platformgame.PlatformGame.main(PlatformGame.java:7)
Command execution failed.

Loading an image

This is the code I'm using to load an image called "boat.png"
#Override
public void paint(Graphics g) {
BufferedImage img = null;
try {
img = ImageIO.read(new File("boat.png"));
} catch (IOException e) {
System.out.println("Can't load the image");
}
}
These two lines generate errors
BufferedImage img = null;
img = ImageIO.read(new File("boat.png"));
Although I've included
import java.io.*;
import java.awt.*;
Please help me find the error !!
The ImageIO class is in the javax.imageio package.
Try importing that package:
import javax.imageio.ImageIO;
I believe the problem is that BufferedImage is in the java.awt.image package, not just java.awt, so you need:
import java.awt.image.*;
or
import java.awt.image.BufferedImage;
Some IDEs will help you fix this error by suggesting which package to import - Eclipse certainly does.
EDIT: You also need to import javax.imageio.* or javax.imageio.ImageIO - but you definitely need one of the earlier imports too...
import java.awt.image.BufferedImage
this import statement should be included.

Categories

Resources