2011-12-08 68 views
3

我正在使用像圖庫等應用程序。 這允許用戶從他的電腦中選擇文件並上傳圖像。上傳圖片後,該圖片將顯示在圖庫中。 我將圖像保存在blobstore中,並通過blobkey獲取。 我婉得到的縮略圖(即從THRE原始圖像大小) 在服務器端,我嘗試使用圖像API來調整基於它TE原來一個人的類BlobKey,這樣GAE - 獲取服務器端的圖像寬度和高度

public String getImageThumbnail(String imageKey) { 
    BlobKey blobKey = new BlobKey(imageKey); 
    Image oldImage = ImagesServiceFactory.makeImageFromBlob(blobKey); 
    /* 
    * can not get width anf height of "oldImage" 
    * then I try to get imageDate of old Image to create a new image like this 
    */ 
    byte[] oldImageData = oldImage.getImageData(); 

    Image newImage1 = ImagesServiceFactory.makeImage(oldImageData); 
/* 
* however 
* oldImage.getImageData(); return null/"" 
* and cause the Exception when call this ImagesServiceFactory.makeImage(oldImageData) 
*/ 

}

有沒有人曾與服務器端的圖像API的工作,請讓我知道如何獲得的寬度以及從一個類BlobKey或字節創建圖像的高度[]

+0

這會是什麼語言我的問題? – Jonny

回答

0

Image對象有身高/體重細節:

int width = oldImage.getWidth(); 
int height = oldImage.getHeight(); 
+0

感謝Splix的回覆, 但是,我試圖使用它,但沒有結果。 我收到了這個 ava.lang.UnsupportedOperationException:沒有圖像數據可用 .... –

+1

哦,是的,這是已知問題,請查看http://code.google.com/p/googleappengine/issues/detail?id = 4757 –

1

感謝所有閱讀 我已經解決使用這種

ImagesService imagesService = ImagesServiceFactory.getImagesService(); 
    BlobKey blobKey = new BlobKey(imageKey); 
    BlobInfo blobInfo = new BlobInfoFactory().loadBlobInfo(blobKey); 
    byte[] bytes = blobstoreService.fetchData(blobKey, 0, blobInfo.getSize()); 
    Image oldImage = ImagesServiceFactory.makeImage(bytes); 
    int w = oldImage.getWidth(); 
    int h = oldImage.getHeight(); 
+1

如果blob大小超過1MB,此代碼將拋出異常。你需要在幾個塊中讀取blob。正如Igor所指出的,請閱讀http://code.google.com/p/googleappengine/issues/detail?id=4757 – Ena