基本信息
源码名称:c++给图片增加水印
源码大小:5.49KB
文件格式:.zip
开发语言:C/C++
更新时间:2020-07-28
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍

#include <iostream>
#include <future>
#include <functional>
#include <Magick  .h>

#include "threadpool.hpp"

//using namespace std;

std::string append_filename(const std::string& filename, const std::string& text);
void do_watermark(const std::string& filename, const Magick::Image& right_image, const Magick::Image& left_image,const std::string& text);


int main(int argc, char** argv){
	Magick::InitializeMagick(NULL);
	Magick::Image right_logo,left_logo;
	std::string filename;

	ThreadPool pool;

	const std::string text("-wm");

	if(argc < 3){
		std::cout << "Insufficient arguments" << std::endl;
		std::cout << "Usage:" << std::endl;
		std::cout << "bulkimagewatermark <left_logo> <right_logo>" << std::endl;
		return 1;
	}

	try{
		left_logo.read(argv[1]);
		right_logo.read(argv[2]);
	}catch(std::exception &err_){
		std::cout << "Error reading the images :" << err_.what() << std::endl;  
		return 1;
	}

	std::packaged_task<void()>* task;

	while(getline(std::cin,filename)){
		if(filename.length() == 0) break;
		task = new std::packaged_task<void()>(std::bind(do_watermark,filename,right_logo,left_logo,text));
		pool.addTask(*task);
		delete task;
	}

	pool.wait();

	return 0;
}


std::string append_filename(const std::string& filename, const std::string& text){
	std::size_t pos = filename.find_last_of('.');
	return filename.substr(0,pos)   text   filename.substr(pos);
}

void do_watermark(const std::string& filename, const Magick::Image& right_image, const Magick::Image& left_image,const std::string& text){
    Magick::Image image;
    std::string output_fname = append_filename(filename,text);
	std::cout << "Processing " << filename << " ............." << std::endl;
	try{
		image.read(filename);
		image.composite(right_image,Magick::GravityType::NorthEastGravity,Magick::CompositeOperator::OverCompositeOp);
		image.composite(left_image,Magick::GravityType::NorthWestGravity,Magick::CompositeOperator::OverCompositeOp);
		image.write(output_fname);		      
	}catch(std::exception &err_){
		std::cout << "Error processing " << filename << " : " << err_.what() << std::endl;
	}
	std::cout << "Done Processing " << output_fname << "." << std::endl;
}