001/**
002 * Copyright (c) 2011, The University of Southampton and the individual contributors.
003 * All rights reserved.
004 *
005 * Redistribution and use in source and binary forms, with or without modification,
006 * are permitted provided that the following conditions are met:
007 *
008 *   *  Redistributions of source code must retain the above copyright notice,
009 *      this list of conditions and the following disclaimer.
010 *
011 *   *  Redistributions in binary form must reproduce the above copyright notice,
012 *      this list of conditions and the following disclaimer in the documentation
013 *      and/or other materials provided with the distribution.
014 *
015 *   *  Neither the name of the University of Southampton nor the names of its
016 *      contributors may be used to endorse or promote products derived from this
017 *      software without specific prior written permission.
018 *
019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
020 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
021 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
022 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
023 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
024 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
025 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
026 * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
027 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
028 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
029 */
030package org.openimaj.demos.sandbox.twitter.images;
031
032import java.awt.GraphicsDevice;
033import java.awt.GraphicsEnvironment;
034import java.io.FileNotFoundException;
035import java.io.UnsupportedEncodingException;
036import java.net.URL;
037
038import javax.swing.JFrame;
039
040import org.openimaj.image.DisplayUtilities;
041import org.openimaj.image.MBFImage;
042import org.openimaj.image.colour.ColourSpace;
043import org.openimaj.image.colour.RGBColour;
044import org.openimaj.image.processing.resize.ResizeProcessor;
045import org.openimaj.stream.functions.ImageFromURL;
046import org.openimaj.stream.functions.ImageSiteURLExtractor;
047import org.openimaj.stream.functions.twitter.TwitterURLExtractor;
048import org.openimaj.stream.provider.twitter.TwitterStreamDataset;
049import org.openimaj.util.api.auth.DefaultTokenFactory;
050import org.openimaj.util.api.auth.common.TwitterAPIToken;
051import org.openimaj.util.concurrent.ArrayBlockingDroppingQueue;
052import org.openimaj.util.function.Operation;
053import org.openimaj.util.stream.BlockingDroppingBufferedStream;
054import org.openimaj.util.stream.CollectionStream;
055import org.openimaj.util.stream.Stream;
056
057import twitter4j.Status;
058
059public class DisplayTwitterImages {
060        /**
061         * Main method
062         * 
063         * @param args
064         * @throws FileNotFoundException
065         * @throws UnsupportedEncodingException
066         */
067        public static void main(String[] args) throws FileNotFoundException, UnsupportedEncodingException {
068                /*
069                 * Construct a twitter stream with an
070                 */
071                final TwitterAPIToken token = DefaultTokenFactory.get(TwitterAPIToken.class);
072                final Stream<Status> stream = new TwitterStreamDataset(token);
073
074                final ArrayBlockingDroppingQueue<MBFImage> buffer = new ArrayBlockingDroppingQueue<MBFImage>(10);
075                final BlockingDroppingBufferedStream<MBFImage> imageStream = new BlockingDroppingBufferedStream<MBFImage>(buffer);
076
077                new Thread(new Runnable() {
078                        @Override
079                        public void run() {
080                                stream.parallelForEach(new Operation<Status>() {
081                                        @Override
082                                        public void perform(Status object) {
083
084                                                final Stream<URL> imageUrlStream = new CollectionStream<URL>(new TwitterURLExtractor()
085                                                                .apply(object))
086                                                                .map(new ImageSiteURLExtractor(false, true));
087
088                                                // Get images
089                                                final Stream<MBFImage> imageStream = imageUrlStream.map(ImageFromURL.MBFIMAGE_EXTRACTOR);
090
091                                                final boolean[] foundImages = { false };
092                                                imageStream.forEach(new Operation<MBFImage>() {
093                                                        @Override
094                                                        public void perform(MBFImage image) {
095                                                                buffer.offer(image);
096                                                                foundImages[0] = true;
097                                                        }
098                                                });
099                                                System.out.println(foundImages[0]);
100                                        }
101                                });
102                        }
103                }).start();
104                final int N_ROWS = 10; 
105                final int IMAGE_WH= 50;
106                final MBFImage b = new MBFImage(IMAGE_WH * N_ROWS, IMAGE_WH * N_ROWS, ColourSpace.RGB);
107                final ResizeProcessor rp = new ResizeProcessor(IMAGE_WH);
108                final JFrame f = DisplayUtilities.displaySimple(b, "image");
109                GraphicsEnvironment env = GraphicsEnvironment.getLocalGraphicsEnvironment();
110                GraphicsDevice dev = env.getScreenDevices()[1];
111                dev.setFullScreenWindow(f);
112                final MBFImage base = new MBFImage(f.getWidth(), f.getHeight(), ColourSpace.RGB);
113                imageStream.forEach(new Operation<MBFImage>() {
114                        
115                        int currentX = 0;
116                        int currentY = 0;
117                        @Override
118                        public void perform(MBFImage object) {
119                                MBFImage r = object.process(rp);
120
121                                final int dx = (IMAGE_WH - r.getWidth()) / 2;
122                                final int dy = (IMAGE_WH - r.getHeight()) / 2;
123
124                                r = r.padding(dx, dy, RGBColour.WHITE);
125                                base.drawImage(r, currentX * IMAGE_WH, currentY * IMAGE_WH);
126                                currentX++;
127                                if (currentX == base.getWidth()/IMAGE_WH) {
128                                        currentY++;
129                                        currentX = 0;
130                                }
131                                if (currentY == base.getHeight()/IMAGE_WH)
132                                        currentY = 0;
133
134                                DisplayUtilities.display(base, f);
135                                
136                        }
137                });
138        }
139}