Created
July 17, 2013 18:49
-
-
Save yurydelendik/6023320 to your computer and use it in GitHub Desktop.
poor man droid screencast
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<canvas id="screencap" onclick="turn()"></canvas> | |
<script> | |
var angle = 0; | |
function turn() { | |
angle = (angle + 90) % 360; | |
} | |
function readImage() { | |
var img = new Image(); | |
img.onload = function () { | |
var size = Math.max(img.width, img.height); | |
var canvas = document.getElementById('screencap'); | |
canvas.width = size; | |
canvas.height = size; | |
var ctx = canvas.getContext('2d'); | |
ctx.save(); | |
ctx.translate(size / 2, size / 2); | |
ctx.rotate(angle / 180 * Math.PI); | |
ctx.translate(-img.width / 2, -img.height / 2); | |
ctx.drawImage(img, 0, 0); | |
ctx.restore(); | |
setTimeout(readImage, 50); | |
}; | |
img.onerror = function () { | |
setTimeout(readImage, 1000); | |
}; | |
img.src = "http://localhost:8087/?r=" + Date.now() | |
} | |
readImage(); | |
</script> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
http = require('http'); | |
exec = require('child_process').exec; | |
var isWindows = /^win/.test(require('os').platform()); | |
var httpPort = 8087; | |
var bindAddress = '127.0.0.1'; | |
var adbPath = isWindows ? 'adb.exe' : 'adb'; | |
var currentImage = ''; | |
http.createServer(function(req, res){ | |
res.writeHead(200, {'Content-Type': 'image/png' }); | |
res.end(currentImage, 'binary'); | |
}).listen(httpPort, bindAddress); | |
function readScreen() { | |
// getting image from the device per | |
// http://blog.shvetsov.com/2013/02/grab-android-screenshot-to-computer-via.html | |
exec(adbPath + ' shell screencap -p', {encoding: 'binary', maxBuffer : 4 * 1024 * 1024}, | |
function (error, output) { | |
if (error) { | |
console.log('error: ' + error); | |
setTimeout(readScreen, 5000); // repeat in 5 sec | |
return; | |
} | |
currentImage = output.replace(/\r\n/g, '\n'); // removing adb CR | |
if (isWindows) { | |
currentImage = currentImage.replace(/\r\n/g, '\n'); // removing windows CR | |
} | |
readScreen(); | |
}); | |
} | |
readScreen(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You will need to install adb. These utilities can be found in the Android Developer Toolkit. (Or I found them at http://downloads.geeksphone.com/)
Run the HTML page in a browser(s), run
node droidscreen.js
and connect your device using USB port. Enjoy the show!