001package org.openimaj.picslurper.client; 002 003import java.io.ByteArrayInputStream; 004import java.io.File; 005import java.io.IOException; 006import java.io.UnsupportedEncodingException; 007import java.util.LinkedList; 008 009import org.openimaj.image.DisplayUtilities; 010import org.openimaj.image.ImageUtilities; 011import org.openimaj.image.MBFImage; 012import org.openimaj.image.colour.ColourSpace; 013import org.openimaj.image.colour.RGBColour; 014import org.openimaj.image.processing.resize.ResizeProcessor; 015import org.openimaj.io.IOUtils; 016import org.openimaj.picslurper.output.WriteableImageOutput; 017import org.zeromq.ZMQ; 018 019/** 020 * @author Sina Samangooei (ss@ecs.soton.ac.uk) 021 * 022 */ 023public class ZMQGraphicalClient { 024 private static final int GRID_W = 160; 025 private static final int GRID_H = 120; 026 027 private static final int GRID_NX = 3; 028 private static final int GRID_NY = 3; 029 030 static class GridDisplay { 031 private int displayWidth; 032 private int displayHeight; 033 private int thumbXOffset; 034 private MBFImage display; 035 private LinkedList<MBFImage> displayList; 036 private ResizeProcessor mainResizer; 037 private ResizeProcessor thumbResizer; 038 039 public GridDisplay() { 040 this.displayWidth = (GRID_NX * GRID_W) * 2; 041 this.displayHeight = (GRID_NY * GRID_H); 042 043 this.thumbXOffset = (GRID_NX * GRID_W); 044 045 this.mainResizer = new ResizeProcessor((GRID_NX * GRID_W), (GRID_NY * GRID_H), true); 046 this.thumbResizer = new ResizeProcessor(GRID_W, GRID_H, true); 047 048 this.display = new MBFImage(displayWidth, displayHeight, ColourSpace.RGB); 049 050 this.displayList = new LinkedList<MBFImage>(); 051 052 this.redraw(); 053 } 054 055 private void redraw() { 056 this.display.fill(RGBColour.WHITE); 057 boolean first = true; 058 // Start at the end (i.e. most recent) 059 int ind = 0; 060 for (final MBFImage img : this.displayList) { 061 062 if (first) { 063 first = false; 064 // main image! 065 this.display.drawImage(img.process(this.mainResizer), 0, 0); 066 } else { 067 final int y = ind / GRID_NX; 068 final int x = (ind - (y * GRID_NX)); 069 this.display.drawImage(img.process(this.thumbResizer), this.thumbXOffset + (x * GRID_W), y * GRID_H); 070 ind++; 071 } 072 } 073 074 DisplayUtilities.displayName(display, "Pics, slurped!"); 075 } 076 077 public void add(MBFImage img) { 078 if (img.getWidth() < 10 || img.getHeight() < 10) 079 return; 080 if (this.displayList.size() == (GRID_NX * GRID_NY + 1)) { 081 this.displayList.removeLast(); 082 } 083 this.displayList.addFirst(img); 084 this.redraw(); 085 } 086 087 } 088 089 // private final static String SERVER = "152.78.64.99:5563"; 090 private final static String SERVER = "127.0.0.1:5563"; 091 092 /** 093 * @param args 094 * @throws UnsupportedEncodingException 095 */ 096 public static void main(String args[]) throws UnsupportedEncodingException { 097 System.out.println("Should be in: " + "/NATIVE/" + System.getProperty("os.arch") + "/" 098 + System.getProperty("os.name")); 099 // Prepare our context and subscriber 100 final ZMQ.Context context = ZMQ.context(1); 101 final ZMQ.Socket subscriber = context.socket(ZMQ.SUB); 102 103 subscriber.connect("tcp://" + SERVER); 104 subscriber.subscribe("IMAGE".getBytes("UTF-8")); 105 106 final GridDisplay display = new GridDisplay(); 107 while (true) { 108 // Consume the header 109 subscriber.recv(0); 110 final ByteArrayInputStream stream = new ByteArrayInputStream(subscriber.recv(0)); 111 WriteableImageOutput instance; 112 try { 113 instance = IOUtils.read(stream, WriteableImageOutput.class, "UTF-8"); 114 System.out 115 .println("Got URL: " + instance.file + " ( " + instance.stats.imageURLs + " ) (about to draw) "); 116 System.out.println("From tweet:\n" + instance.status.getText()); 117 for (final File imageFile : instance.listImageFiles("/Volumes/LetoDisk")) { 118 display.add(ImageUtilities.readMBF(imageFile)); 119 } 120 System.out.println("SUCCESS!"); 121 } catch (final IOException e) { 122 System.out.println("FAILED!"); 123 e.printStackTrace(); 124 } 125 } 126 } 127}