简体   繁体   English

如何在Linux上压缩映像?

[英]How to compress image on linux?

I am writing function to compress image using GDI+ on windows, and it's working well, 我正在编写在Windows上使用GDI +压缩图像的函数,并且运行良好,

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

But, I want to have this function on linux, I don't know what lib I can use to implement such function on linux, who can help me? 但是,我想在linux上使用此功能,但我不知道我可以在Linux上使用哪些lib来实现此功能,谁能帮助我? Thnx 日Thnx

You can use the ImageMagick library or netpbm library. 您可以使用ImageMagick库或netpbm库。 Netpbm also has command line tools to manipulate images. Netpbm还具有用于处理图像的命令行工具。

OpenCV provides an easy to use interface for all types of image processing as well as simple things such as image compression. OpenCV为所有类型的图像处理以及诸如图像压缩之类的简单操作提供了易于使用的界面。 It may be a bit of an overkill for image compression only, but I suppose it may helpful if you plan on doing other things with these images. 仅对于图像压缩而言,这可能有点过大,但是我想如果您打算对这些图像进行其他操作,则可能会有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM