473,320 Members | 1,939 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,320 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 7091
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: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, youll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.