使用Qt合并图片的算法

1 水平方向合并图片

QImage MergeImageH(vector <QImage > image)
{
	int image_width=0;
	int max_height = 0;
	vector <QImage > ::iterator it;
	for(it = image.begin();it!=image.end();++it)
	{
		int width = (*it).width();
		image_width += width;
		if((*it).height()>max_height)
		{
			max_height =(*it).height();
		}
	}
	QImage result_image_h(image_width,max_height,QImage::Format_RGB32);
	result_image_h.fill(Qt::white);
	QPainter painter_h;
	painter_h.begin(&result_image_h);
	int x_number=0;
	for(it = image.begin();it!=image.end();++it)
	{
		painter_h.drawImage(x_number,0,(*it));
		x_number += (*it).width();
	}
	painter_h.end();
	QString name = image_output_path_.append("/").append(merge_output_filename_).append(".").append(convert_format_);
	result_image_h.save(name,convert_format_.toAscii().data());
	return result_image_h;
}

2 竖直方向合并图片

QImage MergeImageV(vector <QImage > image)
	int max_width = 0;
	int image_height=0;
	vector <QImage > ::iterator it;
	for(it = image.begin();it!=image.end();++it)
	{
		int height = (*it).height();
		image_height += height;
		if((*it).width() > max_width)
		{
			max_width =(*it).width();
		}
	}

	QImage result_image_v(max_width,image_height,QImage::Format_RGB32);
	result_image_v.fill(Qt::white);
	QPainter painter_v;
	painter_v.begin(&result_image_v);
	int y_number=0;
	for(it = image.begin();it!=image.end();++it)
	{
		painter_v.drawImage(0,y_number,(*it));
		y_number += (*it).height();
	}
	painter_v.end();	
	QString name = image_output_path_.append("/").append(merge_output_filename_).append(".").append(convert_format_);
	emit startConvertMove(1,1);
	result_image_v.save(name,convert_format_.toAscii().data());
	return result_image_v;
}













你可能感兴趣的:(image,qt,merge,QImage)