GAE - 在服务器端获取图像的宽度和高度

3

我正在处理一款类似于图片库的应用。这使得用户可以从计算机选择文件并上传图像。上传图像后,该图像将显示在库中。 我将图像保存在blobstore中,并通过blobkey获取它。 我想要获取一个缩略图(从原始图像调整大小而来) 在服务器端,我尝试使用图像API根据其blobkey调整原始图像的大小,例如:

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或byte[]创建的图像的宽度和高度。


这是哪种编程语言? - Jonny
2个回答

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

0

图像对象具有高度/宽度细节:

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

感谢Splix的回复, 然而,我尝试使用它,但没有结果。 我得到了这个ava.lang.UnsupportedOperationException: 没有可用的图像数据 - kelly Nguyen
1
哦,是的,这是已知问题,请查看http://code.google.com/p/googleappengine/issues/detail?id=4757。 - Igor Artamonov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接