其他分享
首页 > 其他分享> > freeimage 动态验证码tif解析成静态jpg

freeimage 动态验证码tif解析成静态jpg

作者:互联网

这函数功能是输入一个路径名,返回解析到的图片 然后存放到一个vector里面
解决了网上代码 内存泄漏 问题。修改了代码结构,修复了大数据量调用崩溃的情况

#include<opencv.hpp>


using namespace cv;
using namespace std;


Mat Gif_To_Mat(FIBITMAP* fiBmp, const FREE_IMAGE_FORMAT fif);
void FreeImageErrorHandler(FREE_IMAGE_FORMAT fif, const char *message);
bool Gif_Load(const string &filename,  vector<Mat>& res);
bool Jpg_To_Video();
bool Show_Video();

unsigned char bpp;
BYTE *bits;
bool Load_flag;
int width;
int height;
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
Mat Src_Gif;


int _tmain(int argc, _TCHAR* argv[])
{
	//FreeImage_Initialise();
	using clock = std::chrono::system_clock;
	using ms = std::chrono::milliseconds;
	const auto before = clock::now();
	cv::String gyLabelFP = "F:/cppcode/cpptest/verifi_code/path.txt";//图片列表路径
	ifstream infile;;
	string line;
	infile.open(gyLabelFP);   //将文件流对象与文件连接起来 
	/*cout << labelFP << endl;*/


	while (getline(infile, line))
	{

		String gif_path = line;
		vector<Mat>res;
		for (int i = 0; i < 2000; i++)
		{
			Load_flag = Gif_Load(gif_path, res);

			if (Load_flag == TRUE)
			{
				//Jpg_To_Video();
				////Show_Video();
				cout << "load file successful!" << endl;

			}
			cout << i << endl;
		}
	}

	//FreeImage_DeInitialise();
	const auto duration = std::chrono::duration_cast<ms>(clock::now() - before);
	
	std::cout << duration.count() << "ms" << std::endl;
	
	return 0;
}

bool Gif_Load(const string &filename,vector<Mat>&res)
{
	FreeImage_Initialise();
	FIBITMAP *dib = 0;
	FIMULTIBITMAP *bitmap = 0;
	FIBITMAP * pFrame;

	fif = FreeImage_GetFileType(filename.c_str(), 0);
	if (fif == FIF_UNKNOWN)						
		fif = FreeImage_GetFIFFromFilename(filename.c_str());
	if (fif == FIF_UNKNOWN)						
		return false;
	if (FreeImage_FIFSupportsReading(fif))		
		dib = FreeImage_Load(fif, filename.c_str());
	if (!dib)									
		return false;//dib Load failed	

	//bpp = FreeImage_GetBPP(dib);
	//bits = (BYTE*)FreeImage_GetBits(dib);
	width = FreeImage_GetWidth(dib);
	height = FreeImage_GetHeight(dib);

	cout << "Load The File:   " << filename.c_str()  << endl;
	cout << "The File's width: " << width << endl;
	cout << "The File's height: " << height << endl;

	//if ((bits == 0) || (width == 0) || (height == 0))		return false;

	bitmap = FreeImage_OpenMultiBitmap(fif, filename.c_str(), 0, 0, 1, GIF_DEFAULT);
	if (bitmap == NULL)
	{ 
		cout << "BitMap == Null" << endl;
		return FALSE;
	}
	
	int count = FreeImage_GetPageCount(bitmap);//获取帧数;

	for (int i = 0; i <count; i++)
	{
		pFrame = FreeImage_LockPage(bitmap, i);
		//cout << "pFrame:" << pFrame << endl;
		Src_Gif = Gif_To_Mat(pFrame, fif);
		res.push_back(Src_Gif);
		string fold = "F:/cppcode/cpptest/data/";
		string Src_Gif_Name = to_string(i);
		
		imwrite(fold + Src_Gif_Name + ".jpg", Src_Gif);
		FreeImage_UnlockPage(bitmap, pFrame, 1);
	}
	BOOL  bSuccess = FreeImage_CloseMultiBitmap(bitmap, 0);
	if (!bSuccess)
	{
		return FALSE;
	}
	FreeImage_Unload(dib);
	FreeImage_DeInitialise();
	Load_flag = TRUE;
	return Load_flag;
}

Mat Gif_To_Mat(FIBITMAP* fiBmp, const FREE_IMAGE_FORMAT fif)
{
	if (fiBmp == NULL || fif != FIF_GIF)
	{
		return Mat();
	}

	//int width = FreeImage_GetWidth(fiBmp);
	//int height = FreeImage_GetHeight(fiBmp);
	
	BYTE intensity;
	BYTE* PIintensity = &intensity;
	if (FreeImage_GetBPP(fiBmp) != 8)
		fiBmp = FreeImage_ConvertTo8Bits(fiBmp);

	//RGBQUAD* pixels = new RGBQUAD;
	RGBQUAD* pixels = FreeImage_GetPalette(fiBmp);

	Mat img = Mat::zeros(height, width, CV_8UC3);

	uchar *p;

	for (int i = 0; i < height; i++)
	{
		p = img.ptr<uchar>(i);
		for (int j = 0; j < width; j++)
		{
			FreeImage_GetPixelIndex(fiBmp, j, height - i, PIintensity);//height - i
			p[3 * j] = pixels[intensity].rgbBlue;
			p[3 * j + 1] = pixels[intensity].rgbGreen;
			p[3 * j + 2] = pixels[intensity].rgbRed;
		}
	}

	return img;
}

在这里插入图片描述
在这里插入图片描述

u010654302 发布了1 篇原创文章 · 获赞 0 · 访问量 10 私信 关注

标签:Load,cout,freeimage,dib,FreeImage,int,fif,jpg,tif
来源: https://blog.csdn.net/u010654302/article/details/104051662