tags:

views:

129

answers:

2

I am trying to save a JPEG image from a URL to a file with Java. URL: http://150.214.222.100//axis-cgi/mjpg/video.cgi?resolution=640x480&compression=1&duration=1&timeout=&dummy=garb

I tried the following: 1)

Image image = fetchImage(urlNorthView);

saveImage2Disk(image);

public static Image fetchImage( URL u ) throws MalformedURLException, IOException   {
      Toolkit tk = Toolkit.getDefaultToolkit();
      return tk.createImage(u );
}

private void saveImage2Disk(Image Image) throws IOException{
        File outputFile = new File("urlNorthView"+Calendar.getInstance().getTimeInMillis()+".jpg");

        BufferedImage bufferedImage = new BufferedImage(Image.getWidth(null),Image.getHeight(null), BufferedImage.TYPE_INT_RGB);
        Graphics2D g2 = bufferedImage.createGraphics();
        g2.drawImage(Image, null, null);
        ImageIO.write(bufferedImage, "jpg", outputFile);
    }

=> Exception:"Width (-1) and height (-1) cannot be <= 0"

2)

inputStream2Disk((InputStream) urlNorthView.getContent());

private void inputStream2Disk(InputStream in) throws Exception{
        File outputFile = new File("urlNorthView"+Calendar.getInstance().getTimeInMillis()+".jpg");
        OutputStream out=new FileOutputStream(outputFile);
        byte buf[]=new byte[1024];
        int len;
        while((len=in.read(buf))>0)
        out.write(buf,0,len);
        out.close();
        in.close();
    }

The file is somehow broken. When I open it with Kate, I can read:

--myboundary Content-Type: image/jpeg Content-Length: 38256 ....

There should not be any text in a binary file.

What could the problem be?

+1  A: 

Try the following method for converting an Image to BufferedImage

private static BufferedImage getBufferedImage(Image img, int imageType) {
    if (img instanceof BufferedImage) {
        return (BufferedImage) img;
    }

     BufferedImage bi = new BufferedImage(img.getWidth(null), img
             .getHeight(null), imageType);

     Graphics2D g = (Graphics2D) bi.getGraphics();
     g.setRenderingHint(RenderingHints.KEY_ANTIALIASING,
             RenderingHints.VALUE_ANTIALIAS_ON);

     g.drawImage(img, 0, 0, null);
     g.dispose();

     return bi;
 }

(where imageType is one of the BufferedImage declared constants. Most likely TYPE_INT_RGB.

Otherwise your first approach is fine.

I'd recommend the first (high-level) approach over the second (low-level).

Bozho
+2  A: 

For some reason, the http response body when requesting that image contains a mime part (mime parts are useful for putting multiple files into a single response). In this response, there is only one mime part, so it is mostly useless.

There is code in the javax.mail package that you might be able to use to parse this properly if you want, but it's not a very good api, imho.

Alternatively, there are a bunch of ways you could hackishly fix this in code yourself. Since there's only one mime part, you can just throw away data from the beginning of your input stream until you see two newline characters in a row (bytes equal to 10). That should work since mime headers are supposed to be 7-bit ascii, iirc, so there's no character encoding to worry about.

Here is some sample code:

URLConnection conn = urlNorthView.openConnection();
InputStream in = conn.getInputStream();
String contentType = conn.getHeaderField("Content-Type");
if (!"image/jpeg".equals(contentType)) {
    // hack: assuming it's mime if not a raw image
    int one = in.read();
    if (one == -1) {
        // stop??
    }
    int two = in.read();
    while (two != -1 && !(two == 10 && one == 10)) {
        one = two;
        two = in.read();
    }
}
// if it was mime, we've stripped off the mime headers
// and should now get the image
inputStream2Disk(in);

Edit: crap, instead of two \n, you'll see two \r\n, or the bytes 0x0d, 0x0a, 0x0d, 0x0a. Throwing away data until you see this pattern is left as an exercise to the reader ;)

Jeremy Huiskamp
Works like a charm :) int two = in.read(); int three = in.read(); int four = in.read(); while (two != -1 two = three; three = four; four= in.read(); }
M.R.
@M.R. nice, but this is too low-level manipulation. You'd generally want to avoid such.
Bozho