使用epeg生成无压缩的缩略图

在生成图片的缩略图时,由于获取的缩略图长宽比例与原图的比例不同时就会导致生成的缩略图变形,为了生成无压缩变形的缩略图,比较方便编程实现的方式是:使用Epeg库。该库能够为JPEG格式图片生成缩略图,如果需要为其他格式的图片生成缩略图,那暂时只有先转换成JPEG图片的格式。
说多了,一切都是浮云,直接上代码:epeg缩略图生成方式:

    char *epeg_trim_and_scale(const char *pszSrcFile, 
    size_t thumb_height, 
    size_t thumb_width, 
    int *pixels_size)
{
    Epeg_Image *im;
    unsigned char *thumb_pic=NULL;
    unsigned char *mem_trim_out = NULL;
    int trim_size = 0;

    *pixels_size = 0;
    im = epeg_file_open(pszSrcFile);   
    do{
        if(!im){
            break;
        }
        int w, h;      
        int x,y;       
        int im_w, im_h;    
        int ret;
        int n = 0;

        epeg_size_get(im, &w, &h);             
        if (thumb_width < 0) {     
            // This means we want %thumb_width of w    
            thumb_width = w * (-thumb_width) / 100;       
            }       
        if (thumb_height < 0) {    
            // This means we want %thumb_height of h       
            thumb_height = h * (-thumb_height) / 100;       
        }      
        im_w = w;      
        im_h = h;      
        x = 0;     
        y = 0;         
        if (im_w * thumb_height > im_h * thumb_width) {         
            x = (im_w - thumb_width * im_h / thumb_height) / 2;     
        } else {            
            y = (im_h - im_w * thumb_height / thumb_width) / 2;     
        }               
        w = x > 0 ? im_w - x * 2 : im_w;        
        h = y > 0 ? im_h - y * 2 : im_h;                
        //trim      
        if(x || y){         
            epeg_decode_bounds_set(im, x, y, w, h);         
            epeg_quality_set(im, 80);

            epeg_memory_output_set(im,&mem_trim_out,&trim_size);
            epeg_trim(im);
            epeg_close(im);
            //conitnue to scale
            im = epeg_memory_open(mem_trim_out,trim_size);
            if(!im){                
                break;          
            }
        }

        epeg_decode_size_set(im, thumb_width, thumb_height);    
        epeg_quality_set(im, 80);       
        epeg_thumbnail_comments_enable (im, 1);     
        epeg_memory_output_set(im, &thumb_pic, &n);//set the results
        ret = epeg_encode(im);
        OS_LOG(LOG_MODE_INTRANET_TRAN, LOGLV_DEBUG,"epeg_trim_and_scale return %d", ret);
        if(ret == 0){
            *pixels_size = n;
        }
        epeg_close(im);      

    }while(0);

    if(mem_trim_out) {
        free(mem_trim_out);
    }

    return (char*)thumb_pic;

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值