Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

50
LINES

< > BotCompany Repo | #1003723 // loadBufferedImage - also takes a file path

JavaX fragment (include) [tags: use-pretranspiled]

Libraryless. Click here for Pure Java version (6670L/38K).

static boolean loadBufferedImage_useImageCache = true;

static BufferedImage loadBufferedImage(String snippetIDOrURLOrFile) ctex {
  ping();
  if (snippetIDOrURLOrFile == null) ret null;
  if (isURL(snippetIDOrURLOrFile))
    ret imageIO_readURL(snippetIDOrURLOrFile);

  if (isSnippetID(snippetIDOrURLOrFile)) {
    S snippetID = "" + parseSnippetID(snippetIDOrURLOrFile);
    
    ifndef NoResourceLoader
    IResourceLoader rl = vm_getResourceLoader();
    if (rl != null)
      ret loadBufferedImage(rl.loadLibrary(snippetID));
    endifndef
    
    File dir = imageSnippetsCacheDir();
    if (loadBufferedImage_useImageCache) {
      dir.mkdirs();
      File file = new File(dir, snippetID + ".png");
      if (file.exists() && file.length() != 0)
        try {
          return ImageIO.read(file);
        } catch (Throwable e) {
          e.printStackTrace();
          // fall back to loading from sourceforge
        }
    }
  
    String imageURL = snippetImageURL_http(snippetID);
    print("Loading image: " + imageURL);
    BufferedImage image = imageIO_readURL(imageURL);
  
    if (loadBufferedImage_useImageCache) {
      File tempFile = new File(dir, snippetID + ".tmp." + System.currentTimeMillis());
      ImageIO.write(image, "png", tempFile);
      tempFile.renameTo(new File(dir, snippetID + ".png"));
      //Log.info("Cached image.");
    }
  
    //Log.info("Loaded image.");
    return image;
  } else
    ret loadBufferedImage(new File(snippetIDOrURLOrFile));
}

static BufferedImage loadBufferedImage(File file) {
  ret loadBufferedImageFile(file);
}

download  show line numbers  debug dex  old transpilations   

Travelled to 18 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, irmadwmeruwu, ishqpsrjomds, lpdgvwnxivlt, mowyntqkapby, mqqgnosmbjvj, ofpaelxlmzfo, onxytkatvevr, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003723
Snippet name: loadBufferedImage - also takes a file path
Eternal ID of this version: #1003723/14
Text MD5: 935506e5b3fa3ad1ef059537c4ddd00e
Transpilation MD5: 6007f20e13853b4da929f3c201da2634
Author: stefan
Category: javax
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2022-01-08 19:32:23
Source code size: 1628 bytes / 50 lines
Pitched / IR pitched: No / No
Views / Downloads: 689 / 827
Version history: 13 change(s)
Referenced in: #1004062 - loadImg - synonym of loadBufferedImage
#1004271 - loadImageAsFile - returns File - todo: merge with loadBufferedImage code
#1006654 - Standard functions list 2 (LIVE, continuation of #761)
#1010707 - loadBufferedImageOpt - use pcall
#1032004 - loadBufferedImageFile