angelo
YaBB Newbies
Offline
Posts: 16
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(); }