473,396 Members | 1,599 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

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 7096
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Jan Schatz | last post by:
Hi, I have an application sending JPEGs via a TCP connection. By now I used a Delphi program on the other side to receive the pics. Now I want to create a Java applet that does the work of the...
1
by: IndianOZ | last post by:
Hi , I'm trying to send an Image over the Socket Connection (from the Client using winsock, which is a VB based client) and at the other end I'm reading the Stream (at the Server which is java...
3
by: Aaron Davies | last post by:
I'm trying to write some classes for my collaborative whiteboard to transmit images over the network, using the javax.imageio API, but I'm running into an odd problem: my code words fine with PNGs,...
1
by: Patrick | last post by:
Hello all! I am using a BufferedImage object to build an image from scratch. I want it to be a grayscale image with only 8bits of color. I have the color information as a byte, saved in variable...
2
by: Jure Erznoznik | last post by:
Hi guys, i have a really stupid problem with this line of code: location.href = "showreport.php?id=" + sText; sText is an id of a job that's running on the server. Showreport.php retrieves...
4
by: Laszlo Szijarto | last post by:
anyone know of a JPEG 2000 encoding / decoding library that works with .NET? Thank you, Laszlo
1
by: Slade | last post by:
Hi, I'm trying to use POST an image to a web page with WebRequest/WebResponse. Only problem is that I must be making an error somewhere in the encoding/decoding process. I've pasted below a bit...
7
by: Brian | last post by:
First off, I am sorry for cluttering this group with my inept questions, but I am stuck again despite a few hours of hair pulling. I have a function (below) that takes a list of html pages that...
0
by: ne0lithic | last post by:
Hey everyone, I'm working on a program based on steganography. For this, i need to access the individual pixels and perform my manipulations. FYI, I do not have the JAI library available with me...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development projectplanning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.