Hm, I think its time for a bit more code
This is the most "cleanest" version of the script.
In fact this part is as good as the exact script from
seltar So the main focus right now would be the line between the
four backslash lines.
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 javax.print.attribute.standard.MediaSizeName;
import javax.print.attribute.standard.MediaSize;
import javax.print.DocFlavor;
import javax.print.PrintService;
import javax.print.PrintServiceLookup;
import javax.print.ServiceUI;
import javax.print.attribute.HashPrintRequestAttributeSet;
import javax.print.attribute.PrintRequestAttributeSet;
import javax.print.attribute.standard.MediaPrintableArea;
import javax.print.attribute.standard.MediaSize;
import javax.print.attribute.standard.*;
import com.sun.image.codec.jpeg.*;
PrintIt p = new PrintIt();
void draw(){
// blabla drawing
if(keyPressed && key == 'p') p.printJpg(get(0,0,width,height));
}
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();
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
//aset.add(MediaSize.findMedia(0.5, 0.5, MediaSize.INCH));
aset.add(MediaSize(10, 10, Size2DSyntax.MM));
////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////
}
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();
}
}