home > FlickrとFlashの連動 - Flickr API
FlickrとFlashの連動 - Flickr API
August 04, 2010
ドキュメントクラス(MainStage.as)は省略。flaファイルのルートにphotoMCという名前のMCを配置します。必要ならローディングMC(loadingimg)も配置。
アクセス先
XML :
http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=" + api_key + "&user_id=" + user_id +"&privacy_filter=1"
画像URL :
http://farm" + farm_id[i] + ".static.flickr.com/" + server_id[i] + "/" + photo_id[i] + "_" + secret[i] + ".jpg"
サイズ指定がある時
http://farm" + farm_id[i] + ".static.flickr.com/" + server_id[i] + "/" + photo_id[i] + "_" + secret[i] + "_" + size + ".jpg"
今回は使ってませんが、サイズの種類は下記です。
s small square 75x75
t thumbnail, 100 on longest side
m small, 240 on longest side
- medium, 500 on longest side
b large, 1024 on longest side (only exists for very large original images)
o original image, either a jpg, gif or png, depending on source format
詳しくは、Flickr APIのドキュメントを確認ください。
http://www.flickr.com/services/api/
Flickr APIを使ってデモを作ってみました。
2010.08.09
今流行の3D風にしてみました。3D眼鏡でみると何となくそう見えます。
3D Flickr Viewer
http://www.publicroots.com/test/flickr/
2010.08.12
ドットで表現してみました。
Dot Flickr Viewer
http://www.publicroots.com/test/flickr/index2.html
package{
import flash.media.*;
import flash.net.*;
import flash.display.*;
import flash.events.*;
import flash.xml.*;
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.display.Loader;
import flash.display.LoaderInfo;
import flash.utils.Timer;
import flash.events.TimerEvent;
import flash.system.Security;public class Flickrapi
{
private var _mainstage:MainStage;
//NSID、api_keyを格納
private var user_id:String = "〜NSID〜";
private var api_key:String = "〜api_key〜"private var myXML:XML;
private var myLoader:Loader;
//リクエスト用のURLを生成します
private var url:String = "http://api.flickr.com/services/rest/?method=flickr.photos.search&api_key=" + api_key + "&user_id=" + user_id +"&privacy_filter=1";
private var myURLReq:URLRequest;
private var myXMLLoader:URLLoader;
private var taotalimg:Number;
private var taotalmaxxx:Number = 100;
private var photoURL:Array = new Array();
private var photo_id:Array = new Array();
private var farm_id:Array = new Array();
private var server_id:Array = new Array();
private var secret:Array = new Array();private var loadcount:Number = 1;
private var myCont:MovieClip;
//ループ時間
private var timer:Timer;
private var myImage:Bitmap;
//サイズ
private var size:String = "b";public function Flickrapi(_ms:MainStage):void
{
trace("[ FlickrAPICls ]");
_mainstage = _ms;
myCont = _mainstage.photoMC;//クロスドメインポリシーファイルのロード
Security.loadPolicyFile("http://farm1.static.flickr.com/crossdomain.xml");
Security.loadPolicyFile("http://farm2.static.flickr.com/crossdomain.xml");
Security.loadPolicyFile("http://farm3.static.flickr.com/crossdomain.xml");
Security.loadPolicyFile("http://farm4.static.flickr.com/crossdomain.xml");
Security.loadPolicyFile("http://farm5.static.flickr.com/crossdomain.xml");_mainstage.loadingimg.visible = true;
TopLoading();
_mainstage.stage.addEventListener(Event.RESIZE, TopLoadingResize);//読込みスタート
FlickrAPIStart();
}//リサイズで中央表示
//TopLoadingResize
private function TopLoadingResize( event:Event ):void {
TopLoading();
}
public function TopLoading():void {
//loadingimg
_mainstage.loadingimg.x = _mainstage.stage.stageWidth/2;
_mainstage.loadingimg.y = _mainstage.stage.stageHeight/2;
//myCont
myCont.x = _mainstage.stage.stageWidth/2 - myCont.width/2;
myCont.y = _mainstage.stage.stageHeight/2 - myCont.height/2;
}
private function FlickrAPIStart():void
{
myURLReq = new URLRequest(url);
//リクエスト発行します
myXMLLoader = new URLLoader();
myXMLLoader.dataFormat = URLLoaderDataFormat.TEXT;
myXMLLoader.addEventListener(Event.COMPLETE,setPhotoXML);
myXMLLoader.addEventListener(IOErrorEvent.IO_ERROR,isError);
myXMLLoader.load(myURLReq);
}
private function setPhotoXML(e:Event):void{
//読み込んだ後の処理を記述
myXML = new XML(myXMLLoader.data);
taotalimg = myXML.photos.@total;
trace("投稿写真の枚数 = "+taotalimg);
//配列にURL格納
for(var i:int = 1 ; i <= taotalmaxxx; i++){
photo_id[i] = myXML.photos.photo[(i-1)].@id;
farm_id[i] = myXML.photos.photo[(i-1)].@farm;
server_id[i] = myXML.photos.photo[(i-1)].@server;
secret[i] = myXML.photos.photo[(i-1)].@secret;photoURL[i] = "http://farm" + farm_id[i] + ".static.flickr.com/" + server_id[i] + "/" + photo_id[i] + "_" + secret[i] + ".jpg";
//trace("photoURL[ "+i+" ] = "+photoURL[i]);
}
//写真のロード
PhotoLoad(loadcount);
}private function PhotoLoad(_lun:Number):void
{
_mainstage.loadingimg.visible = true;var photoURLreq:URLRequest = new URLRequest(photoURL[_lun]);
myLoader = new Loader();
myLoader.contentLoaderInfo.addEventListener(Event.COMPLETE,setImage);
myLoader.contentLoaderInfo.addEventListener(IOErrorEvent.IO_ERROR,isError);
myLoader.load(photoURLreq);
}
private function setImage(e:Event){
myCont.alpha = 0;
myImage = e.target.content;
var loadedImage:Bitmap = Bitmap(myImage);
myCont.addChild(myImage);
//タイマー
timer = new Timer(4000,1);
timer.addEventListener(TimerEvent.TIMER, PhotoChangeFunc);
timer.start();
_mainstage.loadingimg.visible = false;
Tweener.addTween( myCont, { salpha:1, time:1, transition:"easeInQuart", onComplete:PhotoAlphaEnd} );
}
private function PhotoAlphaEnd():void
{
Tweener.removeTweens( myCont );
}private function PhotoChangeFunc( event:TimerEvent ):void
{
timer.removeEventListener(TimerEvent.TIMER, PhotoChangeFunc);
Tweener.addTween( myCont, { alpha:0, time:1, transition:"easeInQuart", onComplete:PhotoEnd} );
}
private function PhotoEnd():void
{
Tweener.removeTweens( myCont );
myCont.removeChild(myImage);
//ループ実行
if(loadcount < taotalmaxxx){
loadcount++;
}else{
loadcount = 1;
}
PhotoLoad(loadcount);
}
private function isError(e:IOErrorEvent):void{
trace("failed to Load photos info");
}
}
}