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 script, custom paper size and NO margins
Page Index Toggle Pages: 1
Printer script, custom paper size and NO margins (Read 2623 times)
Printer script, custom paper size and NO margins
Jan 25th, 2010, 1:35pm
 
Hi all,

I've spended hours now on this project
But I'm just unable to fix the next thing
REALLY hope you can help me.

So this is the deal: (fictive)
Code:

void setup() {
     size( 400, 300,
}

void draw() {
  print( 10, 20 , 30, 30);
}


I know this code above is just bullPucky
But it comes down to that I want to print this
area lets say every 2 seconds.

Now the important part. The page should be
this 30 x 30 px and NO margins.

So, you think its possilbe?
Re: Print script with custom page size and NO margins
Reply #1 - Jan 25th, 2010, 4:49pm
 
So you want to draw a rectangle on a small sketch every 2 seconds?
How long do you want the rectangle to stay drawn for?

EDIT: Something like this?

Quote:
void setup(){
  size(400,300);
  fill(128,0,0);
  noStroke();
}

void draw(){
  background(0,64,0);
  if((millis()%2500)<500){
    rect(10,20,30,30);
  }
}


Re: Print script with custom page size and NO margins
Reply #2 - Jan 25th, 2010, 11:44pm
 
Oh I'm sorry. Looks like I'm talking
in the wrong jargon.

What I like to see is this "print" region (which is of course not a correct code) Printed out on a actual hardware printer.

I've seen two print approaches now. But Both I can't seem
to set the page size and margins.

Thanks for the reply though ^^
Re: Print script with custom page size and NO margins
Reply #3 - Jan 25th, 2010, 11:57pm
 
Oh, I see. You actually want to print out (on paper) an image generated one block at a time.

While I really can't help you with getting your printer to print something via processing, I can make this suggestion: Generate the entire image you want to print *before* you print it. It'll be much easier, and Processing will have no problem "remembering" the whole image you're building as you build it. (Of course this goes right out the window if the whole point is to print out your thing one block at a time.)
Re: Print script with custom page size and NO margins
Reply #4 - Jan 26th, 2010, 3:16am
 
So ok this is what I have so far:

Code:
    class PrintIt{
    PrintService[] services;
    PrintService service;
    DocFlavor docflavor;
    Doc myDoc;
    PrintRequestAttributeSet aset =  new HashPrintRequestAttributeSet();
    DocPrintJob job;
    PrintIt(){
      myDoc = null;
      job = null;
      services = null;
      setService(PrintServiceLookup.lookupDefaultPrintService());
      setDocFlavor(DocFlavor.BYTE_ARRAY.AUTOSENSE);
//     aset.add(MediaSize.findMedia(1, 1, MediaSize.INCH));
      aset.add(MediaSize(1, 1, MediaSize.MM));
    }


The commented out works only so-so because it finds
the closest existing size. And I need a NEW size.

How do I do this? The Vars next are the sizes I want to get (px)
int VarBoxH = 30;
int VarBoxW = 40;
dpi is 240

I'm getting an error:
The function MediaSize(int, int, int) does not exist.
Re: Print script with custom page size and NO margins
Reply #5 - Jan 26th, 2010, 5:34am
 
timmy wrote on Jan 26th, 2010, 3:16am:
I'm getting an error:
The function MediaSize(int, int, int) does not exist.

Add Code:
import javax.print.attribute.standard.MediaSize; 

at the start of the sketch.
Re: Print script with custom page size and NO margins
Reply #6 - Jan 26th, 2010, 6:25am
 
Check thats already there,

some imports:
Code:
    import javax.print.*;
   import javax.print.attribute.*;
   import com.sun.image.codec.jpeg.*;
Re: Print script with custom page size and NO margins
Reply #7 - Jan 26th, 2010, 7:31am
 
I don't understand your answer. But to remain generic, you can add: Code:
import javax.print.attribute.standard.*; 

to your list.

Sorry if I can't help much more, I dived into printing in Java and was a bit overwhelmed by the complexity. I can manage that complexity, but just hadn't time yet to explore it further.

Out of curiosity, do you have a special printer that manage so small images? A kind of sticky ribbon printer?
Re: Print script with custom page size and NO margins
Reply #8 - Jan 26th, 2010, 7:55am
 
Thanks again for the reply,

What I meant to say is that I already had those on top of my script.
I'm printing to a DotMatrix printer. (school project)
Re: Printer script, custom paper size and NO margins
Reply #9 - Jan 26th, 2010, 8:57am
 
The import I show isn't in your list. * isn't recursive, it is flat.
Re: Printer script, custom paper size and NO margins
Reply #10 - Jan 26th, 2010, 10:34am
 
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();
 }

}
Page Index Toggle Pages: 1