Discussion:
Print BufferedImage the result is blank
(too old to reply)
p***@gmail.com
2013-10-15 08:56:33 UTC
Permalink
Hi, every one

following are parameter set of my environment

/////////////////

OS: Win 7 64 bit
Java: java version "1.6.0_29" 32 bit
Test Printer: CutePDF Writer 2.8 、Microsoft XPS Document Writer

///////////

I try to print a image to printer, while the image file size is small the result is correct,
but while the file is large (jpeg format about 1.4 MB) the result is blank!!
I have assign -Xmx1024m to JVM.

following is the code to print, can any one give some suggestions
///////////////

public class TestPrinter2 {
public static void main(String[] args) throws Exception {
PrintService service = PrintServiceLookup.lookupDefaultPrintService();

PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();

set.add(new Copies(1));


PrinterJob pj = PrinterJob.getPrinterJob();

if (pj.printDialog(set)) {

service = pj.getPrintService();

final BufferedImage img = ImageIO.read(new File("C:/TEMP/large.jpg"));

DocFlavor inFlavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;

Doc doc = new SimpleDoc(new Printable() {

public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 0)
return Printable.NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D) graphics;
g2d.drawImage(img, (int)pageFormat.getImageableX(), (int)pageFormat.getImageableY(), (int)pageFormat.getWidth(), (int)pageFormat.getHeight(),null);
return Printable.PAGE_EXISTS;
}

}, inFlavor, null);

DocPrintJob job = service.createPrintJob();

job.print(doc, set);
}
}
}

//////////////////

thanks for reply
Joerg Meier
2013-10-15 11:27:05 UTC
Permalink
Post by p***@gmail.com
OS: Win 7 64 bit
Java: java version "1.6.0_29" 32 bit
And that didn't strike you as odd ? I recommend starting by getting the
upropriate (and current) version of Java for your machine. Your Java
version is two years old and has many large and dangerous security flaws.
Note that Java 7 is the current version. Java 6 is no longer even supported
officially.

As for your problem, you are not giving the image any time to load, and
proceed to trying to print it without ever even checking if it was finished
loading, or loaded even partially.

I'm pretty rusty at loading Images through Java libraries, so I hope this
is the apropriate way, if not, maybe someone else here could correct me.
You get a decent example at

<http://docs.oracle.com/javase/7/docs/api/java/awt/MediaTracker.html>

and you don't need to worry about the Applet crap, but MediaTracker does
need a Component for some obscure reason. You can simply pass it a new
JFrame() and all should be good.

Liebe Gruesse,
Joerg
--
Ich lese meine Emails nicht, replies to Email bleiben also leider
ungelesen.
Knute Johnson
2013-10-15 14:53:05 UTC
Permalink
Post by Joerg Meier
Post by p***@gmail.com
OS: Win 7 64 bit
Java: java version "1.6.0_29" 32 bit
And that didn't strike you as odd ? I recommend starting by getting the
upropriate (and current) version of Java for your machine. Your Java
version is two years old and has many large and dangerous security flaws.
Note that Java 7 is the current version. Java 6 is no longer even supported
officially.
As for your problem, you are not giving the image any time to load, and
proceed to trying to print it without ever even checking if it was finished
loading, or loaded even partially.
I'm pretty rusty at loading Images through Java libraries, so I hope this
is the apropriate way, if not, maybe someone else here could correct me.
You get a decent example at
<http://docs.oracle.com/javase/7/docs/api/java/awt/MediaTracker.html>
and you don't need to worry about the Applet crap, but MediaTracker does
need a Component for some obscure reason. You can simply pass it a new
JFrame() and all should be good.
Liebe Gruesse,
Joerg
I'm not sure what his problem is but waiting for the image to load isn't
it. javax.ImageIO.read() loads the complete image before returning from
the call.
--
Knute Johnson
Jeff Higgins
2013-10-15 16:04:00 UTC
Permalink
Post by p***@gmail.com
Hi, every one
following are parameter set of my environment
/////////////////
OS: Win 7 64 bit
Java: java version "1.6.0_29" 32 bit
Test Printer: CutePDF Writer 2.8 、Microsoft XPS Document Writer
///////////
I try to print a image to printer, while the image file size is small the result is correct,
but while the file is large (jpeg format about 1.4 MB) the result is blank!!
I have assign -Xmx1024m to JVM.
following is the code to print, can any one give some suggestions
[snip code]

While I can't specifically answer your question, these links might
help.<http://docs.oracle.com/javase/7/docs/technotes/guides/jps/spec/JPSTOC.fm.html>
<http://docs.oracle.com/javase/7/docs/technotes/guides/jps/spec/appendix_printGIF.fm.html#7083>
Knute Johnson
2013-10-16 02:43:36 UTC
Permalink
Post by p***@gmail.com
Hi, every one
following are parameter set of my environment
/////////////////
OS: Win 7 64 bit
Java: java version "1.6.0_29" 32 bit
Test Printer: CutePDF Writer 2.8 、Microsoft XPS Document Writer
///////////
I try to print a image to printer, while the image file size is small the result is correct,
but while the file is large (jpeg format about 1.4 MB) the result is blank!!
I have assign -Xmx1024m to JVM.
following is the code to print, can any one give some suggestions
///////////////
public class TestPrinter2 {
public static void main(String[] args) throws Exception {
PrintService service = PrintServiceLookup.lookupDefaultPrintService();
PrintRequestAttributeSet set = new HashPrintRequestAttributeSet();
set.add(new Copies(1));
PrinterJob pj = PrinterJob.getPrinterJob();
if (pj.printDialog(set)) {
service = pj.getPrintService();
final BufferedImage img = ImageIO.read(new File("C:/TEMP/large.jpg"));
DocFlavor inFlavor = DocFlavor.SERVICE_FORMATTED.PRINTABLE;
Doc doc = new SimpleDoc(new Printable() {
public int print(Graphics graphics, PageFormat pageFormat, int pageIndex) throws PrinterException {
if (pageIndex > 0)
return Printable.NO_SUCH_PAGE;
Graphics2D g2d = (Graphics2D) graphics;
g2d.drawImage(img, (int)pageFormat.getImageableX(), (int)pageFormat.getImageableY(), (int)pageFormat.getWidth(), (int)pageFormat.getHeight(),null);
return Printable.PAGE_EXISTS;
}
}, inFlavor, null);
DocPrintJob job = service.createPrintJob();
job.print(doc, set);
}
}
}
//////////////////
thanks for reply
Try this code and see if you are still having the problem.

import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.awt.print.*;
import java.io.*;
import javax.imageio.*;

public class test implements Printable {
final BufferedImage image;

public test() throws IOException {
image = ImageIO.read(new File("kittens.jpg"));
}

public int print(Graphics g, PageFormat pf, int index) {
if (index != 0)
return Printable.NO_SUCH_PAGE;

g.drawImage(image,(int)pf.getImageableX(),
(int)pf.getImageableY(),null);
return Printable.PAGE_EXISTS;
}

public static void main(String[] args) {
try {
test t = new test();
PrinterJob pj = PrinterJob.getPrinterJob();
PageFormat pf = pj.defaultPage();
pj.setPrintable(t);
if (pj.printDialog())
try {
pj.print();
} catch (PrinterException pe) {
pe.printStackTrace();
}
} catch (IOException ioe) {
ioe.printStackTrace();
}
}
}
--
--

Knute Johnson
Loading...