Skip to content

Instantly share code, notes, and snippets.

@yurydelendik
Created July 17, 2013 18:49
Show Gist options
  • Save yurydelendik/6023320 to your computer and use it in GitHub Desktop.
Save yurydelendik/6023320 to your computer and use it in GitHub Desktop.
poor man droid screencast
<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>
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();
@yurydelendik
Copy link
Author

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!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment