Reputation: 35316
I'm trying to understand how to deal with GAE Image api, to automatically convert between image format to JPEG.
Here is my code:
byte[] oldImageData = model.getImage();
Image oldImage = ImagesServiceFactory.makeImage(oldImageData);
LOG.info("Image format - " + oldImage.getFormat().toString());
model.setImage(oldImage.getImageData()); // byte array must be JPEG
The oldImageData
byte array here can be a byte array of a JPEG or PNG image, or worst case not an image.
Finally, what I need to do is to make sure that oldImageData
is a byte array for a JPEG
image.
Upvotes: 2
Views: 974
Reputation: 80340
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Image oldImage = ImagesServiceFactory.makeImage(oldImageData);
// this throws an exception if data is not image or unsupported format
// you can wrap this in try..catch and act accordingly
oldImage.getFormat();
// this is a dummy transform that does nothing
Transform transform = ImagesServiceFactory.makeCrop(0.0, 0.0, 1.0, 1.0);
// setting the output to JPEG
OutputSettings outputSettings = new OutputSettings(ImagesService.OutputEncoding.JPEG);
outputSettings.setQuality(100);
// apply dummy transform and output settings
Image newImage = imagesService.applyTransform(transform, oldImage, outputSettings);
byte[] newImageData = newImage.getImageData();
Upvotes: 6
Reputation: 9914
You can check the magic number
of any image and based on that you can take action. The following link will give you the list of magic numbers and you can use that in a utilty for future purpose
Upvotes: 0