PicasaPhotoLoader


Picasaから簡単に画像を取ってこれるように整理してみた。

いろいろ作っていると、インターフェイスの統一が大切、ってのが身に染みてわかってきたので、なんとなく作ってみた。

loadに検索キーワードや大きさ、枚数を入れると読み込んでくれるクラス。

問題は、4枚読み込む指示を出しても、4枚の保証が無い。
検索結果を使っているので、ある程度しょうがないけど、なにかの対策はあった方がいいかも。

PicasaPhotoLoader – wonderfl build flash online

Picasa Web Albums query parameters reference
http://code.google.com/intl/ja/apis/picasaweb/docs/2.0/reference.html#Parameters

▼ActionScript AS3(FP10)
[sourcecode language=”as3″]
/*
* Picasaから簡単に画像を取ってこれるようにしたかったので。
*
* Picasa Web Albums query parameters reference
* http://code.google.com/intl/ja/apis/picasaweb/docs/2.0/reference.html#Parameters
*
* */
package {
import flash.display.Sprite;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.text.TextField;
/**
* …
* @author umhr
*/
[SWF(width = 465, height = 465)]
public class Main extends Sprite {
private var _photos:PicasaPhotos;

public function Main():void {
_photos = new PicasaPhotos();
_photos.addEventListener(Event.COMPLETE, onComplete);
_photos.addEventListener(IOErrorEvent.IO_ERROR, onIoError);

var parameters:Object = { q:"sakura japan"};
_photos.load( parameters );

}
private function onIoError(event:Event):void {
trace("無いよ");
}
private function onComplete(event:Event):void {

this.addChild(_photos.loader.getBitmap("0"));
trace(_photos.loader.items[0].url);

var tf:TextField = new TextField();
tf.text = "Photo by " + _photos.credits[0];
tf.textColor = 0xFFFFFF;
tf.autoSize = "left";
tf.background = true;
tf.backgroundColor = 0x000000;
this.addChild(tf);

}
}
}

import flash.system.Security;
import flash.events.Event;
import flash.system.LoaderContext;
class PicasaPhotos {
public var xmlLoader:MultiLoader;
public var loader:MultiLoader;
public var credits:Array = [];
private var _complete:Function = function(event:Event):void{};
private var _ioError:Function = function(event:Event):void{};
public function PicasaPhotos() {
Security.loadPolicyFile("http://photos.googleapis.com/data/crossdomain.xml");
xmlLoader = new MultiLoader("xml");
loader = new MultiLoader("photos");
}
public function addEventListener(type:String, listener:Function):void {
this["_" + type] = listener;
}
public function load(props:Object = null):void {
var defaultProps:Object = { "max-results":3, imgmax:"320", imglic:"creative_commons", kind:"photo", "start-index":"1"};

var xmlURL:String = "http://photos.googleapis.com/data/feed/base/all?alt=rss";

var p:String;
for (p in props) {
defaultProps[p] = props[p];
}

for (p in defaultProps) {
xmlURL += "&" + p + "=" + encodeURIComponent(decodeURIComponent(defaultProps[p]));
}
xmlLoader.add(xmlURL, { id:"xml", type:"text" } );
xmlLoader.addEventListener(Event.COMPLETE, loadIMG);
xmlLoader.start();
}
private function loadIMG(event:Event):void {
var myXML:XML = xmlLoader.getXML("xml");
default xml namespace = new Namespace("http://search.yahoo.com/mrss/");
var n:int = myXML.channel.item.length();
if (n == 0) {
atIoError(null);
return;
}
for (var i:int = 0; i < n; i++) {
var url:String = myXML.channel.item[i].group.content.@url;
loader.add(url, { context:new LoaderContext(true), id:String(credits.length), type:"image" } );
credits.push(myXML.channel.item[i].group.credit);
}
loader.addEventListener(Event.COMPLETE, atComp);
loader.start();
}
private function atIoError(event:Event):void {
_ioError(event);
}
private function atComp(event:Event):void {
_complete(event);
}
}

import flash.display.Bitmap;
import flash.display.BitmapData;
import flash.display.Loader;
import flash.events.Event;
import flash.events.IOErrorEvent;
import flash.net.URLLoader;
import flash.net.URLLoaderDataFormat;
import flash.net.URLRequest;
import flash.system.LoaderContext;
import flash.system.Security;
import flash.utils.Dictionary;
import flash.utils.ByteArray;
import flash.utils.getQualifiedClassName;
import flash.display.MovieClip;
class MultiLoader{
public static var IMAGE_EXTENSIONS:Array = ["swf", "jpg", "jpeg", "gif", "png"];
public static var TEXT_EXTENSIONS:Array = ["txt", "js", "xml", "php", "asp"];
public static const COMPLETE:String = "complete";
private var _listener:Function = function(event:Event):void{};
private var _loads:Dictionary;
private var _keyFromId:Dictionary;
private var _loadCount:int;
private var _itemsLoaded:int;
public var items:Array;
public function MultiLoader(name:String = ""){
_loads = new Dictionary();
_keyFromId = new Dictionary();
_itemsLoaded = 0;
items = [];
}
public function add(url:String, props:Object = null):void {
var loadingItem:LoadingItem = new LoadingItem();
loadingItem.url = url;
loadingItem.type = getType(url, props);
if(props){
if(props.context){
loadingItem.context = props.context;
}
if (props.id) {
_keyFromId[props.id] = url;
}
if (props.preventCache) {
loadingItem.preventCache = props.preventCache;
}
}
items.push(loadingItem);
}
private function getType(url:String, props:Object = null):String{
var result:String = "";
if (props && props.type) {
return props.type;
}
var i:int;
var extension:String;
var n:int = IMAGE_EXTENSIONS.length;
for (i = 0; i < n; i++) {
extension = IMAGE_EXTENSIONS[i];
if(extension == url.substr(-extension.length).toLowerCase()){
result = "image";
break;
}
}
if(result == ""){
n = TEXT_EXTENSIONS.length;
for (i = 0; i < n; i++) {
extension = TEXT_EXTENSIONS[i];
if(extension == url.substr(-extension.length).toLowerCase()){
result = "text";
break;
}
}
}
return result;
}

public function start():void{
var n:int = items.length;
for (var i:int = 0; i < n; i++) {
var type:String = items[i].type;
var url:String = items[i].url;
url += (items[i].preventCache)?"?rand=" + Math.random():"";
var uRLRequest:URLRequest = new URLRequest(url);
if(type == "image"){
_loads[items[i].url] = loadImage(uRLRequest, items[i].context);
}else if(type == "text"){
_loads[items[i].url] = loadText(uRLRequest);
}else if (type == "byteImage") {
byteImage(uRLRequest, items[i].context);
}
}
}
public function addEventListener(type:String,listener:Function):void{
_listener = listener;
}
public function getBitmap(key:String):Bitmap{
key = keyMatching(key);
var bitmap:Bitmap;
try{
if (getQualifiedClassName(_loads[key].content) == "flash.display::MovieClip") {
var mc:MovieClip = _loads[key].content;
var bitmapData:BitmapData = new BitmapData(mc.width, mc.height);
bitmapData.draw(mc);
bitmap = new Bitmap(bitmapData);
}else {
bitmap = _loads[key].content;
}
}catch (e:*) {
//bitmap = new Bitmap();
}
return bitmap;
}
public function getBitmapData(key:String):BitmapData{
key = keyMatching(key);
var bitmap:Bitmap = getBitmap(key);
var bitmapData:BitmapData = new BitmapData(bitmap.width, bitmap.height);
bitmapData.draw(bitmap);
return bitmapData;
}
private function loadImage(url:URLRequest, context:LoaderContext = null):Loader {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, onComp);
loader.load(url,context);
return loader;
}
private function byteImage(url:URLRequest, context:LoaderContext = null):void {
var loader:Loader = new Loader();
loader.contentLoaderInfo.addEventListener(Event.COMPLETE, loadBytes);
loader.load(url, context);
function loadBytes(event:Event):void {
_loads[url.url] = new Loader();
_loads[url.url].contentLoaderInfo.addEventListener(Event.COMPLETE, onComp);
_loads[url.url].loadBytes(event.target.bytes);
}
}
public function getBinary(key:String):ByteArray{
return _loads[keyMatching(key)].contentLoaderInfo.bytes;
}

public function getText(key:String):String {
key = keyMatching(key);
return key?_loads[key].data:key;
}
public function getXML(key:String):XML {
return XML(getText(key));
}
private function keyMatching(key:String):String {
return _loads[key]?key:_keyFromId[key];
}

private function loadText(url:URLRequest):URLLoader{
var loader:URLLoader = new URLLoader();
loader.addEventListener(Event.COMPLETE, onComp);
loader.load(url);
return loader;
}
private function onComp(event:Event):void{
_itemsLoaded ++;
if (_itemsLoaded == items.length) {
_itemsLoaded = 0;
_listener(event);
}
}
public function get itemsTotal():int{
return items.length;
}
public function get itemsLoaded():int{
return _itemsLoaded;
}
public function get loadedRatio():Number {
return _itemsLoaded / items.length;
}
}

import flash.net.URLRequest;
import flash.system.LoaderContext;
class LoadingItem{
public var url:String;
public var preventCache:Boolean;
public var type:String;
public var status:String;
public var context:LoaderContext;
public function LoadingItem(){};
}
[/sourcecode]