|
7 | 7 | # on brightness. |
8 | 8 | class Mirror2 < Processing::App |
9 | 9 | load_library :video |
10 | | - include_package 'processing.video' |
11 | | - |
| 10 | + java_import 'processing.video.Capture' |
| 11 | + attr_reader :video, :cols, :rows |
| 12 | + # Size of each cell in the grid |
12 | 13 | CELL_SIZE = 15 |
13 | 14 |
|
14 | | - attr_reader :video |
15 | | - |
16 | 15 | def setup |
17 | | - sketch_title 'Mirror2' |
18 | | - # Set up columns and rows |
19 | | - color_mode(RGB, 255, 255, 255, 100) |
20 | | - rect_mode(CENTER) |
21 | | - # This the default video input, see the GettingStartedCapture |
22 | | - # example if it creates an error |
| 16 | + sketch_title 'mirror' |
| 17 | + frame_rate(30) |
| 18 | + @cols = width / CELL_SIZE |
| 19 | + @rows = height / CELL_SIZE |
| 20 | + colorMode(RGB, 255, 255, 255, 100) |
| 21 | + # Try test_capture to find name of your Camera |
23 | 22 | @video = Capture.new(self, width, height, "UVC Camera (046d:0825)") |
24 | 23 | # Start capturing the images from the camera |
25 | 24 | video.start |
26 | | - background(0) |
27 | 25 | end |
28 | 26 |
|
29 | 27 | def draw |
30 | | - return unless video.available |
| 28 | + return unless video.available # ruby guard clause |
31 | 29 |
|
| 30 | + background(0, 0, 255) |
32 | 31 | video.read |
33 | 32 | video.load_pixels |
34 | | - background(0, 0, 255) |
35 | | - # Begin loop for columns |
36 | | - grid(width, height, CELL_SIZE, CELL_SIZE) do |x, y| |
37 | | - loc = (width - x - 1) + y * width |
38 | | - # Each rect is colored white with a size determined by brightness |
39 | | - c = video.pixels[loc] |
40 | | - sz = (brightness(c) / 255.0) * CELL_SIZE |
| 33 | + grid(cols, rows) do |i, j| |
| 34 | + x = i * CELL_SIZE |
| 35 | + y = j * CELL_SIZE |
| 36 | + loc = (width - x -1 + y * width)# Reversing x to mirror the image |
| 37 | + col = video.pixels[loc] |
| 38 | + # Code for drawing a single rect |
| 39 | + # Using translate in order for rotation to work properly |
| 40 | + # rectangle size is based on brightness |
| 41 | + sz = g.brightness(col) / 255 * CELL_SIZE |
| 42 | + rect_mode(CENTER) |
41 | 43 | fill(255) |
42 | 44 | no_stroke |
| 45 | + # Rects are larger than the cell for some overlap |
43 | 46 | rect(x + CELL_SIZE / 2, y + CELL_SIZE / 2, sz, sz) |
44 | 47 | end |
45 | 48 | end |
46 | 49 |
|
47 | 50 | def settings |
48 | | - size(480, 360, P2D) |
| 51 | + size(480, 360) |
49 | 52 | end |
50 | 53 | end |
51 | 54 |
|
|
0 commit comments