How does this loadBytes() work for loading an image?

I've get this code from an example by "Michael Zick Doherty" about twitter stream api. This little piece is used to load an image if the image has not a correct extension (.jpg or .png etc). why does this work? I never used bytes. You can just load anything to a byte array?

snnipet:

if (!imgUrl.endsWith(".jpg")) {
        byte[] imgBytes = loadBytes(imgUrl);
        saveBytes("tempImage.jpg", imgBytes);
        imgUrl = "tempImage.jpg";
      }

Answers

  • Answer ✓

    "You can just load anything to a byte array?"
    Well, yes, anything on your hard disk is saved as a bunch of bytes, even text files or programs.

  • edited December 2013 Answer ✓

    "is used to load an image if the image has not a correct extension"
    Note that loadImage() states:

    The extension parameter is used to determine the image type in cases where the image filename does not end with a proper extension. Specify the extension as the second parameter to loadImage(), as shown in the third example on this page.

    loadImage(imgUrl, "jpg");

    This trick is not necessary (not sure when the second parameter was introduced, perhaps the trick was needed in old versions of Processing).

  • Answer ✓

    This little piece is used to load an image if the image has not a correct extension (.jpg or .png etc)

    i'm not sure it is. it's reading an image from a file with no extension (possibly with a .png extension) and saving it as a file with a .jpg extension. this won't convert the file so you might end up with png data in a .jpg file. this might still work - some code is intelligent enough to know that the extension and the file contents are two different things and load it regardless.

    or it could just be that the file was a jpg file anyway, was just missing an extension. this code gives it one. <- oh, found the code now, it's this one. twitter doesn't always supply extensions by the look. philho's loadImage trick would probably also work.

  • Thanks guys.

Sign In or Register to comment.