Skip to content

Instantly share code, notes, and snippets.

@adenine
Created December 11, 2025 10:05
Show Gist options
  • Select an option

  • Save adenine/02a71f73189cb7320771322c84b368a0 to your computer and use it in GitHub Desktop.

Select an option

Save adenine/02a71f73189cb7320771322c84b368a0 to your computer and use it in GitHub Desktop.
Framingham
import processing.video.*;
// --- Global Variables ---
Capture video;
PImage[] frameGrid; // Array to hold the still images
int gridColumns;
int gridRows;
int totalFrames;
int frameCounter = 0; // Tracks how many frames have been stored
// The original video block dimensions
final int VID_W = 32;
final int VID_H = 24;
void setup() {
// Use a stable size and omit P3D for better compatibility
size(640, 480);
// 1. Initialize Capture: Robustly check for and start the camera
String[] cameras = Capture.list();
if (cameras.length == 0) {
println("FATAL: No cameras available. Cannot run sketch.");
exit();
}
try {
// We request the fixed 32x24 resolution
video = new Capture(this, VID_W, VID_H);
video.start();
} catch (Exception e) {
println("Failed to start Capture device: " + e.getMessage());
exit();
}
// --- Grid Calculation ---
gridColumns = width / VID_W;
gridRows = height / VID_H;
totalFrames = gridColumns * gridRows;
// 2. Initialize the array to store the PImage objects
frameGrid = new PImage[totalFrames];
println("Grid Size: " + gridColumns + " columns x " + gridRows + " rows. Total frames to capture: " + totalFrames);
background(0);
frameRate(30);
}
// --- Video Event Handler (Preferred over checking video.available in draw) ---
void captureEvent(Capture c) {
if (c == video) {
c.read();
// Only capture and store the frame if the grid is NOT full
if (frameCounter < totalFrames) {
// 3. Store the frame:
// CRITICAL: We use video.get() to create a *copy* of the frame.
// If you just assigned `frameGrid[frameCounter] = video;`,
// all array elements would point to the *same* continuously updated object.
frameGrid[frameCounter] = video.get();
frameCounter++;
// Stop the camera once the grid is full
if (frameCounter == totalFrames) {
video.stop();
println("Grid is full. Capture stopped.");
}
}
}
}
void draw() {
// Draw the background only if we want the grid to appear one frame at a time
// If you want a static grid that slowly fills, leave this line out.
// background(0);
// 4. Iterate through the array and draw all stored frames
for (int i = 0; i < frameCounter; i++) {
PImage img = frameGrid[i];
if (img != null) {
// Calculate the X and Y position for the current grid cell (i)
int x = i % gridColumns;
int y = i / gridColumns;
// Draw the stored image at the calculated grid coordinates
// We draw it at its original size (VID_W x VID_H)
image(img, x * VID_W, y * VID_H);
}
}
// Optional: Display status text
fill(255);
textSize(16);
textAlign(LEFT, TOP);
if (frameCounter < totalFrames) {
text("Capturing frame " + (frameCounter + 1) + " of " + totalFrames, 10, 10);
} else {
text("Capture Complete!", 10, 10);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment