问题
this is my first post so please let me know if i dont relay the question clearly.
i am trying to open a (black and white) jpeg or png image file as an array in dev c++ so that i can isolate the place of the white pixels. the pointers, and loops for the isolation i understand. however, opening the image file has not worked and have been unable to find an example that i can use. besides the given #include "cstdlib" #include "iostream" #include "stdio.h"
i have no other header files. i know fopen for file open, however none of my attempts have displayed the data of the image.
thank you for any help
THIS IS NOT MY CODE. THIS IS BIV1991 EXAMPLE PROVIDED TO ME i am posting because the header for gdiplus.h is not being located. the header unknwn, windows,objidl were suggested to solve that problem
#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;
}
回答1:
To start with, there is no standard C++ library to process images. Secondly, jpeg and png image formats are compressed image formats that need some complex sort of decompressing algorithm. So to start work with images you have two ways:
Using of simple image format like bmp. bmp is just pixel buffer with some light header, describing that pixel buffer format. You can write a program loading bmp in half an our on your own (and waste about an hour to understand the format itself). More on bmp: http://en.wikipedia.org/wiki/BMP_file_format.
Using EXTERNAL library. That is, you don't have this library with your language or compiler package. You need to download and even install it sometimes. In general, to use external library you need to do five thighs: 1) Find image processing library in Internet. 2) Put xxx.lib file in your project folder. 3) Tell your linker you want to use xxx.lib 4) Add in your code #include "xxx.h". 5) Use library API to load image.
回答2:
If You programming for Windows, You may use GDIPlus for reading of Jpeg and another images types and operates with them, for example drawing or getting of they pixels. Guarantee 100%. I was do it many times.
Whats to include:
#include <gdiplus.h>
#pragma comment(lib,"gdiplus.lib")
What to do in code:
// 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
来源:https://stackoverflow.com/questions/27477441/opening-a-jpeg-or-png-image-as-pixel-data-in-c-or-c