Skip to content

Instantly share code, notes, and snippets.

@arvydas
Created July 8, 2013 20:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save arvydas/5952285 to your computer and use it in GitHub Desktop.
Save arvydas/5952285 to your computer and use it in GitHub Desktop.
Single pixel scanner with BlinkStick and webcam using Processing language
import blinkstick.*;
import processing.video.*;
Capture cam;
int width = 640;
int height = 480;
int rectSize = 20;
BlinkStick device;
void setup() {
size(width, height);
String[] cameras = Capture.list();
if (cameras.length == 0) {
println("There are no cameras available for capture.");
exit();
} else {
println("Available cameras:");
for (int i = 0; i < cameras.length; i++) {
println(cameras[i]);
}
// The camera can be initialized directly using an
// element from the array returned by list():
cam = new Capture(this, cameras[0]);
cam.start();
}
device = BlinkStick.findFirst();
if (device == null)
{
println("There are no BlinkSticks connected.");
}
}
void draw() {
if (cam.available() == true) {
cam.read();
image(cam, 0, 0);
cam.loadPixels();
fill(255, 255, 255, 0);
double r = 0, g = 0, b = 0;
int rectX = (width - rectSize) / 2;
int rectY = (height - rectSize) / 2;
rect(rectX, rectY, rectSize, rectSize);
for (int x = rectX; x < rectX + rectSize; x++)
{
for (int y = rectY; y < rectY + rectSize; y++)
{
int p1 = cam.pixels[y * width + x];
r += (p1 >>> 16) & 0xff;
g += (p1 >>> 8) & 0xff;
b += p1 & 0xff;
}
}
int rr = (int)(r / rectSize / rectSize);
int gg = (int)(g / rectSize / rectSize);
int bb = (int)(b / rectSize / rectSize);
if (device != null)
{
device.setColor(rr, gg, bb);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment