我正在尝试了解如何处理GAE Image api,以自动将图像格式转换为JPEG格式。
这是我的代码:
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
这里的oldImageData
字节数组可以是JPEG或PNG图像的字节数组,或者最坏的情况不是图像。
最后,我需要做的是确保oldImageData
是JPEG
图像的字节数组。
答案 0 :(得分:6)
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();
答案 1 :(得分:0)
您可以查看任何图片的magic number
,并根据您的情况采取行动。以下链接将为您提供幻数列表,您可以在实用程序中将其用于将来