469,332 Members | 6,821 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,332 developers. It's quick & easy.

ImageIO problems encoding jpeg

Hi group

I have some code that saves an image to a file using ImageIO. And it
works for all formats except jpeg. A scaled down version follows:

public void write(String filename) throws IOException {
int idx = filename.lastIndexOf('.');
if (idx >= 0) {
File file = new File(filename);
String format = filename.substring(idx + 1).toLowerCase();
ImageIO.write(image, format, file);
}
}

The image has been instanciated using:
image = new BufferedImage(w 0 ? w : 1, h 0 ? h : 1,
BufferedImage.TYPE_INT_ARGB);
....where 'w' is the width and 'h' is the height.

And given "img.png" I get a very nice PNG image. But "img.jpg" gives a
weird result.

The two images can be seen here (they are quite small):
http://194.255.21.180/img.png
http://194.255.21.180/img.jpg

Bigger versions can be seen here:
http://194.255.21.180/bigger.png
http://194.255.21.180/bigger.jpg

Firefox actually says the following when asked to display the JPEG:
<quote>
The image “http://194.255.21.180/img.jpg” cannot be displayed, because
it contains errors.
</quote>
....but The Gimp has no problem displaying it.

Has anybody seen this ?
Or do you know what I am doing wrong ?

Best,
Robert
Sep 18 '07 #1
5 6834
Robert Larsen wrote:
...
>I have some code that saves an image to a file using ImageIO. And it
works for all formats except jpeg. A scaled down version follows:
AN SSCCE is generally more useful than a 'scaled
down' version.
<http://www.physci.org/codes/sscce.html>
It would also be preferable to make an image
within the app. (e.g. a GradientPaint with a grid
drawn over the top), then use that as the test
case to write.
>The two images can be seen here (they are quite small):
http://194.255.21.180/img.png
http://194.255.21.180/img.jpg
I could not see the JPG. IE will not load it, and instead
displays the little 'missing' image box.
>Bigger versions can be seen here:
http://194.255.21.180/bigger.png
http://194.255.21.180/bigger.jpg
ditto.
>Has anybody seen this ?
Not yet.

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200709/1

Sep 18 '07 #2
Robert Larsen wrote:
...
image = new BufferedImage(w 0 ? w : 1, h 0 ? h : 1,
BufferedImage.TYPE_INT_ARGB);
As an aside, I do not believe JPEG supports any form of
transparency (Aplha). You might instead try..
<http://java.sun.com/javase/6/docs/ap...l#TYPE_INT_RGB
>
--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200709/1

Sep 18 '07 #3
Andrew Thompson wrote:
Robert Larsen wrote:
..
>I have some code that saves an image to a file using ImageIO. And it
works for all formats except jpeg. A scaled down version follows:

AN SSCCE is generally more useful than a 'scaled
down' version.
Done:

import java.awt.image.BufferedImage;
import java.io.*;
import javax.imageio.*;
import javax.swing.*;
import java.awt.*;

public class SSCCE extends JFrame {
private final static int WIDTH = 400;
private final static int HEIGHT = 400;

private BufferedImage image;

public SSCCE() {
super("Bad JPEG");
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
ImagePanel p = new ImagePanel(WIDTH, HEIGHT);
pack();
setSize(WIDTH + getInsets().left + getInsets().right, HEIGHT +
getInsets().top + getInsets().bottom);
getContentPane().add(p);
setVisible(true);
}

public void write(String filename) throws IOException {
int idx = filename.lastIndexOf('.');
if (idx >= 0) {
File file = new File(filename);
String format = filename.substring(idx + 1).toLowerCase();
ImageIO.write(image, format, file);
}
}

private class ImagePanel extends JPanel {
public ImagePanel(Dimension d) {
this(d.width, d.height);
}

public ImagePanel(int w, int h) {
setSize(w, h);
image = new BufferedImage(w 0 ? w : 1, h 0 ? h : 1,
BufferedImage.TYPE_INT_ARGB);
GradientPaint gp = new GradientPaint(0, 0, Color.blue, w, h,
Color.green);
Graphics2D g2d = image.createGraphics();
g2d.setPaint(gp);
g2d.fillRect(0, 0, w, h);
gp = new GradientPaint(0, 0, Color.green, w, h, Color.blue);
g2d.setPaint(gp);
g2d.fillArc(0, 0, w, h, 0, 360);
}

public void paintComponent(Graphics g) {
g.drawImage(image, 0, 0, this);
}
}

public static void main(String args[]) throws IOException {
SSCCE sscce = new SSCCE();
for (String filename : args) {
System.out.println("Saving to " + filename);
sscce.write(filename);
}
}
}
Usage:
robert-desktop:~/code/ToolboxNG $ javac SSCCE.java
robert-desktop:~/code/ToolboxNG $ java SSCCE test.png test.gif test.jpg
test.bmp
Saving to test.png
Saving to test.gif
Saving to test.jpg
Saving to test.bmp
robert-desktop:~/code/ToolboxNG $

This should pop up a window displaying the image as it should look.
The PNG and GIF look correct, but the JPG looks wrong (it CAN be
displayed using an image editor but Firefox and probably IE won't have
anything to do with it). The BMP file has a zero length.
Sep 18 '07 #4
Andrew Thompson wrote:
Robert Larsen wrote:
..
> image = new BufferedImage(w 0 ? w : 1, h 0 ? h : 1,
BufferedImage.TYPE_INT_ARGB);

As an aside, I do not believe JPEG supports any form of
transparency (Aplha). You might instead try..
<http://java.sun.com/javase/6/docs/ap...l#TYPE_INT_RGB
Well...that actually did it (blush).
Thou I would've expected the encoder to ignore alpha if it didn't
support it.

Anyway...thanks a lot
Sep 18 '07 #5
Robert Larsen wrote:
...
(JPEG)
>...You might instead try..
<http://java.sun.com/javase/6/docs/ap...l#TYPE_INT_RGB

Well...that actually did it (blush).
Cool (that it's fixed, not that it's embarrassing).
>Thou I would've expected the encoder to ignore alpha if it didn't
support it.
Aaahh (reminiscing) the 'Age of Innocence' - I vaguely recall it. ;-)

--
Andrew Thompson
http://www.athompson.info/andrew/

Message posted via JavaKB.com
http://www.javakb.com/Uwe/Forums.asp...neral/200709/1

Sep 18 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by Jan Schatz | last post: by
1 post views Thread by IndianOZ | last post: by
3 posts views Thread by Aaron Davies | last post: by
2 posts views Thread by Jure Erznoznik | last post: by
1 post views Thread by Slade | last post: by
7 posts views Thread by Brian | last post: by
reply views Thread by suresh191 | last post: by
1 post views Thread by haryvincent176 | last post: by
reply views Thread by Marylou17 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.