WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

 
 
Thread Tools Rating: Thread Rating: 13 votes, 5.00 average. Display Modes
Prev Previous Post   Next Post Next
  #3  
Old 09-06-2012, 11:20 AM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Here is a sample script that will capture the screen to a texture over a specified time interval and send a downsized version of the image to all the connected websocket clients. The image is downsized and compressed to PNG in a separate thread, so it should not slow down the main graphics thread too much, depending on the size of your window. If you need to reduce the network traffic, you could try compressing to JPG instead.
Code:
import viz
import vizhtml
import viztask
import base64
import Image
import StringIO
viz.go()

viz.add('maze.osgb')

code = """
<html>
<head>
	<title>vizhtml Screen Share Example</title>
	<script type="text/javascript" src="vizhtml.js"></script>
</head>
<body>
<script language="javascript">

var socket = new VizardWebSocket();

socket.onevent('open', function(e){
	document.getElementById('status').innerHTML = 'connected';
})

socket.onevent('close', function(e){
	document.getElementById('status').innerHTML = 'waiting for connection';
	socket.reconnect();
})

socket.onevent('set_image', function(e){
	document.getElementById('screen').src = 'data:image/png;base64,' + e.data.data;
})

</script>

<div>Status:</div><div id='status'>waiting for connection</div></br>
<div>Screen:</div>
<img id='screen'>
</body>
</html>
"""

# http://localhost:8080/vizhtml/screen_share
vizhtml.registerCode('screen_share',code)

# Size of image to send
IMAGE_SIZE = [160,120]

# Time interval (in seconds) to send screen capture
CAPTURE_INTERVAL = 0.25

def ScreenCaptureTask():

	# Blank texture that will hold screen capture
	tex = viz.addBlankTexture([1,1])

	def SendScreenImage(texture):

		# Compute downsample size while preserving original aspect ratio
		width, height = texture.getSize()[:2]
		ratio = min( IMAGE_SIZE[0] / float(width), IMAGE_SIZE[1] / float(height) )
		if ratio < 1.0:
			size = [ int(round(width * ratio)), int(round(height * ratio)) ]
		else:
			size = [ width, height ]

		# Convert texture to PIL image
		im = Image.frombuffer('RGB',[width,height],texture.getImageData(),'raw','RGB',0,1)

		# Resize image and flip
		im = im.resize(size).transpose(Image.FLIP_TOP_BOTTOM)

		# Save image to base 64 encoded PNG buffer
		output = StringIO.StringIO()
		im.save(output,'PNG')
		buf = base64.b64encode(output.getvalue())
		output.close()

		# Send buffer to all connected clients
		vizhtml.sendAll('set_image',viz.Data(data=buf))

	while True:

		# Wait for capture interval
		yield viztask.waitTime(CAPTURE_INTERVAL)

		# Capture screen to texture
		viz.window.screenCapture(tex)
		yield None

		# Send texture to clients asynchronously
		yield viztask.waitDirector(SendScreenImage,tex)

viztask.schedule( ScreenCaptureTask() )
Reply With Quote
 


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Use saved text file data as replay sources problem mizutani_jun Vizard 4 10-14-2010 04:49 PM
Using 5DT plug-in with the data coming through network... mcicek Vizard 1 05-18-2009 05:17 PM
two continuous sockets timbo Vizard 6 07-09-2008 10:16 AM
tracking using quaternarion data jfreeman Vizard 2 06-01-2005 08:48 AM


All times are GMT -7. The time now is 02:17 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC