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

143
LINES

< > BotCompany Repo | #672 // Focal points (developing)

JavaX source code - run with: x30.jar

1  
!636
2  
!629 // standard functions
3  
!658 // image classes
4  
!673 // thread {
5  
6  
import java.awt.*;
7  
import java.awt.image.*;
8  
import java.util.List;
9  
import javax.imageio.*;
10  
import java.lang.reflect.*;
11  
12  
main {
13  
  static Point[] usersFocalPoints = {
14  
    new Point(205, 233),
15  
    new Point(266, 172),
16  
    new Point(327, 229),
17  
    new Point(266, 294)
18  
  };
19  
  
20  
  static int spatialTolerance = 10; // generous
21  
  
22  
  static RGBImage originalImage;
23  
24  
  // returns a score (higher = better)
25  
  static int checkFocalPoints(Point[] points, Point[] ref) {
26  
    int correct = 0;
27  
    List<Point> list = new ArrayList<Point>(Arrays.asList(points));
28  
    for (Point p : ref) {
29  
      Point u = findPoint(p, list);
30  
      if (u != null) {
31  
        ++correct;
32  
        list.remove(u);
33  
      }
34  
    }
35  
    int superfluous = list.size();
36  
    return correct-superfluous;
37  
  }
38  
  
39  
  static boolean nearEnough(Point p, Point u) {
40  
    return sqr(p.x-u.x) + sqr(p.x-u.y) <= sqr(spatialTolerance);
41  
  }
42  
  
43  
  static double sqr(double a) { return a*a; }
44  
  
45  
  static Point findPoint(Point p, List<Point> list) {
46  
    for (Point u : list) {
47  
      if (nearEnough(p, u))
48  
        return u;
49  
    }
50  
    return null;
51  
  }
52  
  
53  
  static void markPoints(RGBImage img, List<Point> list,
54  
    Color color) {
55  
    for (Point p : list) {
56  
      for (int y = p.y-1; y <= p.y+1; y++)
57  
        for (int x= p.x-1; x <= p.x+1; x++)
58  
          img.setPixel(x, y, x == p.x && y == p.y ? Color.white : color);
59  
    }
60  
  }
61  
  
62  
  psvm {
63  
    String imageID = "#1000335"; // Picture of a big dot
64  
    if (args.length != 0) imageID = args[0];
65  
    final String _imageID = imageID;
66  
    
67  
    final ImageSurface imageSurface = new ImageSurface();
68  
    makeFrame(new JScrollPane(imageSurface));
69  
    
70  
    thread {
71  
      originalImage = loadImage(_imageID);
72  
      RGBImage img = new RGBImage(originalImage);
73  
      markPoints(img, Arrays.asList(usersFocalPoints), Color.red);
74  
      imageSurface.setImage(img);
75  
    }.start();
76  
  }
77  
  
78  
  static void makeFrame(Component panel) {
79  
    JFrame frame = new JFrame("A JavaX Frame");
80  
    frame.add(panel);
81  
    frame.setBounds(100, 100, 600, 600);
82  
    frame.setVisible(true);
83  
    exitOnFrameClose(frame);
84  
  }
85  
  
86  
  static boolean useImageCache = true;
87  
  static RGBImage loadImage(String snippetID) {
88  
   try {
89  
    File dir = new File(System.getProperty("user.home"), ".tinybrain/image-cache");
90  
    if (useImageCache) {
91  
      dir.mkdirs();
92  
      File file = new File(dir, snippetID + ".png");
93  
      if (file.exists() && file.length() != 0)
94  
        try {
95  
          return new RGBImage(ImageIO.read(file));
96  
        } catch (Throwable e) {
97  
          e.printStackTrace();
98  
          // fall back to loading from sourceforge
99  
        }
100  
    }
101  
102  
    String imageURL = getImageURL(parseSnippetID(snippetID));
103  
    System.err.println("Loading image: " + imageURL);
104  
    BufferedImage image = ImageIO.read(new URL(imageURL));
105  
106  
    if (useImageCache) {
107  
      File tempFile = new File(dir, snippetID + ".tmp." + System.currentTimeMillis());
108  
      ImageIO.write(image, "png", tempFile);
109  
      tempFile.renameTo(new File(dir, snippetID + ".png"));
110  
      //Log.info("Cached image.");
111  
    }
112  
113  
    //Log.info("Loaded image.");
114  
    return new RGBImage(image);
115  
   } catch (IOException e) {
116  
    throw new RuntimeException(e);
117  
   }
118  
  }
119  
120  
  static String getImageURL(long snippetID) throws IOException {
121  
    String url;
122  
    if (snippetID == 1000010 || snippetID == 1000012)
123  
      url = "http://tinybrain.de:8080/tb/show-blobimage.php?id=" + snippetID;
124  
    else
125  
      url = "http://eyeocr.sourceforge.net/filestore/filestore.php?cmd=serve&file=blob_" + snippetID
126  
        + "&contentType=image/png";
127  
    return url;
128  
  }
129  
130  
  public static long parseSnippetID(String snippetID) {
131  
    return Long.parseLong(shortenSnippetID(snippetID));
132  
  }
133  
134  
  private static String shortenSnippetID(String snippetID) {
135  
    if (snippetID.startsWith("#"))
136  
      snippetID = snippetID.substring(1);
137  
    String httpBlaBla = "http://tinybrain.de/";
138  
    if (snippetID.startsWith(httpBlaBla))
139  
      snippetID = snippetID.substring(httpBlaBla.length());
140  
    return snippetID;
141  
  }
142  
  
143  
}

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: #672
Snippet name: Focal points (developing)
Eternal ID of this version: #672/1
Text MD5: d98cc6c7b2700e11577fce17875a783a
Author: stefan
Category:
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-07-16 17:08:51
Source code size: 4198 bytes / 143 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 626 / 499
Referenced in: [show references]