1 #include "ImageRec.h" 2 using namespace std; 3 4 void ImageRec::getImageFile() 5 { 6 fstream img("1.jpg", fstream::in | ios::binary); 7 fstream imgout("2.jpg", fstream::out | ios::binary); 8 img.seekg(0, img.end); 9 long length = img.tellg(); 10 img.seekg(0, img.beg); 11 if (length>0) 12 { 13 char* buffer = new char[length]; 14 img.read(buffer, length); 15 16 17 18 imgout.write(buffer,length); 19 delete[] buffer; 20 } 21 img.close(); 22 imgout.close(); 23 24 return ; 25 }
C++ 文件讀寫。怎么得到圖片的像素?
https://github.com/irwir/CxImage
https://github.com/wavescholar/CxImageVS2010 這個可以用
使用方法https://www.cnblogs.com/lidabo/p/6923426.html

#include <iostream> #include <string> #include <fstream> #include "../CxImage/xfile.h" #include "../CxImage/ximajpg.h" #include "../CxImage/ximabmp.h" using namespace std; #pragma comment(lib,"CxImage.lib") int main() { fstream img("1.jpg", fstream::in | ios::binary); CxImage image; // bmp -> jpg image.Load("1.jpg", CXIMAGE_FORMAT_JPG); if (image.IsValid()) { /*if (!image.IsGrayScale()) image.IncreaseBpp(24); image.SetJpegQuality(80); image.Save("1.bmp", CXIMAGE_FORMAT_BMP);*/ CxImage imageG; image.SplitRGB(nullptr, &imageG, nullptr); imageG.Save("1g.jpg", CXIMAGE_FORMAT_JPG); //imageG.arr } // png -> tif //image.Load("image.png", CXIMAGE_FORMAT_PNG); //if (image.IsValid()) { // image.Save("image.tif", CXIMAGE_FORMAT_TIF); //} //fstream imgout("2.jpg", fstream::out | ios::binary); img.seekg(0, img.end); long length = img.tellg(); img.seekg(0, img.beg); if (length > 0) { char* buffer = new char[length]; //char* buffer = new char[length]; img.read(buffer, length); CxFile* fimg; memset(&fimg, 0, length); //fimg() fimg->Write(buffer, length,1); CxImageJPG jpg; bool s= jpg.Decode(fimg); //imgout.write(buffer, length); delete[] buffer; } img.close(); //imgout.close(); //jpg.get // ximajpg std::cout << "Hello World!\n"; }
配置很重要
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ClCompile> <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> <InlineFunctionExpansion>Default</InlineFunctionExpansion> <FunctionLevelLinking>false</FunctionLevelLinking> <Optimization>Disabled</Optimization> <SuppressStartupBanner>true</SuppressStartupBanner> <WarningLevel>Level3</WarningLevel> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat> <AdditionalIncludeDirectories>..\jpeg;..\png;..\zlib;..\mng;..\tiff;..\j2k;..\jasper\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;JAS_WIN_MSVC_BUILD;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <AssemblerListingLocation>.\Debug\</AssemblerListingLocation> <PrecompiledHeaderOutputFile>.\Debug\cximage.pch</PrecompiledHeaderOutputFile> <ObjectFileName>.\Debug\</ObjectFileName> <ProgramDataBaseFileName>.\Debug\</ProgramDataBaseFileName> <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks> </ClCompile> <Link> <SubSystem>Console</SubSystem> <GenerateDebugInformation>true</GenerateDebugInformation> <AdditionalLibraryDirectories>..\x64\Debug\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> </Link> </ItemDefinitionGroup>

<?xml version="1.0" encoding="utf-8"?> <Project DefaultTargets="Build" xmlns="http://schemas.microsoft.com/developer/msbuild/2003"> <ItemGroup Label="ProjectConfigurations"> <ProjectConfiguration Include="Debug|Win32"> <Configuration>Debug</Configuration> <Platform>Win32</Platform> </ProjectConfiguration> <ProjectConfiguration Include="Release|Win32"> <Configuration>Release</Configuration> <Platform>Win32</Platform> </ProjectConfiguration> <ProjectConfiguration Include="Debug|x64"> <Configuration>Debug</Configuration> <Platform>x64</Platform> </ProjectConfiguration> <ProjectConfiguration Include="Release|x64"> <Configuration>Release</Configuration> <Platform>x64</Platform> </ProjectConfiguration> </ItemGroup> <PropertyGroup Label="Globals"> <VCProjectVersion>16.0</VCProjectVersion> <Keyword>Win32Proj</Keyword> <ProjectGuid>{cf48985e-1232-48ba-9ba3-5101a0d51269}</ProjectGuid> <RootNamespace>ConsoleApplication1</RootNamespace> <WindowsTargetPlatformVersion>10.0</WindowsTargetPlatformVersion> </PropertyGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.Default.props" /> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'" Label="Configuration"> <ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries> <PlatformToolset>v142</PlatformToolset> <CharacterSet>Unicode</CharacterSet> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'" Label="Configuration"> <ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries> <PlatformToolset>v142</PlatformToolset> <WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>Unicode</CharacterSet> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'" Label="Configuration"> <ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>true</UseDebugLibraries> <PlatformToolset>v142</PlatformToolset> <CharacterSet>MultiByte</CharacterSet> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'" Label="Configuration"> <ConfigurationType>Application</ConfigurationType> <UseDebugLibraries>false</UseDebugLibraries> <PlatformToolset>v142</PlatformToolset> <WholeProgramOptimization>true</WholeProgramOptimization> <CharacterSet>Unicode</CharacterSet> </PropertyGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <ImportGroup Label="ExtensionSettings"> </ImportGroup> <ImportGroup Label="Shared"> </ImportGroup> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> </ImportGroup> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> </ImportGroup> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> </ImportGroup> <ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" /> </ImportGroup> <PropertyGroup Label="UserMacros" /> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <LinkIncremental>true</LinkIncremental> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <LinkIncremental>false</LinkIncremental> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <LinkIncremental>true</LinkIncremental> <ReferencePath>$(VC_ReferencesPath_x64);$(ReferencePath)</ReferencePath> </PropertyGroup> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <LinkIncremental>false</LinkIncremental> </PropertyGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ClCompile> <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> <InlineFunctionExpansion>Default</InlineFunctionExpansion> <FunctionLevelLinking>false</FunctionLevelLinking> <Optimization>Disabled</Optimization> <SuppressStartupBanner>true</SuppressStartupBanner> <WarningLevel>Level3</WarningLevel> <MinimalRebuild>true</MinimalRebuild> <DebugInformationFormat>EditAndContinue</DebugInformationFormat> <AdditionalIncludeDirectories>..\jpeg;..\png;..\zlib;..\mng;..\tiff;..\j2k;..\jasper\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;JAS_WIN_MSVC_BUILD;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <AssemblerListingLocation>.\Debug\</AssemblerListingLocation> <PrecompiledHeaderOutputFile>.\Debug\cximage.pch</PrecompiledHeaderOutputFile> <ObjectFileName>.\Debug\</ObjectFileName> <ProgramDataBaseFileName>.\Debug\</ProgramDataBaseFileName> <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks> </ClCompile> <Link> <SubSystem>Console</SubSystem> <GenerateDebugInformation>true</GenerateDebugInformation> </Link> </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ClCompile> <RuntimeLibrary>MultiThreadedDLL</RuntimeLibrary> <InlineFunctionExpansion>OnlyExplicitInline</InlineFunctionExpansion> <StringPooling>true</StringPooling> <FunctionLevelLinking>true</FunctionLevelLinking> <Optimization>MaxSpeed</Optimization> <SuppressStartupBanner>true</SuppressStartupBanner> <WarningLevel>Level3</WarningLevel> <AdditionalIncludeDirectories>..\jpeg;..\png;..\zlib;..\mng;..\tiff;..\j2k;..\jasper\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;JAS_WIN_MSVC_BUILD;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <AssemblerListingLocation>.\Release\</AssemblerListingLocation> <PrecompiledHeaderOutputFile>.\Release\cximage.pch</PrecompiledHeaderOutputFile> <ObjectFileName>.\Release\</ObjectFileName> <ProgramDataBaseFileName>.\Release\</ProgramDataBaseFileName> </ClCompile> <Link> <SubSystem>Console</SubSystem> <EnableCOMDATFolding>true</EnableCOMDATFolding> <OptimizeReferences>true</OptimizeReferences> <GenerateDebugInformation>true</GenerateDebugInformation> </Link> </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ClCompile> <RuntimeLibrary>MultiThreadedDebugDLL</RuntimeLibrary> <InlineFunctionExpansion>Default</InlineFunctionExpansion> <FunctionLevelLinking>false</FunctionLevelLinking> <Optimization>Disabled</Optimization> <SuppressStartupBanner>true</SuppressStartupBanner> <WarningLevel>Level3</WarningLevel> <DebugInformationFormat>ProgramDatabase</DebugInformationFormat> <AdditionalIncludeDirectories>..\jpeg;..\png;..\zlib;..\mng;..\tiff;..\j2k;..\jasper\include;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories> <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;JAS_WIN_MSVC_BUILD;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <AssemblerListingLocation>.\Debug\</AssemblerListingLocation> <PrecompiledHeaderOutputFile>.\Debug\cximage.pch</PrecompiledHeaderOutputFile> <ObjectFileName>.\Debug\</ObjectFileName> <ProgramDataBaseFileName>.\Debug\</ProgramDataBaseFileName> <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks> </ClCompile> <Link> <SubSystem>Console</SubSystem> <GenerateDebugInformation>true</GenerateDebugInformation> <AdditionalLibraryDirectories>..\x64\Debug\;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> </Link> </ItemDefinitionGroup> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ClCompile> <WarningLevel>Level3</WarningLevel> <FunctionLevelLinking>true</FunctionLevelLinking> <IntrinsicFunctions>true</IntrinsicFunctions> <SDLCheck>true</SDLCheck> <PreprocessorDefinitions>NDEBUG;_CONSOLE;%(PreprocessorDefinitions)</PreprocessorDefinitions> <ConformanceMode>true</ConformanceMode> </ClCompile> <Link> <SubSystem>Console</SubSystem> <EnableCOMDATFolding>true</EnableCOMDATFolding> <OptimizeReferences>true</OptimizeReferences> <GenerateDebugInformation>true</GenerateDebugInformation> </Link> </ItemDefinitionGroup> <ItemGroup> <ClCompile Include="ConsoleApplication1.cpp" /> </ItemGroup> <ItemGroup> <Library Include="..\x64\Debug\cximage.lib" /> <Library Include="..\x64\Debug\jasper.lib" /> <Library Include="..\x64\Debug\jbig.lib" /> <Library Include="..\x64\Debug\Jpeg.lib" /> <Library Include="..\x64\Debug\libdcr.lib" /> <Library Include="..\x64\Debug\mng.lib" /> <Library Include="..\x64\Debug\png.lib" /> <Library Include="..\x64\Debug\Tiff.lib" /> <Library Include="..\x64\Debug\zlib.lib" /> </ItemGroup> <ItemGroup> <ClInclude Include="..\CxImage\xfile.h" /> <ClInclude Include="..\CxImage\ximabmp.h" /> <ClInclude Include="..\CxImage\ximacfg.h" /> <ClInclude Include="..\CxImage\ximadef.h" /> <ClInclude Include="..\CxImage\ximage.h" /> <ClInclude Include="..\CxImage\ximagif.h" /> <ClInclude Include="..\CxImage\ximaico.h" /> <ClInclude Include="..\CxImage\ximaiter.h" /> <ClInclude Include="..\CxImage\ximajas.h" /> <ClInclude Include="..\CxImage\ximajbg.h" /> <ClInclude Include="..\CxImage\ximajpg.h" /> <ClInclude Include="..\CxImage\ximamng.h" /> <ClInclude Include="..\CxImage\ximapcx.h" /> <ClInclude Include="..\CxImage\ximapng.h" /> <ClInclude Include="..\CxImage\ximaraw.h" /> <ClInclude Include="..\CxImage\ximaska.h" /> <ClInclude Include="..\CxImage\ximatga.h" /> <ClInclude Include="..\CxImage\ximath.h" /> <ClInclude Include="..\CxImage\ximatif.h" /> <ClInclude Include="..\CxImage\ximawbmp.h" /> <ClInclude Include="..\CxImage\ximawmf.h" /> <ClInclude Include="..\CxImage\xiofile.h" /> <ClInclude Include="..\CxImage\xmemfile.h" /> </ItemGroup> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <ImportGroup Label="ExtensionTargets"> </ImportGroup> </Project>
這個是完整的配置。(這塊搞了大半天) 執行完效果 如下圖