java – 如何使用Google App Engine Blobstore压缩图像

我们有一个应用程序服务于blobstore的一系列图像.一个
示例在这里:

http://lh4.ggpht.com/f76xUkRZLRkb_Qz5uu82TX3LoBRh4eYb9hxYwMRMLCk5ghO_OL0DW2v4rRnkewUyDWfBuBttgbUvuJJXwtFQosEB=s0

这是一个巨大的png,因此下载量为536K.

如果我们将其调整为400,它仍然很大(263k):

http://lh4.ggpht.com/f76xUkRZLRkb_Qz5uu82TX3LoBRh4eYb9hxYwMRMLCk5ghO_OL0DW2v4rRnkewUyDWfBuBttgbUvuJJXwtFQosEB=s400

我们怎样才能以更好的方式请求或存储图片
压缩?我们的应用程序有一个移动客户端,并等待
273K让它变得非常慢.

最佳答案
Images API.压缩和调整图像大小:

// get image from blobstore
ImagesService imagesService = ImagesServiceFactory.getImagesService();
Image oldImage = ImagesServiceFactory.makeImageFromBlob(blobKey); 

// compress & resize it
OutputSettings settings = new OutputSettings(ImagesService.OutputEncoding.JPEG);
settings.setQuality(90);
Transform transform = ImagesServiceFactory.makeResize(newWidth, newHeight) 
Image newImage = imagesService.applyTransform(transform, oldImage, settings);
byte[] blobData = newImage.getImageData();

//save data to blobstore
FileService fileService = FileServiceFactory.getFileService();
AppEngineFile file = fileService.createNewBlobFile("image/jpeg", someFilename);
FileWriteChannel writeChannel = fileService.openWriteChannel(file, true);
writeChannel.write(ByteBuffer.wrap(blobData));
writeChannel.closeFinally();

// get the blobKey to newly written data
BlobKey blobKey = fileService.getBlobKey(file);

转载注明原文:java – 如何使用Google App Engine Blobstore压缩图像 - 代码日志