ImageIO problems encoding jpeg

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Robert Larsen

    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):



    Bigger versions can be seen here:



    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
  • Andrew Thompson

    #2
    Re: ImageIO problems encoding jpeg

    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.
    ditto.
    >Has anybody seen this ?
    Not yet.

    --
    Andrew Thompson


    Message posted via JavaKB.com
    Alexistogel merupakan situs slot online server asia yang menawarkan kemudahan dalam bermain slot tanpa harus mengeluarkan modal besar.


    Comment

    • Andrew Thompson

      #3
      Re: ImageIO problems encoding jpeg

      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


      Message posted via JavaKB.com
      Alexistogel merupakan situs slot online server asia yang menawarkan kemudahan dalam bermain slot tanpa harus mengeluarkan modal besar.


      Comment

      • Robert Larsen

        #4
        Re: ImageIO problems encoding jpeg

        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.

        Comment

        • Robert Larsen

          #5
          Re: ImageIO problems encoding jpeg

          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

          Comment

          • Andrew Thompson

            #6
            Re: ImageIO problems encoding jpeg

            Robert Larsen wrote:
            ...
            (JPEG)
            >
            >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


            Message posted via JavaKB.com
            Alexistogel merupakan situs slot online server asia yang menawarkan kemudahan dalam bermain slot tanpa harus mengeluarkan modal besar.


            Comment

            Working...