Last active
December 30, 2015 05:19
-
-
Save shiffman/7782227 to your computer and use it in GitHub Desktop.
printing an image from Processing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.FileInputStream; | |
import java.io.IOException; | |
import javax.print.Doc; | |
import javax.print.DocFlavor; | |
import javax.print.DocPrintJob; | |
import javax.print.PrintException; | |
import javax.print.PrintService; | |
import javax.print.PrintServiceLookup; | |
import javax.print.SimpleDoc; | |
import javax.print.attribute.HashPrintRequestAttributeSet; | |
import javax.print.attribute.PrintRequestAttributeSet; | |
import javax.print.attribute.standard.Copies; | |
void setup() { | |
} | |
void draw() { | |
} | |
void mousePressed() { | |
try { | |
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet(); | |
pras.add(new Copies(1)); | |
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras); | |
if (pss.length == 0) | |
throw new RuntimeException("No printer services available."); | |
PrintService ps = pss[0]; | |
System.out.println("Printing to " + ps); | |
DocPrintJob job = ps.createPrintJob(); | |
FileInputStream fin = new FileInputStream(dataPath("file.png")); | |
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null); | |
job.print(doc, pras); | |
fin.close(); | |
} | |
catch (Exception e) { | |
e.printStackTrace(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment