Java: Can't read Tiff image file [duplicate] - java

This question already has answers here:
Can't read and write a TIFF image file using Java ImageIO standard library
(5 answers)
Closed 5 years ago.
I'm trying to read an image from a relative path:
String fp = "../resources/img/wc/text/039.tiff";
The following code succeeds:
File fi = new File(getClass().getResource(fp).getPath());
System.out.println("fi: " + fi);
if (fi.exists() && !fi.isDirectory()) {
System.out.println("file exists"); // <-- console prints this
}
try {
img = ImageIO.read(getClass().getResource(fp));
System.out.println("file read"); // <-- console prints this
} catch (IOException e) {
e.printStackTrace();
}
... but the following code just after it:
System.out.println(img.getType());
... fails, reporting:
Exception in thread "main" java.lang.NullPointerException
at com.ddc.fmwscanner.java.LoadImageApp.ddNextImage(LoadImageApp.java:60)
at com.ddc.fmwscanner.java.LoadImageApp.<init>(LoadImageApp.java:85)
at com.ddc.fmwscanner.main.FmwScanner.main(FmwScanner.java:15)
I know the image is valid, because I can open it using non-Java methods. However, those methods will not open the image from a .jar, so I need to use a pure Java method.
Any insight is appreciated.

This ended up being a problem with loading .tiff files in pure Java. Installing TwelveMonkeys ImageIO plugin did the trick. Thanks again, especially to #IlarioPierbattista, who directed me to the solution!

Related

Image won't show up when using JAR file [duplicate]

This question already has answers here:
Jar get image as resource
(3 answers)
Closed 2 years ago.
So I'm using an image for the chess pieces in this basic chess game I'm making.
When I run the program in Eclipse, it works perfectly fine as expected. But when I use Eclipse to export and then run that program, it gives the error java.imageio.IIOException: Can't read input file!
The image is stored in the source folder in a package names images.
I load the image using
BufferedImage image = null;
try {
image = ImageIO.read(new File("src/images/Chess_Pieces.png"));
} catch (IOException e) {
System.out.println(e);
}
I've tried locating the image to many different places and I've tried different ways of loading the image, none of them work and I have made sure that the image actually appears correctly in the exported JAR file.
Change it:
image = ImageIO.read(getClass().getResource("/images/Chess_Pieces.png"));
See :
Different ways of loading a file as an InputStream
Loading image resource

Premature EOF while reading JPG using itext [duplicate]

This question already has an answer here:
Failure to read JPEG file from byte[]
(1 answer)
Closed 5 years ago.
When attempting to include some jpeg files into a PDF using iText I get an error:
Premature EOF while reading JPG
The images are loaded from android phones and most can be embedded into the pdf file, however some can not.
PushbuttonField ad = pdfForm.getNewPushbuttonFromField(fieldName);
if(ad != null) {
ad.setLayout(PushbuttonField.LAYOUT_ICON_ONLY);
ad.setProportionalIcon(true);
try {
ad.setImage(Image.getInstance(basePath + "/" + r.value));
} catch (Exception e) {
log.log(Level.SEVERE, "Image error detail", e);
}
pdfForm.replacePushbuttonField(fieldName, ad.getField());
}
The error occurs during setImage at: com.itextpdf.text.Jpeg.processParameters(Jpeg.java:219)
iText version is: 5.5.5
I have put an image that causes the error onto a public dropbox folder: https://dl.dropboxusercontent.com/u/46349359/image.jpg
The image is 1.6 MB and is displayed without a problem in html or using other image display tools.
As stated by Amedee this question was a duplicate. The specific resolution that has worked for me is to:
install imageMagick
Prior to adding image file image.jpg into a PDF using iText run:
"convert image.jpg image.jpg"
Then add the jpeg

How to add a URL to download a file in java [duplicate]

This question already has answers here:
How can I download and save a file from the Internet using Java?
(23 answers)
Closed 4 years ago.
I want to add a URL into my java program: http://www.markit.com/news/InterestRates_JPY_20160426.zip; so basically when you open this link a zip file is downloaded. How do I do that?
And then, I want to unzip the downloaded file in the java program as well.
How do I do these in java?
You can use zip4j to unzip your file.
To download a file in Java you can use this code.
try
{
String url = "download url";
String path = "C:/Users/...."; // Path to where the files is going to be downloaded.
ReadableByteChannel in = Channels.newChannel( new URL(url).openStream() );
FileOutputStream fileOutputStream = new FileOutputStream(path);
FileChannel out = fileOutputStream.getChannel();
out.transferFrom(in, 0, Long.MAX_VALUE);
}
catch (Exception e)
{
e.printStackTrace();
}

Can't find file error handling [duplicate]

This question already has answers here:
How to Check Path is existing or not in java?
(3 answers)
Closed 7 years ago.
Is there a particular way on how to handle errors if my FileInputStream cannot find the inputted file name in its directory? I'm just trying to make it so if my program can't find the file that was typed it'd print an error stating "file doesn't exist"
EDIT: Left out info. Hoping to let users reinput the file name.
Should be as simple as this
try {
<your code>
} catch(FileNotFoundException e) {
e.printStackTrace();
}
The catch does not end your program, you can easily put a loop around the whole block and try it with a different file name each time.
Nevertheless it would be better to test if the file exists, before you try to open it.
new File("filename").exits()

Reading Excel file safely using Apache POI [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
Java: Check if file is already open
I am making a swing utility in which i am creating and using a excel sheet by Apache poi.
If the file is already opened while i m trying to access it it throws an exception like some other process is using this excel file. So all i want is to check if that excel file is already opened and if yes then close it.
Did you do some Google search on this topic: Here is some I came up with
Java: Check if file is already open
check if a file is already open before trying to delete it
checking that an excel file is already opened by another application
I am copying the answer from the first question on stackoverflow here:
File file = new File(fileName);
FileChannel channel = new RandomAccessFile(file, "rw").getChannel();
// Get an exclusive lock on the whole file
FileLock lock = channel.lock();
try {
lock = channel.tryLock();
// Ok. You get the lock
} catch (OverlappingFileLockException e) {
// File is open by someone else
} finally {
lock.release();
}
Hope this will be of help.
Have you tried this:
File file = new File(pathToFile);
if (file.canRead()) {
<do whatever you are doing with the file>
}

Categories

Resources