【问题标题】:Create image serving URL for new Blob file为新的 Blob 文件创建图像服务 URL
【发布时间】:2011-09-19 07:33:07
【问题描述】:

使用 ImagesService 转换上传的图片后,我想将其存储回一个新的 Blob 文件,并通过 ImagesService 提供的getServingUrl() 提供。

按照here 的描述将图像存储在新的 AppEngineFile 中工作正常,我可以使用开发服务器在本地打开和查看它。

但是,当将新 AppEngineFile 的 blobKey 传递给 ImagesService.getServingUrl() a 时

java.lang.IllegalArgumentException: 无法读取 blob。

抛出异常。任何想法可能是什么问题?这是我用来转换和存储上传图片的代码(blobKey和blobInfo对应的是上传的文件,不是新创建的)。

/* Transform image in existing Blob file */
Image originalImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
Transform verticalFlip = ImagesServiceFactory.makeVerticalFlip();
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Image newImage = imagesService.applyTransform(verticalFlip, originalImage);

/* Store newImage in an AppEngineFile */
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile(blobInfo.getContentType());
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
ByteBuffer buffer = ByteBuffer.wrap(newImage.getImageData());
writeChannel.write(buffer);

/* closeFinally assigns BlobKey to new file object */
writeChannel.closeFinally();
BlobKey newBlobKey = fileService.getBlobKey(file);

编辑:

上面的代码是正确的,问题是使用newBlobKey.toString()而不是newBlobKey.getKeyString()来存储新blob键的字符串表示。

【问题讨论】:

    标签: image google-app-engine blob


    【解决方案1】:

    您为什么要这样做?一旦你转换了一张图片,它就会被缓存起来,无论如何它总是很快的。如果您真的想保存它,只需使用 urlfetch 读取数据并将它们存储在 BlobStore 中;-)

    【讨论】:

    • 一个 imageServingUrl 可以裁剪和调整图片大小,但是 imageService 可以做的更像是旋转。因此,想法是将图像上传到 Blob 存储,旋转它,将其存储回 Blob 存储,然后创建旋转图像的 imageServingUrl 以提供不同比例的预览。上面的代码确实有效,我必须更新问题/添加答案。
    【解决方案2】:

    在问题中发布的代码末尾执行以下操作时可以正常工作:

    String url = imagesService.getServingUrl(newBlobKey)
    

    然后可以使用该 URL 来缩放和裁剪新图像,如文档中所述 http://code.google.com/appengine/docs/java/images/overview.html#Transforming_Images_from_the_Blobstore

    【讨论】:

      猜你喜欢
      • 2020-07-27
      • 2023-03-19
      • 2017-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-31
      • 2020-06-25
      相关资源
      最近更新 更多