6

App Engine BlobStore を使用して、アップロードされた画像の高さと幅を取得する必要があります。私が次のコードを使用したことを見つけるために:

try {
            Image im = ImagesServiceFactory.makeImageFromBlob(blobKey);

            if (im.getHeight() == ht && im.getWidth() == wd) {
                flag = true;
            }
        } catch (UnsupportedOperationException e) {

        }

画像をアップロードして BlobKey を生成できますが、Blobkey を makeImageFromBlob() に渡すと、次のエラーが生成されます。

java.lang.UnsupportedOperationException: 画像データがありません

この問題を解決する方法、または BlobKey から直接画像の高さと幅を見つける他の方法。

4

3 に答える 3

7

Image 自体のほとんどのメソッドは、現在 UnsupportedOperationException をスローします。そこで、com.google.appengine.api.blobstore.BlobstoreInputStream.BlobstoreInputStream を使用して blobKey からデータを操作しました。これで、画像の幅と高さを取得できます。

byte[] data = getData(blobKey);
Image im = ImagesServiceFactory.makeImage(data);
if (im.getHeight() == ht && im.getWidth() == wd) {}
private byte[] getData(BlobKey blobKey) {
    InputStream input;
    byte[] oldImageData = null;
    try {
        input = new BlobstoreInputStream(blobKey);
                ByteArrayOutputStream bais = new ByteArrayOutputStream();
        byte[] byteChunk = new byte[4096];
        int n;
        while ((n = input.read(byteChunk)) > 0) {
            bais.write(byteChunk, 0, n);
        }
        oldImageData = bais.toByteArray();
    } catch (IOException e) {}

    return oldImageData;

}
于 2012-07-26T05:49:23.480 に答える
4

Guava を使用できる場合、実装は簡単に実行できます。

public static byte[] getData(BlobKey blobKey) {
    BlobstoreInputStream input = null;
    try {
        input = new BlobstoreInputStream(blobKey);
        return ByteStreams.toByteArray(input);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        Closeables.closeQuietly(input);
    }
}

残りは同じままです。

于 2012-09-18T08:47:57.810 に答える
0

もう 1 つの可能性は、画像を無用に変換することです (0 度の回転など)。

Image oldImage = ImagesServiceFactory.makeImageFromFilename(### Filepath ###);
Transform transform = ImagesServiceFactory.makeRotate(0);
oldImage = imagesService.applyTransform(transform,oldImage);

その変換の後、期待どおりに画像の幅と高さを取得できます。

oldImage.getWidth();

これが機能したとしても、この変換はパフォーマンスに悪影響を及ぼします ;)

于 2016-10-14T08:38:10.790 に答える