本文介绍了如何使用C ++中的预定义头文件保存图像或更改图像格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,



有没有办法在Linux环境下使用c ++使用预定义的头文件(不使用第三方应用程序)保存图像或更改图像格式。 br $> b $ b

提前致谢。



我的尝试:



我在msdn for windows中找到了相同req的代码:



#include< windows.h>

#include< gdiplus.h>

#include< stdio.h>



#include< gdiplusimagecodec.h>

#pragma comment(lib,gdiplus.lib)





使用命名空间Gdiplus;



int GetEncoderClsid(const WCHAR * format) ,CLSID * pClsid)

{

UINT num = 0; //图像编码器的数量

UINT size = 0; //图像编码器阵列的大小(以字节为单位)



ImageCodecInfo * pImageCodecInfo = NULL;



GetImageEncodersSize( & num,& size);



if(size == 0)

return -1; //失败



pImageCodecInfo =(ImageCodecInfo *)(malloc(size));

if(pImageCodecInfo == NULL)

返回-1; //失败



GetImageEncoders(num,size,pImageCodecInfo);



for(UINT j = 0 ; j
{

if(wcscmp(pImageCodecInfo [j] .MimeType,format)== 0)

{

* pClsid = pImageCodecInfo [j] .Clsid;

免费(pImageCodecInfo);

返回j; //成功

}

}



免费(pImageCodecInfo);





返回-1; //失败

}





int _tmain(int argc,_TCHAR * argv [])

{

//初始化GDI +。

GdiplusStartupInput gdiplusStartupInput;

ULONG_PTR gdiplusToken;

GdiplusStartup(& gdiplusToken,& gdiplusStartupInput,NULL);



CLSID encoderClsid;

EncoderParameters encoderParameters;

ULONG品质;

状态统计;



//从磁盘获取图片。

图片* image = new Image(LC:\\Users \\ Elisa \\Documents \\Visual Studio 2010 \\Projects \\ Image_Conversion_2 \\Penguins.jpg) ;



//获取JPEG编码器的CLSID。

GetEncoderClsid(Limage / jpeg,& encoderClsid);



//在我们调用Image :: Save之前,我们必须初始化一个

// E ncoderParameters对象。 EncoderParameters对象

//有一个EncoderParameter对象数组。在这个

//的情况下,数组中只有一个EncoderParameter对象。

//一个EncoderParameter对象有一个值数组。

//在这种情况下,数组中只有一个值(类型为ULONG)

//。我们将这个值从0到100变化。



encoderParameters.Count = 1;

encoderParameters.Parameter [0] .Guid = EncoderQuality;

encoderParameters.Parameter [0] .Type = EncoderParameterValueTypeLong;

encoderParameters.Parameter [0] .NumberOfValues = 1;



//将图像保存为质量等级为0的JPEG。

quality = 0;

encoderParameters.Parameter [0] .Value =&质量;

stat = image->保存(LShapes001.jpg,& encoderClsid,& encoderParameters);



if(stat == Ok)

wprintf(L%s已成功保存。\ n,LShapes001.jpg);

else

wprintf(L%d尝试保存%s failed.\ n,stat,LShapes001.jpg);



//将图像保存为质量等级为50的JPEG。

质量= 50;

encoderParameters.Parameter [0] .Value =& quality;

st at = image->保存(LShapes050.jpg,& encoderClsid,& encoderParameters);



if(stat == Ok)

wprintf(L%s已成功保存。\ n,LShapes050.jpg);

其他

wprintf(L%d尝试保存%s failed.\,stat,LShapes050.jpg);



//将图像保存为质量等级为100的JPEG 。

质量= 100;

encoderParameters.Parameter [0] .Value =& quality;

stat = image->保存( LShapes100.jpg,& encoderClsid,& encoderParameters);



if(stat == Ok)

wprintf(L %s已成功保存。\ n,LShapes100.jpg);

其他

wprintf(L%d尝试保存%s失败。 \\ n,stat,LShapes100.jpg);



删除图片;

GdiplusShutdown(gdiplusToken);

返回0;

}

Hello,

Is there any way to save images or change image formats using predefined header files (without using third party applications) using c++ in Linux environment.

Thanks in advance.

What I have tried:

I found this code for the same req in msdn for windows:

#include < windows.h >
#include < gdiplus.h >
#include < stdio.h >

#include <gdiplusimagecodec.h>
#pragma comment (lib, "gdiplus.lib")


using namespace Gdiplus;

int GetEncoderClsid(const WCHAR* format, CLSID* pClsid)
{
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 )
{
*pClsid = pImageCodecInfo[j].Clsid;
free(pImageCodecInfo);
return j; // Success
}
}

free(pImageCodecInfo);


return -1; // Failure
}


int _tmain(int argc, _TCHAR* argv[])
{
// Initialize GDI+.
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);

CLSID encoderClsid;
EncoderParameters encoderParameters;
ULONG quality;
Status stat;

// Get an image from the disk.
Image* image = new Image(L"C:\\Users\\Elisa\\Documents\\Visual Studio 2010\\Projects\\Image_Conversion_2\\Penguins.jpg");

// Get the CLSID of the JPEG encoder.
GetEncoderClsid(L"image/jpeg", &encoderClsid);

// Before we call Image::Save, we must initialize an
// EncoderParameters object. The EncoderParameters object
// has an array of EncoderParameter objects. In this
// case, there is only one EncoderParameter object in the array.
// The one EncoderParameter object has an array of values.
// In this case, there is only one value (of type ULONG)
// in the array. We will let this value vary from 0 to 100.

encoderParameters.Count = 1;
encoderParameters.Parameter[0].Guid = EncoderQuality;
encoderParameters.Parameter[0].Type = EncoderParameterValueTypeLong;
encoderParameters.Parameter[0].NumberOfValues = 1;

// Save the image as a JPEG with quality level 0.
quality = 0;
encoderParameters.Parameter[0].Value = &quality;
stat = image->Save(L"Shapes001.jpg", &encoderClsid, &encoderParameters);

if(stat == Ok)
wprintf(L"%s saved successfully.\n", L"Shapes001.jpg");
else
wprintf(L"%d Attempt to save %s failed.\n", stat, L"Shapes001.jpg");

// Save the image as a JPEG with quality level 50.
quality = 50;
encoderParameters.Parameter[0].Value = &quality;
stat = image->Save(L"Shapes050.jpg", &encoderClsid, &encoderParameters);

if(stat == Ok)
wprintf(L"%s saved successfully.\n", L"Shapes050.jpg");
else
wprintf(L"%d Attempt to save %s failed.\n", stat, L"Shapes050.jpg");

// Save the image as a JPEG with quality level 100.
quality = 100;
encoderParameters.Parameter[0].Value = &quality;
stat = image->Save(L"Shapes100.jpg", &encoderClsid, &encoderParameters);

if(stat == Ok)
wprintf(L"%s saved successfully.\n", L"Shapes100.jpg");
else
wprintf(L"%d Attempt to save %s failed.\n", stat, L"Shapes100.jpg");

delete image;
GdiplusShutdown(gdiplusToken);
return 0;
}

推荐答案


这篇关于如何使用C ++中的预定义头文件保存图像或更改图像格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-31 23:48