Premature EOF while reading JPG using itext [duplicate] - java

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

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

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

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!

how to use GhostScript to convert a pdf to jpg

It bothered me a whole day afternoon.
I encounter many problems and by now i cannot overcome them.
my code:
public void pdf2jpg(){
try {
File pdfFile =new File("c://tmp//1.pdf");
PDFDocument document = new PDFDocument();
document.load(pdfFile);
SimpleRenderer renderer = new SimpleRenderer();
renderer.setResolution(300);
List<Image> images = renderer.render(document);
for (int i = 0; i < images.size(); i++) {
Image img= images.get(i);
ImageIO.write((RenderedImage)img, "jpg", new File(i+".jpg"));
}
} catch (IOException | RendererException | DocumentException e) {
e.printStackTrace();
}
}
My box: Windows 7, jdk:1.7.0_45(64bit), GPL ghostscript 9.0.4.
when I use ghost4j 0.4.4, I got error "net.sf.ghost4j.renderer.RendererException: net.sf.ghost4j.GhostscriptException: Cannot initialize Ghostscript interpreter. Error code is -20"
on List<Image> images = renderer.render(document);
Some thread here mentions it's about ghost4j version. so i change to 0.4.6, error 20 disappears, but comes Warning: An error occurred while reading an XREF table. on the same sentence.
I cannot figure out how to get out of this 'mud',
Thanks very much for your help.
I'd guess that the returned bitmap is simply too large for memory, given that you get an out of memory error.
You should try using Ghostscript directly from the command line for 2 reasons, firstly you will be able to see if there is a real error message about the xref, which would indicate your PDF file is damaged, secondly you might reasonably just run a shell command to invoke GhostScript to render the PDF directly to JPEG rather than going through an in-memory bitmap. Its probably faster apart from anything else.
gswin32c -sDEVICE=jpeg -o out.jpg input.pdf

Unable to load image from the same package in Java?

In my java package, I have a file called 'prog.ico'. I'm trying to load this file, via the following code:
java.net.URL url = this.getClass().getResource("prog.ico");
java.awt.Image image = ImageIO.read( url );
System.out.println("image: " + image);
This gives the output:
image: null
What am I doing wrong? The .ico file exists in the same package as the class from which I'm running this code.
It seems that the .ico image format is not supported. See this question and it's answer to get around this.
To prevent link rot: This solution recommends using Image4J to process .ico files.
I've written a plugin for ImageIO that adds support for .ICO (MS Windows Icon) and .CUR (MS Windows Cursor) formats.
You can get it from GitHub here: https://github.com/haraldk/TwelveMonkeys/tree/master/imageio/imageio-ico
After you have it installed the plugin, you should be able to read your icon using the code in your original post.
I thing you must go over FileInputStream to wrap the file
File file = new File("prog.ico");
FileInputStream fis = new FileInputStream(file);
BufferedImage image = ImageIO.read(fis); //reading the image file

Java read different type of image formats jpg,tif,gif,png

I am trying to read some image files jpg, tif, gif, png and need to save files and create icons.
And i am getting UnsupportedTypeException.
ImageIO.read(file);
If i use following line, as earlier discuss in form.
BufferedImage img = JPEGCodec.createJPEGDecoder(inputStream).decodeAsBufferedImage();
I get JPEGCodec cannot found symbol.
I am using netbean 7.0.1. I have also added jai-imageio.jar.
By default, ImageIO can only read JPG, GIF and PNG file formats, if I remember right. To add new formats like TIFF, you need to add a plugin, which is a jar file, to your classpath, and to add an ImageIO.scanForPlugins() to you code before you try to read a file.
Example of plugin:
http://ij-plugins.sourceforge.net/plugins/imageio/
Try "ImageIO plugins" in Google.
JAI-ImageIO does include plugins for file formats like TIFF, so in principal what you are trying to do should work. However, to install JAI-ImageIO it's not enough to add it to your classpath. See the complete installation instructions here: http://java.sun.com/products/java-media/jai/INSTALL-jai_imageio_1_0_01.html
Fr detail we can see the like
http://www.randelshofer.ch/blog/2011/08/reading-cmyk-jpeg-images-with-java-imageio/
Image img = null;
ImageInputStream iis = new FileImageInputStream(file);
try {
for (Iterator<ImageReader> i = ImageIO.getImageReaders(iis);
img == null && i.hasNext(); ) {
ImageReader r = i.next();
try {
r.setInput(iis);
img = r.read(0);
} catch (IOException e) {}
}
} finally {
iis.close();
}
return img;
Java advance image io also solve the problem, but its hard to maintain to install on all plateform.

Categories

Resources