App Engine和云端存储生成缩略图图像的文件大小比原始图像大 [英] App Engine and Cloud Storage I generate thumbnail image has larger file size than original image

查看:81
本文介绍了App Engine和云端存储生成缩略图图像的文件大小比原始图像大的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的功能适用于我,但缩略图文件大小大于原始文件大小。 原始文件:30Kb b
$ b

缩略图文件:50Kb



My Function

  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();
//从Cloud Storage对象制作图像并对其进行转换。
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
BlobKey blobKey = blobstoreService.createGsBlobKey(/ gs /+ appIdentity.getDefaultGcsBucketName()+/+ filename);

Image thumb = null;
尝试{
Image blobImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
Transform resize = ImagesServiceFactory.makeResize(width,height);
thumb = imagesService.applyTransform(resize,blobImage);

} catch(Exception e){
e.printStackTrace();
}
字符串扩展名= Files.getFileExtension(filename);
filename = stripExtension(filename);
filename = String.format(%1 $ s_%2 $ s。%3 $ s,文件名,缩略图,扩展名);

if(thumb!= null)
//将转换后的图像写回云存储对象。
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 。还有什么我错过了吗?有很多类似的问题,我不确定我的问题是否重复。



编辑:



这是Google云端的连结

https://storage.googleapis.com/exalted-etching-131403.appspot.com/pro/abc/door-baby-bouncer.jpg



缩略图:



https://storage.googleapis.com/exalted-etching-131403.appspot.com/pro/abc /door-bouncer_thumbnail.jpg

解决方案

感谢tx802和jterrace

我现在找到了解决方案。我添加OutputSettings来转换函数以将Encoding更改为JPEG

  thumb = imagesService.applyTransform(resize,blobImage,new OutputSettings(OutputEncoding。 JPEG)); 

现在我的缩略图只有5Kb。


My function work for me but the thumbnail image file size is larger than original file size.

Original File: 30Kb

Thumbnail File: 50Kb

My Function

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()));
}

The original file is only 30KB, but the thumbnail is 50KB. They have the same V-Resolution and H-Resolution 96dpi and Bit Depth 24. Are there something else I have missed? There are a lot of similar Questions, I'm not sure if my question is duplicated.

EDIT:

this is link on google cloud

https://storage.googleapis.com/exalted-etching-131403.appspot.com/pro/abc/door-baby-bouncer.jpg

thumbnail:

https://storage.googleapis.com/exalted-etching-131403.appspot.com/pro/abc/door-baby-bouncer_thumbnail.jpg

解决方案

Thank tx802 and jterrace

I found the solution now. I add OutputSettings to transform function to change Encoding to JPEG

thumb = imagesService.applyTransform(resize, blobImage, new OutputSettings(OutputEncoding.JPEG));

Right now my thumbnail image is only 5Kb.

这篇关于App Engine和云端存储生成缩略图图像的文件大小比原始图像大的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆