我的功能对我有用,但缩略图图像文件大小比原始文件大。
缩略图文件:50Kb
我的功能
public static void thumbnailImage(String filename, int width, int height) throws IOException{
GcsService gcsService = GcsServiceFactory.createGcsService(new RetryParams.Builder()
.initialRetryDelayMillis(10)
.retryMaxAttempts(10)
.totalRetryPeriodMillis(15000)
.build());
AppIdentityService appIdentity = AppIdentityServiceFactory.getAppIdentityService();
ImagesService imagesService = ImagesServiceFactory.getImagesService();
// Make an image from a Cloud Storage object, and transform it.
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/" + appIdentity.getDefaultGcsBucketName() +"/"+ filename);
Image thumb = null;
try{
Image blobImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
Transform resize = ImagesServiceFactory.makeResize(width, height);
thumb = imagesService.applyTransform(resize, blobImage);
}catch(Exception e){
e.printStackTrace();
}
String extension = Files.getFileExtension(filename);
filename = stripExtension(filename);
filename = String.format("%1$s_%2$s.%3$s", filename, thumbnail, extension);
if(thumb!=null)
// Write the transformed image back to a Cloud Storage object.
gcsService.createOrReplace(
new GcsFilename(appIdentity.getDefaultGcsBucketName(), filename),
new GcsFileOptions.Builder().mimeType("image/jpeg").acl("public-read").build(),
ByteBuffer.wrap(thumb.getImageData()));
}
原始文件仅为30KB
,但缩略图为50KB
。它们具有相同的V分辨率和H分辨率96dpi
以及位深度24
。还有其他我错过的东西吗?有很多类似的问题,我不确定我的问题是否重复。
编辑:
这是谷歌云上的链接
https://storage.googleapis.com/exalted-etching-131403.appspot.com/pro/abc/door-baby-bouncer.jpg
缩略图:
答案 0 :(得分:1)
感谢tx802和jterrace
我现在找到了解决方案。我将OutputSettings添加到转换函数以将编码更改为JPEG
thumb = imagesService.applyTransform(resize, blobImage, new OutputSettings(OutputEncoding.JPEG));
现在我的缩略图只有5Kb。