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

79
LINES

< > BotCompany Repo | #662 // Image displayer (from snippets database)

JavaX source code - run with: x30.jar

!636

!629 // standard functions
!658 // image classes

import java.awt.*;
import java.awt.image.*;
import java.util.List;
import javax.imageio.*;

public class main {
  public static void main(String[] args) {
    JFrame frame = new JFrame("A JavaX Frame");
    
    RGBImage image = loadImage("#1000326"); // Bryan Cranston!
    JScrollPane panel = new JScrollPane(new ImageSurface(image));
    
    frame.add(panel);
    frame.setBounds(100, 100, 500, 400);
    frame.setVisible(true);
    exitOnFrameClose(frame);
  }
  
  static boolean useImageCache = true;
  static RGBImage loadImage(String snippetID) {
   try {
    File dir = new File(System.getProperty("user.home"), ".tinybrain/image-cache");
    if (useImageCache) {
      dir.mkdirs();
      File file = new File(dir, snippetID + ".png");
      if (file.exists() && file.length() != 0)
        try {
          return new RGBImage(ImageIO.read(file));
        } catch (Throwable e) {
          e.printStackTrace();
          // fall back to loading from sourceforge
        }
    }

    String imageURL = getImageURL(parseSnippetID(snippetID));
    System.err.println("Loading image: " + imageURL);
    BufferedImage image = ImageIO.read(new URL(imageURL));

    if (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 new RGBImage(image);
   } catch (IOException e) {
    throw new RuntimeException(e);
   }
  }

  static String getImageURL(long snippetID) throws IOException {
    String url;
    if (snippetID == 1000010 || snippetID == 1000012)
      url = "http://tinybrain.de:8080/tb/show-blobimage.php?id=" + snippetID;
    else
      url = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + snippetID
        + "&contentType=image/png";
    return url;
  }

  public static long parseSnippetID(String snippetID) {
    return Long.parseLong(shortenSnippetID(snippetID));
  }

  private static String shortenSnippetID(String snippetID) {
    if (snippetID.startsWith("#"))
      snippetID = snippetID.substring(1);
    String httpBlaBla = "http://tinybrain.de/";
    if (snippetID.startsWith(httpBlaBla))
      snippetID = snippetID.substring(httpBlaBla.length());
    return snippetID;
  }
}

Author comment

Began life as a copy of #661

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, qbtsjoyahagl, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #662
Snippet name: Image displayer (from snippets database)
Eternal ID of this version: #662/1
Text MD5: 710320763ada71dbf6d38464b661740f
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-07-13 05:05:50
Source code size: 2511 bytes / 79 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 695 / 612
Referenced in: [show references]