Hi guys, I’m looking for some help on a project I have been working on. I am using Processing and Arduino in this project and have both sides working; I just need help linking the two together.
The idea is that an image is taken on Processing and is converted into a bitmap image. This image will be printed using a thermal printer on Arduino.
I have the camera working on Processing; it takes a b&w image (small enough for the printer), then converts it to a bitmap image and saves it ready to be printed. I have to manually upload the image to the Arduino sketch folder and run the code, which prints perfectly.
I need help with how to print the image directly after it has been taken. Is there a way to get Arduino to print this file straight away? I have looked into passing information through the serial, but I’m unsure how to pass a bitmap image this way and then print it. Any help on this would be much appreciated!!
Thank you in advance
Processing code:
Takes a 210x210 b&w image once a key is pressed, converts it to a bitmap and saves it as "imgTest.png"
import processing.video.*;
Capture cam;
int state = 0;
void setup() {
size(210, 210);
String[] cameras = Capture.list();
cam = new Capture(this, cameras[1]);
cam.start();
}
void draw() {
if (cam.available() == true) {
cam.read();
}
scale(-1, 1);
image(cam, -width, height/150, width, height);
//filter(GRAY);
filter(THRESHOLD, 0.5);
}
void keyPressed() {
saveFrame("imgTest.png");
//selectInput("imgTest.png", "processImage");
processImage();
}
void processImage() {
String filename, basename;
PImage img;
PrintWriter output;
int pixelNum, byteNum, bytesOnLine = 99,
x, y, b, rowBytes, totalBytes, lastBit, sum;
println("Loading image...");
filename = "imgTest.png";
img = loadImage("imgTest.png");
// Morph filename into output filename and base name for data
x = filename.lastIndexOf('.');
if(x > 0) filename = filename.substring(0, x); // Strip current extension
x = filename.lastIndexOf('/');
if(x > 0) basename = filename.substring(x + 1); // Strip path
else basename = filename;
filename += ".h"; // Append '.h' to output filename
println("Writing output to " + filename);
// Calculate output size
rowBytes = (img.width + 7) / 8;
totalBytes = rowBytes * img.height;
// Convert image to B&W, make pixels readable
img.filter(THRESHOLD);
img.loadPixels();
// Open header file for output (NOTE: WILL CLOBBER EXISTING .H FILE, if any)
output = createWriter(filename);
// Write image dimensions and beginning of array
output.println("#ifndef _" + basename + "_h_");
output.println("#define _" + basename + "_h_");
output.println();
output.println("#define " + basename + "_width " + img.width);
output.println("#define " + basename + "_height " + img.height);
output.println();
output.print("static const uint8_t PROGMEM " + basename + "_data[] = {");
// Generate body of array
for(pixelNum=byteNum=y=0; y<img.height; y++) { // Each row...
for(x=0; x<rowBytes; x++) { // Each 8-pixel block within row...
lastBit = (x < rowBytes - 1) ? 1 : (1 << (rowBytes * 8 - img.width));
sum = 0; // Clear accumulated 8 bits
for(b=128; b>=lastBit; b >>= 1) { // Each pixel within block...
if((img.pixels[pixelNum++] & 1) == 0) sum |= b; // If black pixel, set bit
}
if(++bytesOnLine >= 10) { // Wrap nicely
output.print("\n ");
bytesOnLine = 0;
}
output.format(" 0x%02X", sum); // Write accumulated bits
if(++byteNum < totalBytes) output.print(',');
}
}
// End array, close file, exit program
output.println();
output.println("};");
output.println();
output.println("#endif // _" + basename + "_h_");
output.flush();
output.close();
println("Done!");
exit();
}
Arduino code:
Have to upload the "imgTest.png" image manually to the sketch folder, once uploaded it prints through the thermal printer
#include "imgTest.h"
#include "SoftwareSerial.h"
#define TX_PIN 6 // Arduino transmit YELLOW WIRE labeled RX on printer
#define RX_PIN 5 // Arduino receive GREEN WIRE labeled TX on printer
SoftwareSerial mySerial(RX_PIN, TX_PIN); // Declare SoftwareSerial obj first
Adafruit_Thermal printer(&mySerial); // Pass addr to printer constructor
void setup() {
mySerial.begin(19200); // Initialize SoftwareSerial
printer.begin(); // Init printer (same regardless of serial type)
printer.printBitmap(imgTest_width, imgTest_height, imgTest_data);
printer.feed(3);
printer.sleep(); // Tell printer to sleep
delay(3000L); // Sleep for 3 seconds
printer.wake(); // MUST wake() before printing again, even if reset
printer.setDefault(); // Restore printer to defaults
}
void loop() {
}