We closed this forum 18 June 2010. It has served us well since 2005 as the ALPHA forum did before it from 2002 to 2005. New discussions are ongoing at the new URL http://forum.processing.org. You'll need to sign up and get a new user account. We're sorry about that inconvenience, but we think it's better in the long run. The content on this forum will remain online.
IndexProgramming Questions & HelpPrograms › Printer Output
Pages: 1 2 
Printer Output (Read 8970 times)
Printer Output
Oct 16th, 2005, 2:52pm
 
Here's a bit of code that allows to output the stage content  into the default Printer device. This implementation will do so without prompting a dialog window. The program was tested on a winxp manchine with processing V91.

http://pitaru.com/processing/printerOutput/printer_output.zip

/amit
Re: Printer Output
Reply #1 - Dec 8th, 2005, 3:28pm
 
It’s been brought to my attention that there's a bug with my printer code: http://pitaru.com/processing/printerOutput/printer_output.zip

The printed image is a slightly zoomed-in cropped version of the stage. It’s also a bit smaller in size (although that may have something to do with dpi settings).

Any idea's why this is happening? I'm guessing it has something to do with the processing 'g' object being drawn into a Java 2D graphics object... ?

Amit
Re: Printer Output
Reply #2 - Dec 8th, 2005, 7:10pm
 
my guess is that it's more to do with how the java printing api works.. i haven't looked at the most recent version of the code but you should make sure you're paying attention to printer margins (not paper size) and do some reading about how device coordinates are mapped between the screen and the printer in java2d.
Re: Printer Output
Reply #3 - Dec 8th, 2005, 11:13pm
 
yup, that makes more sense. i'll figure it out and post the revised code. thanks!

amit
Re: Printer Output
Reply #4 - May 11th, 2006, 11:52pm
 
Has anyone expanded this successfully?  I'm trying to print directly to the printer as well.
Re: Printer Output
Reply #5 - May 12th, 2006, 7:41pm
 
usage:
Code:

PrintIt p = new PrintIt();
void draw(){
// blabla drawing
if(keyPressed && key == 'p') p.printJpg(get(0,0,width,height));
}


The class:
Code:

// //////////////////////////////////////////////////////////////////////////
// Printer - Jpg and Text (more can easily be implemented)
//
// PrintService http://java.sun.com/j2se/1.4.2/docs/api/javax/print/PrintService.html
// DocFlavor http://java.sun.com/j2se/1.4.2/docs/api/javax/print/DocFlavor.html
// PrintRequestAttributeSet http://java.sun.com/j2se/1.4.2/docs/api/javax/print/attribute/PrintRequestAttributeSet.html
// Attribute http://java.sun.com/j2se/1.4.2/docs/api/javax/print/attribute/Attribute.html
//
//
// Yonas Sandbæk - http://seltar.wliia.org
// //////////////////////////////////////////////////////////////////////////

import javax.print.*;
import javax.print.attribute.*;
import com.sun.image.codec.jpeg.*;

PrintIt p = new PrintIt();
class PrintIt{
PrintService[] services;
PrintService service;
DocFlavor docflavor;
Doc myDoc;
PrintRequestAttributeSet aset;
DocPrintJob job;
PrintIt(){
myDoc = null;
job = null;
services = null;
setService(PrintServiceLookup.lookupDefaultPrintService());
setDocFlavor(DocFlavor.BYTE_ARRAY.AUTOSENSE);
aset = new HashPrintRequestAttributeSet();
}

void setService(PrintService p)
{
service = p;
}

void setDocFlavor(DocFlavor d)
{
docflavor = d;
}

void listPrinters(){
services = PrintServiceLookup.lookupPrintServices(null, null);
for (int i = 0; i < services.length; i++) {
System.out.println(services[i].getName());
DocFlavor[] d = services[i].getSupportedDocFlavors();
for(int j = 0; j < d.length; j++)
System.out.println(" "+d[j].getMimeType());
}
services = null;
}

// prints a given image
void printJpg(PImage img){
setDocFlavor(DocFlavor.BYTE_ARRAY.JPEG);
print(bufferImage(img));
}

// prints a given string
void printString(String s){
setDocFlavor(DocFlavor.BYTE_ARRAY.AUTOSENSE);
print(s.getBytes());
}

boolean print(byte[] b){
if(!service.isDocFlavorSupported(docflavor)){
println("MimeType: \""+docflavor.getMimeType()+"\" not supported by the currently selected printer");
return false;
}

boolean ret = true;
try{
myDoc = new SimpleDoc(b, docflavor, null);
}
catch(Exception e){
println(e);
ret = false;
}

job = service.createPrintJob();
try {
job.print(myDoc, aset);
}
catch (PrintException pe) {
println(pe);
ret = false;
}

return ret;
}

// used with printJpg()
byte[] bufferImage(PImage srcimg){
ByteArrayOutputStream out = new ByteArrayOutputStream();
BufferedImage img = new BufferedImage(srcimg.width, srcimg.height, 2);
img = (BufferedImage)createImage(srcimg.width, srcimg.height);
for(int i = 0; i < srcimg.width; i++)
{
for(int j = 0; j < srcimg.height; j++)
{
int id = j*srcimg.width+i;
img.setRGB(i,j, srcimg.pixels[id]);
}
}
try{
JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
JPEGEncodeParam encpar = encoder.getDefaultJPEGEncodeParam(img);
encpar.setQuality(1,false);
encoder.setJPEGEncodeParam(encpar);
encoder.encode(img);
}
catch(FileNotFoundException e){
System.out.println(e);
}
catch(IOException ioe){
System.out.println(ioe);
}
return out.toByteArray();
}

}

Re: Printer Output
Reply #6 - Jul 23rd, 2006, 3:16pm
 
Hi,

We are trying to printout the stage with pitaru's printer_output.zip but the printed text has a very low resolution.

We tried to make create font with an higher size ( like 150 instead of default 48 ), we tried to work with some java code trying to change the printer resolution but we couldn't...


How is it possible to printout the fonts with a better print resolution?

We are working with mac OS 10.3.9 on a G4, printing with a HP_LaserJet_2200.

Thank you.
Re: Printer Output
Reply #7 - Sep 20th, 2006, 5:11pm
 
Why don't you try to make a pdf-output, at least you should have a great resolution for the vector/line stuff. Make sure you create the Fonst right

see

http://processing.org/discourse/yabb_beta/YaBB.cgi?board=Collaboration;action=display;num=1137255033

I'm not sure, but it should be possible to print the pdf, sure that won't work well in a Browser...

for PDF-printing have a look here, maybe it helps
Re: Printer Output
Reply #8 - Oct 12th, 2006, 12:31pm
 
Hi,

we are still trying to print-out with processing.
we can not use pdf because we would like to print-out directly from an application.

we also tried seltar's code: it works but prints have half the size as the image data... and we couldn't find where to correct it.    

does anyone know how to solve the size problem?
thanks a lot. asli
Re: Printer Output
Reply #9 - Jan 16th, 2007, 7:34pm
 
hello,

did any of you already manage to directly output to a printer from a processing application as a pdf ? this would be very helpful!


all the best

lia.-
Re: Printer Output
Reply #10 - Jan 8th, 2008, 1:59am
 
this doesn't seem to work with opengl on a mac.

has anybody figured out something to print out opengl sketches?
Re: Printer Output
Reply #11 - Mar 10th, 2009, 10:03am
 
Hi everyone,
I'm looking for a way to print pdf from my app.
Can anyone got this working?
If it's not working I'm thinking about using a apple script to bypass the print dialog box each time.Which is not the ideal solution for my case...
Any help will be appreciated...
Re: Printer Output
Reply #12 - Mar 22nd, 2009, 8:24pm
 
Is it possible to print a jpg, that I saved in my sketch folder, direcly from my app. ?
Re: Printer Output
Reply #13 - Mar 22nd, 2009, 8:40pm
 
Basically, yes, Java has a print API.
I can investigate a bit, I am not familiar with it.
Re: Printer Output
Reply #14 - Mar 22nd, 2009, 9:48pm
 
I just tried the code from seltar.
It work!. It does a print job, but everything is turend out very black.
If I save the frame with the "saveFrame() cammand" it looks pretty good. But if i save it like the print (with bufferimage) It has the same black color as the prints.
I also tried to print the "savedFrame() version" but then it also turend out black.
(note; not just black, I can see my image a littlebit.)


I'm trying to make a kind of "photobooth machine" for a school project.
I just startet a week ago with progressing so it's like chinees for me.
A already 'figured' some things out.

PhiLho if you have time to take a look at it and maybe make some improvements.

import processing.video.*;
import javax.print.*;
import javax.print.attribute.*;
import com.sun.image.codec.jpeg.*;

Capture cam;
int row;
int cnt;
PImage fotostrip;
PrintIt p;

void setup() {
 size(256, 768, P2D);
 fotostrip = loadImage("foto0.png");
 p = new PrintIt();
 cam = new Capture(this, 256, 192,30);
 row = 0;
 //background(0);
}

void draw() {
 if (cam.available()) {
   cam.read();
   //set(0,192*3,cam);
   image(cam, 0,cam.height*3);    
 }
}

void keyPressed(){
 if (key == 'b' || key == 'B') {
   copy(0, 576, cam.width, cam.height, 0, row*192, cam.width, cam.height);
   delay(500);
   row+=1;
 }
 if (key == 's' || key == 'S') {
   println ("SAVED TO HD");
   //saveBytes("foto"+cnt+".jpg",bufferImage(get(0,0,width,height)));
   saveFrame("foto"+cnt+".png");
   cnt+=1;
 }
 if(keyPressed && key == 'p'| key == 'P') p.printJpg(fotostrip);
 if(keyPressed && key == 'p'| key == 'P') p.printJpg(get(0,0,width,height));
}

//  ****************  PRINT  ****************
class PrintIt{
 PrintService[] services;
 PrintService service;
 DocFlavor docflavor;
 Doc myDoc;
 PrintRequestAttributeSet aset;
 DocPrintJob job;
 PrintIt(){
   myDoc = null;
   job = null;
   services = null;
   setService(PrintServiceLookup.lookupDefaultPrintService());
   setDocFlavor(DocFlavor.BYTE_ARRAY.AUTOSENSE);
   aset =  new HashPrintRequestAttributeSet();
 }

 void setService(PrintService p)
 {
   service = p;
 }

 void setDocFlavor(DocFlavor d)
 {
   docflavor = d;  
 }

 void listPrinters(){
   services = PrintServiceLookup.lookupPrintServices(null, null);
   for (int i = 0; i < services.length; i++) {
     System.out.println(services[i].getName());
     DocFlavor[] d = services[i].getSupportedDocFlavors();
     for(int j = 0; j < d.length; j++)
       System.out.println("  "+d[j].getMimeType());
   }
   services = null;
 }

 // prints a given image
 void printJpg(PImage img){
   setDocFlavor(DocFlavor.BYTE_ARRAY.JPEG);
   print(bufferImage(img));
 }  

 boolean print(byte[] b){
   if(!service.isDocFlavorSupported(docflavor)){
     println("MimeType: \""+docflavor.getMimeType()+"\" not supported by the currently selected printer");
     return false;
   }

   boolean ret = true;
   try{
     myDoc = new SimpleDoc(b, docflavor, null);  
   }
   catch(Exception e){
     println(e);
     ret = false;
   }  

   job = service.createPrintJob();
   try {
     job.print(myDoc, aset);
   }  
   catch (PrintException pe) {
     println(pe);
     ret = false;

   }
   println("PRINT" + ret);
   return ret;
 }

 // used with printJpg()
 byte[] bufferImage(PImage srcimg){
   ByteArrayOutputStream out = new ByteArrayOutputStream();
   BufferedImage img = new BufferedImage(srcimg.width, srcimg.height, 2);
   img = (BufferedImage)createImage(srcimg.width, srcimg.height);
   for(int i = 0; i < srcimg.width; i++)
   {
     for(int j = 0; j < srcimg.height; j++)
     {
       int id = j*srcimg.width+i;
       img.setRGB(i,j, srcimg.pixels[id]);  
     }
   }
   try{
     JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
     JPEGEncodeParam encpar = encoder.getDefaultJPEGEncodeParam(img);
     encpar.setQuality(1,false);
     encoder.setJPEGEncodeParam(encpar);
     encoder.encode(img);
   }
   catch(FileNotFoundException e){
     System.out.println(e);
   }
   catch(IOException ioe){
     System.out.println(ioe);
   }
   return out.toByteArray();
 }

}


// ***************  BUFFER IMAGE  ****************
byte[] bufferImage(PImage srcimg){
 ByteArrayOutputStream out = new ByteArrayOutputStream();
 BufferedImage img = new BufferedImage(srcimg.width, srcimg.height, 2);
 img = (BufferedImage)createImage(srcimg.width,srcimg.height);
 for(int i = 0; i < srcimg.width; i++)
   for(int j = 0; j < srcimg.height; j++)
     img.setRGB(i,j,srcimg.pixels[j*srcimg.width+i]);
 try{
   JPEGImageEncoder encoder = JPEGCodec.createJPEGEncoder(out);
   JPEGEncodeParam encpar = encoder.getDefaultJPEGEncodeParam(img);
   encpar.setQuality(1,false);
   encoder.setJPEGEncodeParam(encpar);
   encoder.encode(img);
 }
 catch(FileNotFoundException e){
   System.out.println(e);
 }
 catch(IOException ioe){
   System.out.println(ioe);
 }
 return out.toByteArray();
}
Pages: 1 2