Beautifl - Flash Gallery

Thumbnail : FlickrCam
FlickrCam
forresto 2011-06-11 All rights reserved

再生するにはFlash Playerが必要です。デスクトップのブラウザでご覧ください。

/**
 * Using my flickr images tagged "texture" to shade webcam video 
 * http://www.flickr.com/photos/forresto/tags/texture/ 
 * Shows using Flickr public RSS feeds, loading images into bitmapData, and webcam color manipulation
 */

// forked from forresto's RainbowCam
// forked from forresto's カタカナcam
// forked from ton's マトリックスの世界へようこそ
package {
    import flash.system.Security;
    import flash.display.Bitmap;
    import flash.display.BitmapData;
    import flash.display.Loader;
    import flash.display.LoaderInfo;
    import flash.display.Sprite;
    import flash.display.StageScaleMode;
    import flash.events.*;
    import flash.events.Event;
    import flash.media.Camera;
    import flash.media.Video;
    import flash.net.*;
        
    [SWF(backgroundColor=0x000000, frameRate=24)]
    public class FlickrCam extends Sprite {
        private const W:int = 465;
        private const H:int = 465;
        
        private var video:Video = new Video(W, H);
        private var videoIn:BitmapData = new BitmapData(W, H, false);
        private var filtered:BitmapData = new BitmapData(W, H, false);
        private var display:Bitmap = new Bitmap(filtered);

        // This RSS feed = these images: http://www.flickr.com/photos/forresto/tags/texture/ 
        private var userid:String = "37996589754@N01";
        private var tags:String = "texture";
        private var URL:String = "http://api.flickr.com/services/feeds/photos_public.gne?id="+userid+"&tags="+tags+"&content_type=1&lang=en-us&format=rss_200";
        
        private var loader:URLLoader;
        private var xmlData:XML;
        private var media:Namespace = new Namespace("http://search.yahoo.com/mrss/");
        
        private var imgs:Vector.<BitmapData> = new Vector.<BitmapData>();
        private var countLoading:uint = 0;
        private static const MAXIMGS:uint = 20;
        private static const USEIMGS:uint = 12;
        private var countLoaded:uint = 0;
        
        private var cycle:uint = 0;
        
        public function FlickrCam():void {
            stage.scaleMode = StageScaleMode.NO_SCALE;
            
            // Setup webcam
            var cam:Camera = Camera.getCamera();
            cam.setMode(W, H, 24, true);
            video.attachCamera(cam);
            
            // Add filtered image
            addChild(display);
            addEventListener(Event.ENTER_FRAME, onEnterFrame);
            
            // These lines are required to use bitmapData of Flickr images
            Security.loadPolicyFile("http://api.flickr.com/crossdomain.xml");
            Security.loadPolicyFile("http://farm1.staticflickr.com/crossdomain.xml");
            Security.loadPolicyFile("http://farm2.staticflickr.com/crossdomain.xml");
            Security.loadPolicyFile("http://farm3.staticflickr.com/crossdomain.xml");
            Security.loadPolicyFile("http://farm4.staticflickr.com/crossdomain.xml"); 
            Security.loadPolicyFile("http://farm5.staticflickr.com/crossdomain.xml"); 
            Security.loadPolicyFile("http://farm6.staticflickr.com/crossdomain.xml"); 
            Security.loadPolicyFile("http://farm7.staticflickr.com/crossdomain.xml"); 
            Security.loadPolicyFile("http://farm8.staticflickr.com/crossdomain.xml"); 
            Security.loadPolicyFile("http://farm9.staticflickr.com/crossdomain.xml"); 
            
            // Get Flickr RSS
            var req:URLRequest = new URLRequest(URL);
            loader = new URLLoader();
            loader.dataFormat = URLLoaderDataFormat.TEXT;
            req.method = "GET";
            req.url = URL;
            loader.addEventListener(Event.COMPLETE, completeHandler);
            
            try {
                loader.load(req);
            } catch (error:Error) {
                trace("Unable to load requested document.");
            }
        }
        
        private function completeHandler(event:Event):void {
            var loader:URLLoader = URLLoader(event.target);
            xmlData = new XML(loader.data);
            default xml namespace = media;
            
            for each(var element:Object in xmlData.channel.item){
                if (countLoading < MAXIMGS) {
                    // Find image url for thumbnail
                    var imgURL:String = element.media::thumbnail.@url;
                    // Convert thumbnail url to 640-size url
                    imgURL = imgURL.replace("_s.jpg", "_z.jpg");
                    // Load image
                    var imgReq:URLRequest = new URLRequest(imgURL);
                    var imgLoader:Loader = new Loader();
                    imgLoader.load(imgReq);
                    imgLoader.contentLoaderInfo.addEventListener(Event.COMPLETE, imageLoaded);
                    countLoading++;
                }
            }
        }
        
        private function imageLoaded(event:Event):void {
            // Get bitmapData from loaded image
            var bitmapData:BitmapData = Bitmap(LoaderInfo(event.target).content).bitmapData;
            // Save to bitmapData Vector
            imgs.push(bitmapData);
            countLoaded++;
        }
        
        private function onEnterFrame(e:Event):void {
            // Draw webcam image to bitmap
            videoIn.draw(video);
            if (countLoaded>0) {
                for (var y:int = 0; y < H; y++) {
                    for (var x:int = 0; x < W; x++) {
                        // Determine which image to use for the shade
                        var useableCount:uint = Math.min(countLoaded, USEIMGS)
                        var idx:uint = uint((videoIn.getPixel(x, y) / 0xffffff) * (useableCount-1));
                        // Get the pixel from that image
                        var color:uint = imgs[idx].getPixel(x, y);
                        // Set the pixel on the filtered image
                        filtered.setPixel(x, y, color);
                    }
                }
                if (cycle > 48) {
                    // Shift the images to change the shade
                    cycle = 0;
                    imgs.push(imgs.shift()); 
                }
                cycle++;
            } else {
                // Draw the unfiltered webcam image
                filtered.draw(video);
            }
        }
    }
}