获取Image应用引擎的宽度和高度

时间:2013-01-24 20:44:35

标签: java google-app-engine

ImagesService imagesService = ImagesServiceFactory.getImagesService();
BlobKey bk = currentProfile.getProfileBlobKey();
Image oldImage = ImagesServiceFactory.makeImageFromBlob(bk);
Transform resize = ImagesServiceFactory.makeResize(500, 500);
Image newImage = imagesService.applyTransform(resize, oldImage);
int imageWidth = newImage.getWidth();
int imageHeight = newImage.getHeight();

此代码似乎不起作用,因为ImagesServiceFactory.makeImageFromBlob(bk);不返回真实的图像。有没有人知道这个的解决方法?这似乎是预期的行为。我们讨论了这个问题here但他们没有解决方法来获取高度和宽度只是字节[]

2 个答案:

答案 0 :(得分:2)

试试这个:

FileService fileService = FileServiceFactory.getFileService();
AppEngineFile blobFile = fileService.getBlobFile(blobKey);
FileReadChannel readChannel = fileService.openReadChannel(blobFile, false);
byte[] imageData = getBytes(Channels.newInputStream(readChannel));
Image oldImage = ImagesServiceFactory.makeImage(imageData);
// now you have the real Image

这个用于读取输入流的小片段:

public static byte[] getBytes(InputStream is) throws IOException {
    ByteArrayOutputStream buffer = new ByteArrayOutputStream();

    int len;
    byte[] data = new byte[10000];
    while ((len = is.read(data, 0, data.length)) != -1) {
        buffer.write(data, 0, len);
    }

    buffer.flush();
    return buffer.toByteArray();
}

答案 1 :(得分:0)

您是否尝试过ImageReader类?