We are about to switch to a new forum software. Until then we have removed the registration on this forum.
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
"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.
"is used to load an image if the image has not a correct extension"
Note that loadImage() states:
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).
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.