473,806 Members | 2,605 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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.lastIn dexOf('.');
if (idx >= 0) {
File file = new File(filename);
String format = filename.substr ing(idx + 1).toLowerCase( );
ImageIO.write(i mage, format, file);
}
}

The image has been instanciated using:
image = new BufferedImage(w 0 ? w : 1, h 0 ? h : 1,
BufferedImage.T YPE_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 7113
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");
setDefaultClose Operation(JFram e.EXIT_ON_CLOSE );
ImagePanel p = new ImagePanel(WIDT H, HEIGHT);
pack();
setSize(WIDTH + getInsets().lef t + getInsets().rig ht, HEIGHT +
getInsets().top + getInsets().bot tom);
getContentPane( ).add(p);
setVisible(true );
}

public void write(String filename) throws IOException {
int idx = filename.lastIn dexOf('.');
if (idx >= 0) {
File file = new File(filename);
String format = filename.substr ing(idx + 1).toLowerCase( );
ImageIO.write(i mage, format, file);
}
}

private class ImagePanel extends JPanel {
public ImagePanel(Dime nsion 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.T YPE_INT_ARGB);
GradientPaint gp = new GradientPaint(0 , 0, Color.blue, w, h,
Color.green);
Graphics2D g2d = image.createGra phics();
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(ima ge, 0, 0, this);
}
}

public static void main(String args[]) throws IOException {
SSCCE sscce = new SSCCE();
for (String filename : args) {
System.out.prin tln("Saving to " + filename);
sscce.write(fil ename);
}
}
}
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
8239
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 Delphi program, but I have problems reading the pics from the socket's stream. Here's my code:
1
8003
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 based) with the following code: InputStream in = client.getInputStream(); BufferedImage rimg = ImageIO.read(in); File file = new File("d:\\ajay.jpg");
3
3430
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, but not with JPEGs. As you can see below, I've attached a progress listener to the reader, which reports progress up to just under 95%, then nothing. The warning listener reports no warnings. The images are written by an ImageWriter in another...
1
10204
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 name byte4. I have written the following code: First, thePanorama is defined as: new BufferedImage(width, height, BufferedImage.TYPE_INT_ARGB); Then I loop through like follows:
2
2188
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 the job and outputs the result in HTML. At the same time job is removed from the server.
4
9649
by: Laszlo Szijarto | last post by:
anyone know of a JPEG 2000 encoding / decoding library that works with .NET? Thank you, Laszlo
1
2684
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 of sample code that basically shows how I am encoding and then decoding the binary image. Many thanks if you can point out what I am doing wrong... thanks, Slade Smith Image bmp =context.GetImage();
7
3119
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 have images on them (not porn but boats). This function then (supposedly) goes through and extracts the links to those images and puts them into a list, appending with each iteration of the for loop. The list of html pages is 82 items long and...
0
2033
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 and I have to make do with awt, imageIO and image. I'm having no issues in loading an image but when i thought I would test things and just save the same image without manipulation I was surprised. After copying the pixels from the image into an int...
0
9598
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10371
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10373
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10111
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9192
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, and deploymentwithout human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
5546
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5683
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4330
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3852
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.