基本上這個東西沒有很紅,
我個人覺得有幾個原因:
一.架設與維護人員訓練耗時
要找精通 c++ & javascript & css & (ajax | html5 | flash)的人
你是要殺人喔?

二.網站製作時間相對較長、成本較高
語言本身的難度、編譯的時間、溝通的時間、debug的時間

三.要錢而且很貴
599美金,跟一個microchip編譯器一樣貴。

四.沒有人幫他寫framework跟plugin
不像 wordpress 一樣,掛一掛就解一半的問題~

主程式:

#include <Wt/WApplication>
#include <Wt/WBreak>
#include <Wt/WContainerWidget>
#include <Wt/WLineEdit>
#include <Wt/WPushButton>
#include <Wt/WText>

#include <Wt/WImage>
#include <Wt/WFileUpload>
#include <Wt/WProgressBar>
#include <Wt/WResource>
#include <Wt/WFileResource>
#include <Wt/WLink>

#include <Wt/WEnvironment>
#include <Wt/WBootstrapTheme>
#include <Wt/WCssTheme>

#include <opencv2/opencv.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <auto_link_opencv.hpp>

#include <auto_link_wt.hpp>

// c++0x only, for std::bind
#include <functional>

using namespace Wt;

/*
 * A simple hello world application class which demonstrates how to react
 * to events, read input, and give feed-back.
 */
class HelloApplication : public WApplication
{
public:
	HelloApplication(const WEnvironment& env);

private:
	WLineEdit* nameEdit_;
	WText* greeting_;

};

HelloApplication::HelloApplication(const WEnvironment& env)
	: WApplication(env)
{
	setTitle("Hello world");                               // application title
	root()->addWidget(new WText("Your name, please ? "));  // show some text
	nameEdit_ = new WLineEdit(root());                     // allow text input
	nameEdit_->setFocus();                                 // give focus
	WPushButton* button
		= new WPushButton("Greet me.", root());              // create a button
	button->setMargin(5, Left);                            // add 5 pixels margin
	root()->addWidget(new WBreak());                       // insert a line break
	greeting_ = new WText(root());                         // empty text
	/*
	 * Connect signals with slots
	 *
	 * - simple Wt-way
	 */
	/*
	 * - using a c++0x lambda:
	 */
	nameEdit_->enterPressed().connect(std::bind([ = ]()
	{
		greeting_->setText("Hello there, " + nameEdit_->text());
	}));	
	button->clicked().connect(std::bind([ = ]()
	{
		greeting_->setText("Hello there, " + nameEdit_->text());
	}));
	// Upload Container
	Wt::WContainerWidget* container = new Wt::WContainerWidget();
	container->setStyleClass("yellow-box");
	Wt::WFileUpload* fu = new Wt::WFileUpload(container);
	fu->setFileTextSize(50 * 1024); // Set the maximum file size to 50 mB.
	Wt::WProgressBar* wbar = new Wt::WProgressBar(container);
	wbar->setRange(0, 100);
	fu->setProgressBar(wbar);
	fu->setMargin(10, Wt::Right);
	// Provide a button to start uploading.
	Wt::WPushButton* uploadButton = new Wt::WPushButton("Send", container);
	uploadButton->setMargin(10, Wt::Left | Wt::Right);
	Wt::WText* out = new Wt::WText(container);
	// Upload when the button is clicked.
	uploadButton->clicked().connect(std::bind([ = ]()
	{
		fu->upload();
		uploadButton->disable();
	}));
	// Upload automatically when the user entered a file.
	fu->changed().connect(std::bind([ = ]()
	{
		fu->upload();
		uploadButton->disable();
		out->setText("File upload is changed.");
	}));
	// React to a succesfull upload.
	fu->uploaded().connect(std::bind([ = ]()
	{
		out->setText("File upload is finished.");
		std::string spool = fu->spoolFileName();
		cv::Mat img = cv::imread(spool);
		cvtColor(img, img, CV_BGR2GRAY);
		std::string savename;
		{ // create save name
			static int count = 0;
			count++;
			if (count > 100)
			{
				count = 0;
			}
			char buffer[32];
			itoa(count, buffer, 10);
			savename = "tmp" + std::string(buffer) + ".png";
		}
		cv::imwrite(savename, img);
		Wt::WFileResource* imageFile = new Wt::WFileResource("image/png", savename);
		imageFile->suggestFileName(savename);
		Wt::WImage* image = new Wt::WImage(imageFile, "PNG version");
		Wt::WFileResource* imageFileOri = new Wt::WFileResource("image/png", spool);
		imageFileOri->suggestFileName(savename);
		Wt::WImage* imageOri = new Wt::WImage(imageFileOri, "PNG version");
		root()->addWidget(image);
		root()->addWidget(imageOri);
	}));
	// React to a file upload problem.
	fu->fileTooLarge().connect(std::bind([ = ]()
	{
		out->setText("File is too large.");
	}));
	root()->addWidget(container);
}
WApplication* createApplication(const WEnvironment& env)
{
	/*
	 * You could read information from the environment to decide whether
	 * the user has permission to start a new application
	 */
	HelloApplication* app = new HelloApplication(env);
	const std::string* theme = env.getParameter("theme");
	if (theme)
	{
		app->setTheme(new Wt::WCssTheme(*theme));
	}
	else
	{
		app->setTheme(new Wt::WBootstrapTheme(app));
	}
	app->useStyleSheet("style/everywidget.css");
	app->useStyleSheet("style/dragdrop.css");
	app->useStyleSheet("style/combostyle.css");
	app->useStyleSheet("style/pygments.css");
	return app;
}
int main(int argc, char** argv)
{
	// --http-address=140.118.175.35 --http-port=8880 --deploy-path=/hello --docroot=docroot  --approot=approot
	return WRun(argc, argv, &createApplication);
}

大概說一下,
WRun 會從 cmd 參數初始化整個程式,
而每次有人要看網頁時,
Wt 都會呼叫 createApplication 來產生一個 WApplication 實體跟一個 thread,

setTitle("Hello world");                               // application title
	root()->addWidget(new WText("Your name, please ? "));  // show some text
	nameEdit_ = new WLineEdit(root());                     // allow text input
	nameEdit_->setFocus();                                 // give focus
	WPushButton* button
		= new WPushButton("Greet me.", root());              // create a button
	button->setMargin(5, Left);                            // add 5 pixels margin
	root()->addWidget(new WBreak());                       // insert a line break
	greeting_ = new WText(root());                         // empty text

初始化一堆按鍵。

nameEdit_->enterPressed().connect(std::bind([ = ]()
	{
		greeting_->setText("Hello there, " + nameEdit_->text());
	}));	
	button->clicked().connect(std::bind([ = ]()
	{
		greeting_->setText("Hello there, " + nameEdit_->text());
	}));

綁函數。

重點載完圖片後轉灰階並顯示:

// React to a succesfull upload.
	fu->uploaded().connect(std::bind([ = ]()
	{
		out->setText("File upload is finished.");
		std::string spool = fu->spoolFileName();
		cv::Mat img = cv::imread(spool);
		cvtColor(img, img, CV_BGR2GRAY);
		std::string savename;
		{ // create save name
			static int count = 0;
			count++;
			if (count > 100)
			{
				count = 0;
			}
			char buffer[32];
			itoa(count, buffer, 10);
			savename = "tmp" + std::string(buffer) + ".png";
		}
		cv::imwrite(savename, img);
		Wt::WFileResource* imageFile = new Wt::WFileResource("image/png", savename);
		imageFile->suggestFileName(savename);
		Wt::WImage* image = new Wt::WImage(imageFile, "PNG version");
		Wt::WFileResource* imageFileOri = new Wt::WFileResource("image/png", spool);
		imageFileOri->suggestFileName(savename);
		Wt::WImage* imageOri = new Wt::WImage(imageFileOri, "PNG version");
		root()->addWidget(image);
		root()->addWidget(imageOri);
	}));

結果圖:

程式碼載點: http://damody.googlecode.com/files/image_procesing_wt.7z
需要的 library prebuilt:
http://library-prebuilt-for-windows.googlecode.com/files/wt-3.3.0-vc11up3-x64.7z
http://library-prebuilt-for-windows.googlecode.com/files/zlib-1.2.7-mingw-x64.7z


arrow
arrow
    全站熱搜

    讓地獄深紅的天亮 發表在 痞客邦 留言(0) 人氣()