Hey All,
I couldn’t find any examples of how to print from OF, so I spent today working out how to do it.
Download gutenprint-5.2.3.tar.bz2 from
http://gimp-print.sourceforge.net/MacOSX.php
And the DMG if you want to install the drivers, but it should all be there.
[API-overview-here:–
http://www.cups.org/documentation.php/api-overview.html
How to print here:
http://www.cups.org/documentation.php/api-cups.html
Probably should be using their native api for file IO but couldn’t be bothered.
http://www.cups.org/documentation.php/api-filedir.html]
opened it up, went in and typed
./configure
make
then
sudo make install
Then added it to an OF project by:
In the project window, double-click on the Targets group and control-click on the simple target to show the context menu. Choose Existing Framework… from the Add submenu. When the file chooser sheet appears, press the / key and enter “/usr/lib”. Scroll down the file list and select the libcups.dylib file. Click the Add button in the file chooser and attributes sheets.
Then the following source, with a JPG file in the data folder called bikers.jpg in it, should work. You’ll have to comment out the printing bits the first time around - to get the correct name of the printer. Don’t forget to change it, as it will be different from mine.
All done using a duplicated version of the alladdons project from:
http://www.openframeworks.cc/files/0057-…-ithGui.zip
Source follows:
[testApp.cpp]
#include "testApp.h"
//--------------------------------------------------------------
void testApp::setup(){
ofBackground(127,127,127);
int i;
cups_dest_t *dests, *dest, *correctPrinter;
int num_dests = cupsGetDests(&dests);
string theRightPrinterName = string("MP620_series__00_00_85_ee_44_3e_");
for (i = num_dests, dest = dests; i > 0; i --, dest ++)
{
const char *value;
value = cupsGetOption("printer-info", dest->num_options, dest->options);
printf("%s (%s)\n", dest->name, value ? value : "no description");
if(theRightPrinterName == dest->name)
{
correctPrinter = dest;
}
}
int job_id;
/* Print a single file */
//job_id = cupsPrintFile(correctPrinter->name, "/usr/share/cups/data/testprint.ps",
// "Test Print JGL", correctPrinter->num_options, correctPrinter->options);
string thePath = ofToDataPath("bikers.jpg");
char * cstr;
cstr = new char [thePath.size()+1];
strcpy (cstr, thePath.c_str());
job_id = cupsPrintFile(correctPrinter->name, cstr ,
"Test Print JGL3", correctPrinter->num_options, correctPrinter->options);
if (job_id == 0)
cout << "Printer error is " << cupsLastError() << endl;
cupsFreeDests(num_dests, dests);
delete[] cstr;
}
//--------------------------------------------------------------
void testApp::update(){
}
//--------------------------------------------------------------
void testApp::draw(){
ofSetColor(0xffffff);
ofDrawBitmapString("This app just prints. Have a look at the console for feedback on printer names.", 100,100);
}
//--------------------------------------------------------------
void testApp::keyPressed (int key){
}
//--------------------------------------------------------------
void testApp::keyReleased (int key){
}
//--------------------------------------------------------------
void testApp::mouseMoved(int x, int y ){
}
//--------------------------------------------------------------
void testApp::mouseDragged(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mousePressed(int x, int y, int button){
}
//--------------------------------------------------------------
void testApp::mouseReleased(){
}
[testApp.h]
#ifndef _TEST_APP
#define _TEST_APP
// JUST TO MAKE SURE IT DOESN'T BREAK ADDONS
#define OF_ADDON_USING_OFXOPENCV
#define OF_ADDON_USING_OFXOBJLOADER
#define OF_ADDON_USING_OFXDIRLIST
#define OF_ADDON_USING_OFXVECTORMATH
#define OF_ADDON_USING_OFXNETWORK
#define OF_ADDON_USING_OFXVECTORGRAPHICS
#define OF_ADDON_USING_OFXOSC
#define OF_ADDON_USING_OFXTHREAD
#define OF_ADDON_USING_OFXXMLSETTINGS
#include "ofMain.h"
#include "ofAddons.h"
#include <cups/cups.h>
class testApp : public ofSimpleApp{
public:
void setup();
void update();
void draw();
void keyPressed (int key);
void keyReleased (int key);
void mouseMoved(int x, int y );
void mouseDragged(int x, int y, int button);
void mousePressed(int x, int y, int button);
void mouseReleased();
// we don't actually use these
// just checking to see if they
// all work in the same place :)
ofxCvGrayscaleImage cvGray;
ofxObjLoader obj;
ofxDirList dirList;
ofxVec2f p;
ofxTCPClient client;
ofxTCPServer server;
ofxOscSender osc_sender;
ofxThread thread;
ofxXmlSettings settings;
};
#endif
If CUPs is working correctly then you should be able to type:
Into your browser to look at the printer names, and various print queues, as well as running test prints.
Cheers,
JGL