画图板用c++实现和用js实现的websocket版本

阅读更多
画图板
opencv的c++
#include   
using namespace std;
#pragma comment(linker, "/subsystem:\"windows\" /entry:\"mainCRTStartup\"")  
const char *pstrWindowsMouseDrawTitle = "鼠标绘图(http://blog.csdn.net/MoreWindows)";
// 鼠标消息的回调函数  
void on_mouse(int event, int x, int y, int flags, void* param)
{
	static bool s_bMouseLButtonDown = false;
	static CvPoint s_cvPrePoint = cvPoint(0, 0);

	switch (event)
	{
	case CV_EVENT_LBUTTONDOWN:
		s_bMouseLButtonDown = true;
		s_cvPrePoint = cvPoint(x, y);
		break;

	case  CV_EVENT_LBUTTONUP:
		s_bMouseLButtonDown = false;
		break;

	case CV_EVENT_MOUSEMOVE:
		if (s_bMouseLButtonDown)
		{
			CvPoint cvCurrPoint = cvPoint(x, y);
			cvLine((IplImage*)param, s_cvPrePoint, cvCurrPoint, CV_RGB(0, 0, 20), 3);
			s_cvPrePoint = cvCurrPoint;
			cvShowImage(pstrWindowsMouseDrawTitle, (IplImage*)param);
		}
		break;
	}
}
int main()
{
	const int MAX_WIDTH = 500, MAX_HEIGHT = 400;
	const char *pstrSaveImageName = "MouseDraw.jpg";

	IplImage *pSrcImage = cvCreateImage(cvSize(MAX_WIDTH, MAX_HEIGHT), IPL_DEPTH_8U, 3);
	cvSet(pSrcImage, CV_RGB(255, 255, 255)); //可以用cvSet()将图像填充成白色  
	cvNamedWindow(pstrWindowsMouseDrawTitle, CV_WINDOW_AUTOSIZE);
	cvShowImage(pstrWindowsMouseDrawTitle, pSrcImage);

	cvSetMouseCallback(pstrWindowsMouseDrawTitle, on_mouse, (void*)pSrcImage);

	int c;
	do{
		c = cvWaitKey(0);
		switch ((char)c)
		{
		case 'r':
			cvSet(pSrcImage, CV_RGB(000, 000, 255));
			cvShowImage(pstrWindowsMouseDrawTitle, pSrcImage);
			break;

		case 's':
			cvSaveImage(pstrSaveImageName, pSrcImage);
			break;
		}
	} while (c > 0 && c != 27);

	cvDestroyWindow(pstrWindowsMouseDrawTitle);
	cvReleaseImage(&pSrcImage);
	return 0;
}

参考
http://blog.csdn.net/morewindows/article/details/8426283

html版本,+tomcat7的websocket




jiben



	
 




 






LineWebSocketServlet.java
package com.hao;

import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.catalina.websocket.MessageInbound;
import org.apache.catalina.websocket.StreamInbound;
import org.apache.catalina.websocket.WebSocketServlet;
import org.apache.catalina.websocket.WsOutbound;

public class LineWebSocketServlet extends WebSocketServlet {
	private static Map mmiList  = new HashMap();

	protected StreamInbound createWebSocketInbound(String subProtocol,
			HttpServletRequest arg1) {
		return new MyMessageInbound();
	}

	private class MyMessageInbound extends MessageInbound {
		WsOutbound myoutbound;
		String mykey;
		@Override
		public void onOpen(WsOutbound outbound) {
			try {
				System.out.println("Open Client.");
				this.myoutbound = outbound;
				mykey ="open "+System.currentTimeMillis();;
				mmiList.put(mykey, this);
				System.out.println("mmiList size:"+mmiList.size());
				outbound.writeTextMessage(CharBuffer.wrap(mykey));
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void onClose(int status) {
			System.out.println("Close Client.");
			mmiList.remove(mykey);
		}

		@Override
		protected void onBinaryMessage(ByteBuffer arg0) throws IOException {

		}

		@Override
		protected void onTextMessage(CharBuffer message) throws IOException {
			System.out.println("LineWebSocketServlet.onTextMessage--->" + message.toString());
			for (Map.Entry entry : mmiList.entrySet()) {
				  MyMessageInbound mmib = (MyMessageInbound) entry.getValue();
	              CharBuffer buffer = CharBuffer.wrap(message);
	              mmib.myoutbound.writeTextMessage(buffer);
	              mmib.myoutbound.flush();
			}
			
		}
	}
}


web.xml
  
      wsLine
      com.hao.LineWebSocketServlet
    
    
      wsLine
      /websocket/line
    
   

你可能感兴趣的:(c,js)