[Windows Mobile]上傳圖片到 Picasa

  • 8840
  • 0
  • 2013-04-15

使用 Google Data API 上傳圖片到 Picasa 相簿中

 

1. 簡介

Google Data API 提供一個簡單的標準通訊協定,以讀取與撰寫網頁上的資料。目前很多 Google 服務都有支援 Google data API,其中包含 Google Apps、Base、Blogger、Calendar、Code Search、Contacts、Notebook、Spreadsheets、 Picasaweb、Documents List 和 YouTube 等等。這些 API 可以讓您輕鬆寫出既豐富又實用的應用程式。

在此運用其中 Picasa Web Albums Data API,網站和程式可利用它整合 Picasa 網頁相簿,讓使用者能夠建立相簿、上傳並擷取相片、評論相片以及進行其他操作。

 image

 

註 : Google 有提供一些 Google Data API 的 Sample Code,有興趣的可以去下載;此外,對於在 Windows Mobile 使用 Google Data API 有興趣的人,可參考 Writing Windows Mobile application using Google Data APIs

image   image

 

2. 方法

2.1 加入參考

首先下載 Google Data API SDK(1.4.0.2)並且安裝。

image image

 

接著加入參考

image

假如安裝目錄為 C:\Program Files\Google\Google Data API SDK\ 的話,則 dll 的位置為
C:\Program Files\Google\Google Data API SDK\Redist\Mobile\WindowsMobile\Google.GData.Client.dll
C:\Program Files\Google\Google Data API SDK\Redist\Mobile\WindowsMobile\Google.GData.Extensions.dll
C:\Program Files\Google\Google Data API SDK\Redist\Mobile\WindowsMobile\Google.GData.Photos.dll

image

註 : 請注意是在 Redist\Mobile\WindowsMobile 的 dll

 

以下程式功能為輸入 Picasa 帳號與密碼,相簿名稱、相片檔案名稱以及相片說明後,按下 Update 按鈕,上傳相片到指定位置

程式碼


using System;
using System.Linq;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

using System.IO;
using System.Net.Sockets;
using Google.GData.Photos;
using Google.GData.Client;
using Google.GData.Extensions;
using Google.GData.Extensions.Location;


namespace SmartDeviceProjectPicasa
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnUpdate_Click(object sender, EventArgs e)
        {
            uploadFile(this.txtUserName.Text, this.txtPassword.Text, this.txtFileName.Text, this.txtAlbumName.Text, this.chkEdit.Checked, this.txtPhotoSummery.Text);
            MessageBox.Show("Success");
        }

        /// <summary>
        /// 上傳圖片至指定相簿
        /// </summary>
        /// <param name="strUsername">帳號</param>
        /// <param name="strPassword">密碼</param>
        /// <param name="fileName">檔案名稱與路徑</param>
        /// <param name="strAlbumname">相簿名稱</param>
        /// <param name="bChkEdit">是否編輯照片資訊</param>
        /// <param name="strPhotoSummery">照片Summery</param>
        public void uploadFile(string strUsername, string strPassword, string fileName , string strAlbumname, bool bChkEdit, string strPhotoSummery)
        {
            string albumid = string.Empty;
            PicasaService service = new PicasaService(strAlbumname);
            service.setUserCredentials(strUsername, strPassword);
            AlbumQuery query = new AlbumQuery(PicasaQuery.CreatePicasaUri(strUsername));
            PicasaFeed picFeed = service.Query(query);

            albumid = GetAlbumid(strUsername, strPassword, strAlbumname);

            #region 當無此相簿時,則建立新的相簿
            if (albumid.Length == 0)
            {
                AlbumEntry newAlbumEntry = new AlbumEntry();
                newAlbumEntry.Title.Text = strAlbumname;
                AlbumAccessor ac = new AlbumAccessor(newAlbumEntry);
                ac.Access = "public";
                PicasaEntry createdAlbumEntry = (PicasaEntry)service.Insert(picFeed, newAlbumEntry);
                albumid = GetAlbumid(strUsername, strPassword, strAlbumname);
            }
            #endregion

            #region 上傳圖片到指定相簿
            PhotoQuery qryPhoto = new PhotoQuery(PicasaQuery.CreatePicasaUri(strUsername, albumid));
            picFeed = service.Query(qryPhoto);
            FileInfo fileInfo = new System.IO.FileInfo(fileName);
            FileStream fileStream = fileInfo.OpenRead();
            Uri postUri = new Uri(PicasaQuery.CreatePicasaUri(strUsername, albumid));
            PicasaEntry createdEntry = (PicasaEntry)service.Insert(postUri, fileStream, "image/jpeg", fileName);
            fileStream.Close();
            #endregion

            #region 編輯圖片資訊
            if (bChkEdit == true)
            {
                EditPhoto(createdEntry, fileName, strPhotoSummery);
            }
            #endregion
        }


        /// <summary>
        /// 取得 Album ID
        /// </summary>
        /// <param name="strUsername">帳號</param>
        /// <param name="strPassword">密碼</param>
        /// <param name="strAlbumname">相簿名稱</param>
        /// <returns></returns>
        public string GetAlbumid(string strUsername, string strPassword, string strAlbumname)
        {
            string albumid = string.Empty;
            PicasaService service = new PicasaService(strAlbumname);
            service.setUserCredentials(strUsername, strPassword);

            AlbumQuery query = new AlbumQuery(PicasaQuery.CreatePicasaUri(strUsername));
            PicasaFeed picFeed = service.Query(query);

            foreach (PicasaEntry entry in picFeed.Entries)
            {
                AlbumAccessor albAc = new AlbumAccessor(entry);
                if (albAc.AlbumTitle == strAlbumname)
                {
                    albumid = albAc.Id;
                    break;
                }
            }
            return albumid;
        }

        /// <summary>
        /// 編輯照片資訊
        /// </summary>
        /// <param name="picEntry">Picasa Entry</param>
        /// <param name="fileName">檔案名稱</param>
        /// <param name="strSummery">照片 summery</param>
        /// <returns></returns>
        public void EditPhoto(PicasaEntry picEntry, string fileName, string strSummery)
        {
            picEntry.Summary.Text = strSummery;
            PicasaEntry updatedEntry = (PicasaEntry)picEntry.Update(); 
        }

    }
}

 

3. 執行結果

image    image

image image

 

註 : 可參考 laneser 前輩的文章 Windows Mobile .NET CF] Hello Camera,將 Camera 拍攝的圖片,直接上傳到 Picasa 中,因為我手機無拍照功能,無法實作此部份。

 

4. 附錄 : 檔案下載

SmartDeviceProjectPicasa.rar