Processing 의 New Tab 을 클릭하여 PrintIt 이라는 이름으로 새로운 탭 생성 후 다음 코드 입력

// //////////////////////////////////////////////////////////////////////////
// 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.*;

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();
  }

}

사용될 메인 코드에서는 다음과 같이 사용하면 됩니다.

글로벌하게 PrintIt 의 인스턴스 생성
PrintIt p = new PrintIt();

draw() 안에 다음과 같이 작성, 특정키보드를 누르면 현재화면이 프린트 되도록 함
  if(keyPressed && key == 'p') p.printJpg(get(0,0,width,height));




Posted by 알 수 없는 사용자