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.
Related
Some years ago I took a couple of courses in programming. Now I'm in a situation where some very basic image processing might save me loads of time. Unfortunately I'm a bit stuck trying to read an image. I haven't done this before and basically just copied some code I found, but I get "javax.imageio.IIOException: Can't read input file!". I've tried moving the image "Test.jpg" and change the path. I don't understand what is causing the issue. I use macOS, not sure if this is relevant.
import java.io.File;
import java.io.IOException;
import java.awt.image.BufferedImage;
import javax.imageio.ImageIO;
public class MyImage{
public static void main(String args[])throws IOException{
BufferedImage image = null;
File f = null;
try {
f = new File("Users/simonprobert/eclipse-workspace/LineLengthCounter/src/Test.jpg");
image = new BufferedImage(100, 100, BufferedImage.TYPE_INT_ARGB);
image = ImageIO.read(f);
System.out.println("Reading complete.");
} catch(IOException e){
System.out.println("Error: "+e);
}
}
}
(Currently I'm just trying to read the image, the rest is not important at this point)
So I have a class that utilizes java's robot class to take a screen picture and make 5 smaller pictures from it.
import java.awt.AWTException;
import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.Toolkit;
import java.awt.image.BufferedImage;
import java.io.IOException;
import java.util.ArrayList;
public class screencap {
static Rectangle rectangle = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
public ArrayList<BufferedImage> get() throws AWTException, IOException {
ArrayList<BufferedImage> array = new ArrayList<BufferedImage>();
Robot robot = new Robot();
BufferedImage bufferedImage = robot.createScreenCapture(rectangle);
BufferedImage img1 = bufferedImage.getSubimage(481,1039,190,31);
BufferedImage img2 = bufferedImage.getSubimage(682,1039,190,31);
BufferedImage img3 = bufferedImage.getSubimage(883,1039,190,31);
BufferedImage img4 = bufferedImage.getSubimage(1085,1039,190,31);
BufferedImage img5 = bufferedImage.getSubimage(1286,1039,190,31);
array.add(img1);
array.add(img2);
array.add(img3);
array.add(img4);
array.add(img5);
return array;
}
}
So what I can do is use the class above to get an array of 5 buffered image like so
screencap nsc = new screencap();
try {
ArrayList<BufferedImage> bfl = nsc.get();
} catch (AWTException | IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
The problem that I encountered was that the code worked flawlessly when I ran it on eclipse but when I exported it as an executable jar file it didn't work and couldn't even throw an error. I'm pretty sure the code at fault here is screencap.get() but I don't know how to fix it. Can someone tell me what went wrong? Thanks in advance.
SOLVED: I turned off window scaling. Apparently it messes with my program somehow.
Also, when I execute the program from cmd it works even if window scaling is on
#ArnaudClaudel thank you for suggesting I use cmd
The reason my program didn't work is that I have window scaling turned on to 125%. It made my exported program look bigger than it supposed to be, making pictures look blurry and it messes with the function in the question. I fixed it by turning off window scaling. Another solution is to execute the jar file from the cmd.
I am participating in a project where we build an app which solves Rubiks' cubes. Initially we started with a desktop app using JavaFX but we decided to switch over to an Android app.
Since I already implemented a working model at least for color recognition, I wanted to reuse that and just build another UI around it. That is where I am stuck right now because I cannot even get Android's bitmap API to work. Unfortunately it looks like I need to stick with it since Swing/AWT/JavaFX image libraries are not available.
So I implemented a JUnit test which I cleaned up a bit:
package de.uniks.rubiksapp;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.robolectric.RobolectricTestRunner;
import org.robolectric.RuntimeEnvironment;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
#RunWith(RobolectricTestRunner.class)
public class TestBitmap {
#Test
public void testBitmap() {
Resources resources = RuntimeEnvironment.application.getResources();
InputStream testImageStream = resources.openRawResource(R.drawable.test_front);
Bitmap testImageBitmap = BitmapFactory.decodeStream(testImageStream);
//Bitmap testImageBitmap = BitmapFactory.decodeResource(resources, R.drawable.test_front);
// --- Pixel readout would be here --- //
System.out.println(testImageBitmap.getWidth() + "x" + testImageBitmap.getHeight() + "px");
FileOutputStream out = null;
try {
out = new FileOutputStream("test.png");
testImageBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
if (out != null) {
out.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
My problem currently is that the bitmaps I get from BitmapFactory seem to be corrupted. When I am trying to read specific pixels using Bitmap.getPixel(), they are always black. So I tried saving the image back to disk which works but the resulting files are only around 40 bytes large and cannot be opened.
Initially, I tried using Bitmap.decodeResource() instead of Bitmap.decodeStream() but that always returns images with a size of 100x100px, although my source images are 1240x800px large. Even when I use Bitmap.Options and set inScaled = false. At least the size is correct when using Bitmap.decodeStream().
Thanks for any help!
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);
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.