I need to retrieve the height and width of uploaded image using App Engine BlobStore. For finding that i used following code :
try {
Image im = ImagesServiceFactory.makeImageFromBlob(blobKey);
if (im.getHeight() == ht && im.getWidth() == wd) {
flag = true;
}
} catch (UnsupportedOperationException e) {
}
i can upload the image and generate the BlobKey but when pass the Blobkey to makeImageFromBlob(), it generate the following error:
java.lang.UnsupportedOperationException: No image data is available
How to solve this problem or any other way to find image height and width directly from BlobKey.
If you can use Guava, the implementation is easier to follow:
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);
}
}
The rest remains the same.
Most of the methods on the Image itself will currently throw UnsupportedOperationException. So i used com.google.appengine.api.blobstore.BlobstoreInputStream.BlobstoreInputStream to manipulate data from blobKey. That's way i can get image width and height.
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;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With