• C++ 文件读写。怎么得到图片的像素?



    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!
    ";
    }
    View Code

    配置很重要

      <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;..	iff;..j2k;..jasperinclude;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
          <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;JAS_WIN_MSVC_BUILD;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
          <AssemblerListingLocation>.Debug</AssemblerListingLocation>
          <PrecompiledHeaderOutputFile>.Debugcximage.pch</PrecompiledHeaderOutputFile>
          <ObjectFileName>.Debug</ObjectFileName>
          <ProgramDataBaseFileName>.Debug</ProgramDataBaseFileName>
          <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
        </ClCompile>
        <Link>
          <SubSystem>Console</SubSystem>
          <GenerateDebugInformation>true</GenerateDebugInformation>
          <AdditionalLibraryDirectories>..x64Debug;%(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;..	iff;..j2k;..jasperinclude;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
          <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;JAS_WIN_MSVC_BUILD;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
          <AssemblerListingLocation>.Debug</AssemblerListingLocation>
          <PrecompiledHeaderOutputFile>.Debugcximage.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;..	iff;..j2k;..jasperinclude;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
          <PreprocessorDefinitions>WIN32;NDEBUG;_LIB;JAS_WIN_MSVC_BUILD;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
          <AssemblerListingLocation>.Release</AssemblerListingLocation>
          <PrecompiledHeaderOutputFile>.Releasecximage.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;..	iff;..j2k;..jasperinclude;%(AdditionalIncludeDirectories)</AdditionalIncludeDirectories>
          <PreprocessorDefinitions>WIN32;_DEBUG;_LIB;JAS_WIN_MSVC_BUILD;_CRT_SECURE_NO_DEPRECATE;%(PreprocessorDefinitions)</PreprocessorDefinitions>
          <AssemblerListingLocation>.Debug</AssemblerListingLocation>
          <PrecompiledHeaderOutputFile>.Debugcximage.pch</PrecompiledHeaderOutputFile>
          <ObjectFileName>.Debug</ObjectFileName>
          <ProgramDataBaseFileName>.Debug</ProgramDataBaseFileName>
          <BasicRuntimeChecks>EnableFastChecks</BasicRuntimeChecks>
        </ClCompile>
        <Link>
          <SubSystem>Console</SubSystem>
          <GenerateDebugInformation>true</GenerateDebugInformation>
          <AdditionalLibraryDirectories>..x64Debug;%(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="..x64Debugcximage.lib" />
        <Library Include="..x64Debugjasper.lib" />
        <Library Include="..x64Debugjbig.lib" />
        <Library Include="..x64DebugJpeg.lib" />
        <Library Include="..x64Debuglibdcr.lib" />
        <Library Include="..x64Debugmng.lib" />
        <Library Include="..x64Debugpng.lib" />
        <Library Include="..x64DebugTiff.lib" />
        <Library Include="..x64Debugzlib.lib" />
      </ItemGroup>
      <ItemGroup>
        <ClInclude Include="..CxImagexfile.h" />
        <ClInclude Include="..CxImageximabmp.h" />
        <ClInclude Include="..CxImageximacfg.h" />
        <ClInclude Include="..CxImageximadef.h" />
        <ClInclude Include="..CxImageximage.h" />
        <ClInclude Include="..CxImageximagif.h" />
        <ClInclude Include="..CxImageximaico.h" />
        <ClInclude Include="..CxImageximaiter.h" />
        <ClInclude Include="..CxImageximajas.h" />
        <ClInclude Include="..CxImageximajbg.h" />
        <ClInclude Include="..CxImageximajpg.h" />
        <ClInclude Include="..CxImageximamng.h" />
        <ClInclude Include="..CxImageximapcx.h" />
        <ClInclude Include="..CxImageximapng.h" />
        <ClInclude Include="..CxImageximaraw.h" />
        <ClInclude Include="..CxImageximaska.h" />
        <ClInclude Include="..CxImageximatga.h" />
        <ClInclude Include="..CxImageximath.h" />
        <ClInclude Include="..CxImageximatif.h" />
        <ClInclude Include="..CxImageximawbmp.h" />
        <ClInclude Include="..CxImageximawmf.h" />
        <ClInclude Include="..CxImagexiofile.h" />
        <ClInclude Include="..CxImagexmemfile.h" />
      </ItemGroup>
      <Import Project="$(VCTargetsPath)Microsoft.Cpp.targets" />
      <ImportGroup Label="ExtensionTargets">
      </ImportGroup>
    </Project>
    View Code

    这个是完整的配置。(这块搞了大半天) 执行完效果 如下图  

  • 相关阅读:
    第十一周作业
    第十周作业
    第九周作业
    第八周作业
    第九周
    第八周
    第七周
    第六周
    第五周
    实验报告二
  • 原文地址:https://www.cnblogs.com/mrguoguo/p/13595229.html
Copyright © 2020-2023  润新知