2011-02-11 54 views
1

我写功能,在Windows上使用GDI +来压缩图像,它的工作很好,如何在Linux上压缩图像?

void ImageProcessorImpl::compressImpl(const std::string& path, int size, UInt8 quality) 
{ 
    HBITMAP hbmReturn = NULL; 
    Bitmap* bmPhoto = NULL; 

    std::wstring upath; 
    UnicodeConverter::toUTF16(path, upath); 

    // make source file close automatically, Bitmap detructor will be called 
    { 
     Bitmap image(upath.c_str()); 

     int srcWidth = image.GetWidth(); 
     int srcHeight = image.GetHeight(); 

     float percent = 0; 
     int destX = 0, destY = 0; 
     if (srcWidth > srcHeight) 
     { 
      percent = ((float)size/(float)srcWidth); 
      destX = (int)((size - (srcWidth * percent))/2); 
     } 
     else 
     { 
      percent = ((float)size/(float)srcHeight); 
      destY = (int)((size - (srcHeight * percent))/2); 
     } 

     if (percent >= 1.0f) 
      return; // skip compress 

     int destWidth = (int)(srcWidth * percent); 
     int destHeight = (int)(srcHeight * percent); 

     bmPhoto = new Bitmap(destWidth, destHeight, PixelFormat24bppRGB); 
     bmPhoto->SetResolution(image.GetHorizontalResolution(), image.GetVerticalResolution()); 

     Graphics *grPhoto = Graphics::FromImage(bmPhoto); 
     Color colorW(255, 255, 255, 255); 
     grPhoto->Clear(colorW); 
     grPhoto->SetInterpolationMode(InterpolationModeHighQualityBicubic); 
     grPhoto->DrawImage(&image, Rect(destX, destY, destWidth, destHeight)); 

     bmPhoto->GetHBITMAP(colorW, &hbmReturn); 
     delete grPhoto; 
    } // end source image file, Bitmap image(upath.c_str()); 

    // find appropriate encoder, jpeg 
    CLSID encoderClsid; 
    getEncoderClsid(L"image/jpeg", &encoderClsid); 

    // set output quality for jpeg alone 
    EncoderParameters encoderParameters; 
    setEncoderQuality(&encoderParameters, &quality); 

    // output to image file with desired quality 
    bmPhoto->Save(upath.c_str(), &encoderClsid, &encoderParameters); 

    // release resources 
    delete bmPhoto; 
    DeleteObject(hbmReturn); 
} 

int ImageProcessorImpl::getEncoderClsid(const WCHAR* format, void* clsid) 
{ 
    UINT num = 0; // number of image encoders 
    UINT size = 0; // size of the image encoder array in bytes 

    ImageCodecInfo* pImageCodecInfo = NULL; 
    GetImageEncodersSize(&num, &size); 
    if(size == 0) 
     return -1; // Failure 

    pImageCodecInfo = (ImageCodecInfo*)(malloc(size)); 
    if(pImageCodecInfo == NULL) 
     return -1; // Failure 

    GetImageEncoders(num, size, pImageCodecInfo); 
    for (UINT j = 0; j < num; ++j) 
    { 
     if (wcscmp(pImageCodecInfo[j].MimeType, format) == 0) 
     { 
      *(CLSID*)clsid = pImageCodecInfo[j].Clsid; 
      free(pImageCodecInfo); 
      return j; //Success 
     } 
    } 

    free(pImageCodecInfo); 
    return -1; // Failure 
} 

void ImageProcessorImpl::setEncoderQuality(void* params, UInt8* quality) 
{ 
    EncoderParameters* encoderParams = (EncoderParameters*)params; 
    encoderParams->Count = 1; 
    encoderParams->Parameter[0].Guid = EncoderQuality; 
    encoderParams->Parameter[0].Type = EncoderParameterValueTypeLong; 
    encoderParams->Parameter[0].NumberOfValues = 1; 

    encoderParams->Parameter[0].Value = quality; 
} 

但是,我想在Linux上,这个功能,我不知道是什么LIB我可以用它来实现这样的在linux上的函数,谁能帮助我? Thnx

回答

2

您可以使用ImageMagick库或netpbm库。 Netpbm也有命令行工具来处理图像。

+0

也许netpbm对我来说是不错的选择,因为它已经被其他网站用来处理图像;同样,我需要一个轻量级高性能的lib来压缩各种上传的图像,基本上只是为了加快浏览下载的这些图像而使其更小和压缩;我会试试看,谢谢 – tiplip 2011-02-11 12:56:45

1

OpenCV为所有类型的图像处理以及诸如图像压缩之类的简单事物提供了一个易于使用的界面。对于图像压缩而言,这可能有点矫枉过正,但如果你打算用这些图像做其他事情,我想这可能会有所帮助。