Skip to content

Commit 3066862

Browse files
authored
[Processing] Simplified image arrays (#4)
Move image array declarations outside of draw loop and removed the image double buffering.
1 parent 210c16b commit 3066862

File tree

1 file changed

+6
-15
lines changed

1 file changed

+6
-15
lines changed

extras/CameraVisualizerRawBytes/CameraVisualizerRawBytes.pde

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -17,24 +17,23 @@ Serial myPort;
1717
// must match resolution used in the sketch
1818
final int cameraWidth = 320;
1919
final int cameraHeight = 240;
20-
2120
final int cameraBytesPerPixel = 2;
22-
2321
final int bytesPerFrame = cameraWidth * cameraHeight * cameraBytesPerPixel;
2422

2523
PImage myImage;
24+
byte[] frameBuffer = new byte[bytesPerFrame];
2625

2726
void setup()
2827
{
2928
size(320, 240);
3029

3130
// if you have only ONE serial port active
32-
//myPort = new Serial(this, Serial.list()[0], 9600); // if you have only ONE serial port active
31+
//myPort = new Serial(this, Serial.list()[0], 9600); // if you have only ONE serial port active
3332

3433
// if you know the serial port name
3534
//myPort = new Serial(this, "COM5", 9600); // Windows
36-
//myPort = new Serial(this, "/dev/ttyACM0", 9600); // Linux
37-
myPort = new Serial(this, "/dev/cu.usbmodem14101", 9600); // Mac
35+
//myPort = new Serial(this, "/dev/ttyACM0", 9600); // Linux
36+
myPort = new Serial(this, "/dev/cu.usbmodem14401", 9600); // Mac
3837

3938
// wait for full frame of bytes
4039
myPort.buffer(bytesPerFrame);
@@ -48,21 +47,15 @@ void draw()
4847
}
4948

5049
void serialEvent(Serial myPort) {
51-
byte[] frameBuffer = new byte[bytesPerFrame];
52-
5350
// read the saw bytes in
5451
myPort.readBytes(frameBuffer);
5552

56-
// create image to set byte values
57-
PImage img = createImage(cameraWidth, cameraHeight, RGB);
58-
5953
// access raw bytes via byte buffer
6054
ByteBuffer bb = ByteBuffer.wrap(frameBuffer);
6155
bb.order(ByteOrder.BIG_ENDIAN);
6256

6357
int i = 0;
6458

65-
img.loadPixels();
6659
while (bb.hasRemaining()) {
6760
// read 16-bit pixel
6861
short p = bb.getShort();
@@ -73,10 +66,8 @@ void serialEvent(Serial myPort) {
7366
int b = ((p >> 0) & 0x1f) << 3;
7467

7568
// set pixel color
76-
img.pixels[i++] = color(r, g, b);
69+
myImage .pixels[i++] = color(r, g, b);
7770
}
78-
img.updatePixels();
71+
myImage .updatePixels();
7972

80-
// assign image for next draw
81-
myImage = img;
8273
}

0 commit comments

Comments
 (0)