在 C 或 C++ 中将 JPEG 或 PNG 图像作为像素数据打开

opening a jpeg or png image as pixel data in c or c++

本文关键字:像素 像素数 数据 图像 C++ 中将 JPEG PNG      更新时间:2023-10-16

这是我的第一篇文章,所以如果我没有清楚地转述这个问题,请告诉我。

我正在尝试在 Dev C++ 中将(黑白(JPEG 或 PNG 图像文件作为数组打开,以便我可以隔离白色像素的位置。 我理解的隔离的指针和循环。 但是,打开图像文件不起作用,并且无法找到我可以使用的示例。 除了给定的 #include "cstdlib" #include "iostream" #include "stdio.h">

我没有其他头文件。我知道 FOPEN 用于文件打开,但是我的尝试都没有显示图像的数据。

感谢您的任何帮助

这不是我的代码。这是提供给BIV1991示例我正在发布是因为找不到 GDIoPlus.h 的标题。建议使用标题unknwn,Windows,objidl来解决这个问题

#include <cstdlib>
#include <iostream>
#include <Unknwn.h>
#include <windows.h>
#include <objidl.h>
#include <gdiplus.h>
using namespace Gdiplus;
#pragma comment(lib,"gdiplus.lib") 
int main(int argc, char *argv[])
{
   // initialize gdiplus
Gdiplus::GdiplusStartupInput si;
ULONG_PTR token;
Gdiplus::GdiplusStartup(&token,&si,0);
// load image as bitmap, any formats supported
Gdiplus::Bitmap image(L"C:\image.jpg",false);
int w = image.GetWidth();
// image.LockBits - to read pixels 
    system("PAUSE");
    return EXIT_SUCCESS;
}

如果您为 Windows 编程,您可以使用 GDIPlus 读取 Jpeg 和其他图像类型并对其进行操作,例如绘制或获取它们的像素。保证100%。我做过很多次。

包括的内容:

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

代码中执行的操作:

// initialize gdiplus
Gdiplus::GdiplusStartupInput si;
ULONG_PTR token;
Gdiplus::GdiplusStartup(&token,&si,0);
// load image, any formats supported
Gdiplus::Bitmap image(L"C:\image.jpg",false);
int w = image.GetWidth();
// image.LockBits - to read pixels

首先,没有标准的C++库来处理图像。其次,jpeg 和 png 图像格式是压缩图像格式,需要某种复杂的解压缩算法。因此,要开始使用图像,您有两种方法:

  1. 使用简单的图像格式,如bmp.bmp只是像素缓冲区带有一些光标题,描述该像素缓冲区格式。您可以编写一个程序,将BMP加载到我们自己的一半(并浪费(大约一个小时来理解格式本身(。更多关于 bmp 的信息:http://en.wikipedia.org/wiki/BMP_file_format。

  2. 使用外部 图书馆。也就是说,您没有此库与您的语言或 编译器包。您需要下载甚至安装它 有时。一般来说,要使用外部库,你需要做五个 大腿:1(在互联网上查找图像处理库。2(将xxx.lib文件放在您的项目文件夹中。3(告诉你的链接器你想使用xxx.lib 4(添加你的代码 #include"xxx.h"。5( 使用库 API 加载图像。