|
| 1 | +/* |
| 2 | + This sketch reads a raw Stream of RGB565 pixels |
| 3 | + from the Serial port and displays the frame on |
| 4 | + the window. |
| 5 | + Use with the Examples -> CameraCaptureRawBytes Arduino sketch. |
| 6 | + This example code is in the public domain. |
| 7 | +*/ |
| 8 | + |
| 9 | +import processing.serial.*; |
| 10 | +import java.nio.ByteBuffer; |
| 11 | +import java.nio.ByteOrder; |
| 12 | + |
| 13 | +Serial myPort; |
| 14 | + |
| 15 | +// must match resolution used in the sketch |
| 16 | +final int cameraWidth = 324; |
| 17 | +final int cameraHeight = 244; |
| 18 | +final int cameraBytesPerPixel = 1; |
| 19 | +final int bytesPerFrame = cameraWidth * cameraHeight * cameraBytesPerPixel; |
| 20 | + |
| 21 | +PImage myImage; |
| 22 | +byte[] frameBuffer = new byte[bytesPerFrame]; |
| 23 | + |
| 24 | +void setup() |
| 25 | +{ |
| 26 | + size(324, 244); |
| 27 | + |
| 28 | + // if you have only ONE serial port active |
| 29 | + //myPort = new Serial(this, Serial.list()[0], 9600); // if you have only ONE serial port active |
| 30 | + |
| 31 | + // if you know the serial port name |
| 32 | + //myPort = new Serial(this, "COM5", 9600); // Windows |
| 33 | + myPort = new Serial(this, "/dev/ttyACM0", 9600); // Linux |
| 34 | + //myPort = new Serial(this, "/dev/cu.usbmodem14401", 9600); // Mac |
| 35 | + |
| 36 | + // wait for full frame of bytes |
| 37 | + myPort.buffer(bytesPerFrame); |
| 38 | + |
| 39 | + myImage = createImage(cameraWidth, cameraHeight, ALPHA); |
| 40 | +} |
| 41 | + |
| 42 | +void draw() |
| 43 | +{ |
| 44 | + image(myImage, 0, 0); |
| 45 | +} |
| 46 | + |
| 47 | +void serialEvent(Serial myPort) { |
| 48 | + // read the saw bytes in |
| 49 | + myPort.readBytes(frameBuffer); |
| 50 | + |
| 51 | + // access raw bytes via byte buffer |
| 52 | + ByteBuffer bb = ByteBuffer.wrap(frameBuffer); |
| 53 | + bb.order(ByteOrder.BIG_ENDIAN); |
| 54 | + |
| 55 | + int i = 0; |
| 56 | + |
| 57 | + while (bb.hasRemaining()) { |
| 58 | + // read 16-bit pixel |
| 59 | + byte p = bb.get(); |
| 60 | + |
| 61 | + |
| 62 | + // set pixel color |
| 63 | + myImage .pixels[i++] = color(Byte.toUnsignedInt(p)); |
| 64 | + } |
| 65 | + myImage.updatePixels(); |
| 66 | + |
| 67 | +} |
0 commit comments