Printing page margins && picture quality

1.- Is it possible from a java application to specify pirnting page margins?

2.- Is it possible for a java application to specify custom printer options like "Enable laminating" this is a id zebra printer? (this parameters appears on the windows printer driver dialog box)

3.- I am trying to print out a picture from a jpg file from a java application, the print out seems to have a color pallete or resolution quality probelm, but printing from Microsoft Word the same picture and using the same printer I am getting a much better quality picture, event both prints have the same size, how to improve print quality?.

publicint print(Graphics g, PageFormat pf,int page)

throws PrinterException

{

int iWmm, iHmm;// milimeters

int iW, iH;// print coords

Image image;

// Printing a picture of 20 mm by 20 mm

iWmm = 20;

iHmm = 20;

if( page == 0)

{

iW = (int)(iWmm*2.755);

iH = (int)(iHmm*2.755);

image = load("colorpicture.jpg");

g2.translate(pf.getImageableX(), pf.getImageableY());

g2.drawImage(image, 0, 0, iW, iH,null);

return PAGE_EXISTS;

}

return NO_SUCH_PAGE;

}

public Image load(String strImg)

{

Image im = Toolkit.getDefaultToolkit().getImage(strImg);

MediaTracker mt =newMediaTracker(new JPanel());

mt.addImage(im, 0);

try

{

mt.waitForID(0);

}

catch (InterruptedException e)

{

System.err.println("Unexpected interrupt in waitForID!");

}

if (mt.isErrorID(0))

{

System.err.println("Couldn't load image file " + strImg);

}

return im;

}

Thanks in advance cvmontuy

[2860 byte] By [cvmontuya] at [2007-11-27 2:49:33]
# 1

1 & 2:

Seems like you should be able to do what you want with the pageDialog of PrinterJob:

PrinterJob printerJob = PrinterJob.getPrinterJob();

printerJob.setJobName("Printing table");

defaultPageFormat = printerJob.defaultPage();

defaultPageFormat = printerJob.pageDialog(defaultPageFormat);

I also notice that the reguler print dialog has a properties button that opens the normal printer properties window.

printerJob.setPageable(this);

if (printerJob.printDialog())

{

try {

printerJob.print();

}

catch (PrinterException e)

{

e.printStackTrace();

}

}

BTW this is in Java 5 on Windows. If this doesn't work on your target platform, let me know.

The Java2D tutorial has a section on printing:

http://java.sun.com/docs/books/tutorial/2d/printing/index.html

3. You might try to scale the image ahead of time

g2.scale(...)

, but maybe someone else can help with this.

Message was edited by:

pthorson

pthorsona at 2007-7-12 3:20:56 > top of Java-index,Desktop,Core GUI APIs...
# 2

> 1.- Is it possible from a java application to specify

> pirnting page margins?

Definitely. You can use the PageFormat class and set the imageable bounds on that

> 2.- Is it possible for a java application to specify

> custom printer options like "Enable laminating" this

> is a id zebra printer? (this parameters appears on

> the windows printer driver dialog box)

I don't know. It might be. Best bet might be to try pulling a printdialog in java, selecting that option and seeing if it works.

tjacobs01a at 2007-7-12 3:20:56 > top of Java-index,Desktop,Core GUI APIs...
# 3

I am working with a zebra id printer

And what I need to do is avoiding the user to change for every print the printing parameters using the printer properties dialog box

The application knows that the printing must use:

: Landscape, Use two sides, use laminator, use color on both sides

1.- And yes manually the user is able to change this but what i whant is set this automatically from the java application.

cvmontuya at 2007-7-12 3:20:56 > top of Java-index,Desktop,Core GUI APIs...
# 4

Printing Quality

I am printing a 2012 x 1271 pixels jpg image on a printer with 200 dpi磗 on a space of 3.3 x 2.1 inchs

If i print the same picture with ms word I get a very good quality picture but printing from the java application gets the picture like in lower resolution and with some black dots over white areas.

// Adding this hints helps very little if any on the print out

g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION,RenderingHints.VALUE_INTERPOLATION_BICUBIC);

g2.setRenderingHint(RenderingHints.KEY_RENDERING, RenderingHints.VALUE_RENDER_QUALITY);

g2.setRenderingHint(RenderingHints.KEY_COLOR_RENDERING, RenderingHints.VALUE_COLOR_RENDER_QUALITY);

g2.setRenderingHint(RenderingHints.KEY_DITHERING , RenderingHints.VALUE_DITHER_ENABLE);

cvmontuya at 2007-7-12 3:20:56 > top of Java-index,Desktop,Core GUI APIs...
# 5
Update:Using other kind of printers the print out is perfect!This seems to be a printer or printer driver problem, but this problem only happens from my java application and not happens using MS Word
cvmontuya at 2007-7-12 3:20:57 > top of Java-index,Desktop,Core GUI APIs...
# 6

It is likely that when you send your print job from Java, it's sending between 72 and 96 dpi. In other words, you have scaled the image prematurely. When the printer gets the data, it says, "Oh look, the data is only 96 dpi, but I need 200 dpi, so I'm going to have to scale up the data" and hence the bad quality.

In word, you insert the image and then drag the corner to the appropriate size. Word keeps all of the original image data, so when it's sent to the printer it is sending at 610 dpi. Since your printer only supports 200 dpi, your printer is going to have to scale the image down. Scaling down is usually OK in terms of quality, unless your printer has a poor scaling algorithm. If it does, then you can scale your image down using Java.

One possible solution is to create a PostScript file on the fly with the JPEG data encoded in it. The Java Print APIs might also have a solution for this. I haven't had good experience with Java and printing.

rkippena at 2007-7-12 3:20:57 > top of Java-index,Desktop,Core GUI APIs...
# 7

Thx.

The image before the printing has a good resolution.

System.out.println("ImageW=" + image.getWidth(null));

System.out.println("ImageH=" +image.getHeight(null)`);

g2.drawImage(image, 0, 0, iW, iH, null);

And this shows that the image is (2012, 1271) pixels and iW and IH are fine cause the print out size is perfect but not the resolution

the printing coordinates are 995, 625 so it has to scale down to more than 50% And As I mention before printing on others printers the resolution is perfect but not on the zebra P520i.

cvmontuy

cvmontuya at 2007-7-12 3:20:57 > top of Java-index,Desktop,Core GUI APIs...
# 8

Try setting the scale instead. I think the drawImage method is scaling ahead of time.

import java.awt.*;

import java.awt.print.*;

import javax.imageio.*;

import java.io.*;

import java.awt.geom.*;

public class PrintEx implements Printable {

private Image img = null;

public PrintEx(Image img) {

this.img = img;

}

public static void main(String[] args) throws Throwable {

Image img = ImageIO.read(new File(args[0]));

PrintEx pex = new PrintEx(img);

PrinterJob job = PrinterJob.getPrinterJob();

job.setPrintable(pex);

job.print();

}

public int print(Graphics _g, PageFormat pf, int page) throws PrinterException {

if (page > 0) return NO_SUCH_PAGE;

Graphics2D g = (Graphics2D) _g;

g.translate(pf.getImageableX(), pf.getImageableY());

System.out.println(pf.getImageableX() + " " + pf.getImageableY());

int w = img.getWidth(null);

int h = img.getHeight(null);

g.scale(0.5, 0.5);

g.drawImage(img, 0, 0, w, h, null);

return PAGE_EXISTS;

}

}

rkippena at 2007-7-12 3:20:57 > top of Java-index,Desktop,Core GUI APIs...
# 9

One thing I noticed was that Java encoded all the data as JPEG in the postscript output. This is disadvantageous because it means that lossyless formats like png are going to be DCT encoded, and lose more information, and JPEGs are going to be DCT encoded one extra time. If you really want high quality prints from your application, you'll generate the PostScript file yourself.

rkippena at 2007-7-12 3:20:57 > top of Java-index,Desktop,Core GUI APIs...
# 10

Finally to improve quality the solution was to implement a

BufferedImageOp to filter the image before printing

Image img = createImage(new FilteredImageSource(img.getSource(), new CustomBufferedImageOp()));

many examples can be found on www.jhlabs.com/ip/filters/

In order to:

Supress background and noice

Reduce number of colors

Incress contrast

....

cvmontuya at 2007-7-12 3:20:57 > top of Java-index,Desktop,Core GUI APIs...