ラベル Google Data API の投稿を表示しています。 すべての投稿を表示
ラベル Google Data API の投稿を表示しています。 すべての投稿を表示

2009-07-15

iPicasa ビューア

ケータイのPicasaアップローダーまで作ったので、ビューアの機能を折角だから追加しようと思う。
iモードから既に閲覧できるので、それよりは便利にしないと意味ないけどね。
で、Google Data APIをつかって画像データを取得するプログラムを書いてみた。

以下は、PhotoEntry に格納されているMediaContentのURIから、データを取得してファイルに保存するプログラム。同様のことをサムネイルのデータにも行っている。


for (PhotoEntry entry : photos) {
MediaContent content = (MediaContent)entry.getContent();

InputStream is = new URL(content.getUri()).openStream();
FileOutputStream os = new FileOutputStream("Photo"+count+".jpg");
int len;
while ((len = is.read(b)) > -1) {
os.write(b, 0, len);
}
is.close();
os.close();

MediaThumbnail mediaThumbnail = entry.getMediaThumbnails().get(0);
is = new URL(mediaThumbnail.getUrl()).openStream();
os = new FileOutputStream("Thumbnail"+count+".jpg");
while ((len = is.read(b)) > -1) {
os.write(b, 0, len);
}
is.close();
os.close();
}


これのファイルに保存するところを、ケータイからのリクエストを中継してあげれば、送ってあげるようにしてあげればいいわけです。

2009-06-26

iアプリ+GAEの開発

iアプリ+GAEの開発は、まだあまり時間を使っていないためか、ノウハウはほとんど溜まっていない。

その中でも、今回のJava版のGAEの開発でTipsはらしいところの1つは以下のリンクにある
Google Data API ライブラリを App Engine から使うための設定。

http://code.google.com/intl/zh-HK/appengine/kb/java.html#googledata

App Engine開発のメーリングリストで対応方法を教えてもらえなかったら、開発を挫折していたと思う。
本当に助かりました。

2008-07-04

最初の一筆。

昔から日記は続いたことはないし、人に文章を見てもらうことにもなれていない。
でもメモはよくとってきたので、肩肘はらず、メモを残すつもりではじめたいと思う。

まずは旅行にいくので、googleを旅行先で活用できないか調べたい。

使ってみたいのは、Picasa Web Albums。
携帯電話からも Picasa は使えるけど、あくまで閲覧だけの様子。

旅行先で写真を撮って、簡単に写真と場所を保存できたらいいなあ。
で調べたのは Picasa Web Albums Data API

google Data API は使ったことがないので、よい勉強になるだろう。
ざっと見てみると google Data APIは、 AtomRSSAtom Publishing Protocol (APP) といったフォーマット、RESTのアプローチでサービスを提供しているよう。

動くものを作ってみたくなったので、Javaによる実装を挑戦。
google のサンプルソースコードは読みやすく、すぐ応用できた。

まずは簡単に写真を登録する Proxy Server を作ってみた。
以下はそのソースコード。


import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.util.Date;
import java.util.List;
import java.util.ResourceBundle;

import org.apache.log4j.Logger;

import com.google.gdata.client.photos.PicasawebService;
import com.google.gdata.data.OtherContent;
import com.google.gdata.data.photos.AlbumEntry;
import com.google.gdata.data.photos.PhotoEntry;
import com.google.gdata.util.ContentType;
import com.google.gdata.util.ServiceException;
import com.sun.net.httpserver.HttpExchange;
import com.sun.net.httpserver.HttpHandler;
import com.sun.net.httpserver.HttpServer;

public class PicasaProxy extends PicasawebClient {
private static final Logger logger = Logger.getLogger(PicasaProxy.class);

final HttpServer server;

AlbumEntry albumEntry;

public PicasaProxy(PicasawebService service, String uname, String passwd,
int alubum, String address, int port)
throws IOException, ServiceException {

super(service, uname, passwd);

logger.debug("uname:"+uname);
logger.debug("passwd:"+passwd);
logger.debug("alubum:"+alubum);
logger.debug("address:"+address);
logger.debug("port:"+port);

selectAlubum(alubum);

String[] parts = address.split("\\.");
byte [] addr = new byte[parts.length];
for (int i=0; i < parts.length; i++) {
addr[i] = (byte)Integer.parseInt(parts[i]);
}
InetAddress inetAddress = InetAddress.getByAddress(addr);
InetSocketAddress hostPort = new InetSocketAddress(inetAddress, port);

server = HttpServer.create(hostPort, 1);
server.createContext("/createPhoto.do", new CreatePhoto());
}

public void start() {
server.start();
}

private void selectAlubum(int index) throws IOException, ServiceException {
List albums = getAlbums();
albumEntry = albums.get(index);
System.out.println(albumEntry.getId());
}

class CreatePhoto implements HttpHandler {
public void handle(HttpExchange httpExchange) throws IOException {
logger.debug("CreatePhoto Start");
try {
InputStream in = httpExchange.getRequestBody();
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte buff[] = new byte[1024];
int len;
while((len = in.read(buff)) != -1) {
out.write(buff, 0, len);
}
in.close();

PhotoEntry photoEntry = new PhotoEntry();
photoEntry.setTitle(albumEntry.getTitle());
photoEntry.setTimestamp(new Date());
OtherContent content = new OtherContent();
content.setBytes(out.toByteArray());
content.setMimeType(new ContentType("image/jpeg"));
photoEntry.setContent(content);

insert(albumEntry, photoEntry);

photoEntry = null;

final String response = "OK";
httpExchange.sendResponseHeaders(200, response.getBytes().length);
httpExchange.getResponseBody().write(response.getBytes());
httpExchange.close();
} catch (Exception e) {
logger.equals(e);
final String responseError = "ERROR: 500 Internal Server Error";
httpExchange.sendResponseHeaders(500, responseError.getBytes().length);
httpExchange.getResponseBody().write(responseError.getBytes());
httpExchange.close();
}
logger.debug("CreatePhoto End");
}
}

public static void main(String[] args) throws Exception {
PicasawebService service = new PicasawebService("PicasaProxy");

ResourceBundle rb = ResourceBundle.getBundle(PicasaProxy.class.getName());
String uname = rb.getString("uname");
String passwd = rb.getString("passwd");
int alubum = Integer.parseInt(rb.getString("alubum"));
String address = rb.getString("address");
int port = Integer.parseInt(rb.getString("port"));

PicasaProxy proxy =
new PicasaProxy(service, uname, passwd, alubum, address, port);
proxy.start();
}
}

今度はiアプリを使り、連携して写真を登録できるようにしよう。