I need to print on a printer an image and some data, in java (Swing) but all in vain. I am just able to do up with the data but not with the image. I have a abc.png file and 6 jTextBoxes, from which the value has to be printed on the printer. I am using the FileWriter and PrinterJob classes to implement the job. The data and the image can be printed separately but not together. Can somebody suggest me something.
我需要在打印机上打印一个图像和一些数据,在java(Swing)中,但都是徒劳的。我只能处理数据,但不能处理图像。我有一个abc.png文件和6个jTextBox,从中必须在打印机上打印该值。我正在使用FileWriter和PrinterJob类来实现该作业。数据和图像可以单独打印,但不能一起打印。有人可以给我一些建议。
Thanks.
谢谢。
Code for printing image:
打印图像代码:
try {
PrintRequestAttributeSet pras = new HashPrintRequestAttributeSet();
pras.add(new Copies(1));
PrintService pss[] = PrintServiceLookup.lookupPrintServices(DocFlavor.INPUT_STREAM.GIF, pras);
if (pss.length == 0)
throw new RuntimeException("No printer services available.");
PrintService ps = pss[0];
System.out.println("Printing to " + ps);
DocPrintJob job = ps.createPrintJob();
FileInputStream fin = new FileInputStream("C://a.gif");
Doc doc = new SimpleDoc(fin, DocFlavor.INPUT_STREAM.GIF, null);
//Doc doc1=new SimpleDoc();
job.print(doc, pras);
//fin.close();
} catch (IOException ie) {
ie.printStackTrace();
} catch (PrintException pe) {
pe.printStackTrace();
}
Code for printing text:
打印文字代码:
Just pass the location of the file on the hard disk, in FileInputStream.
只需在FileInputStream中传递硬盘上文件的位置即可。
1 个解决方案
#1
1
One solution could be to create a second image that contains your original image and the text.
一种解决方案是创建包含原始图像和文本的第二个图像。
Something like this may work? (un-tested):
这样的东西可能有用吗? (未测试):
public Image addTextToImage(BufferedImage i, String[] text) {
final int VERTICLE_PADDING_PIXELS = 5;
final int LEFT_MARGIN_PIXELS = 5;
FontMetrics fm = i.createGraphics().getFontMetrics();
int width = i.getWidth();
int height = i.getHeight()
+ (text.length * (fm.getHeight() + VERTICLE_PADDING_PIXELS));
for (String s : text) {
width = Math.max(width, fm.stringWidth(s) + LEFT_MARGIN_PIXELS);
}
BufferedImage result = new BufferedImage(i.getHeight(), width, height);
Graphics2D g = result.createGraphics();
g.drawImage(i, 0, 0, null);
for (int x = 0; x < text.length; x++) {
g.drawString(text[x], LEFT_MARGIN_PIXELS, i.getHeight() + (x + 1) *VERTICLE_PADDING_PIXELS + x*fm.getHeight());
}
return result;
}
#1
1
One solution could be to create a second image that contains your original image and the text.
一种解决方案是创建包含原始图像和文本的第二个图像。
Something like this may work? (un-tested):
这样的东西可能有用吗? (未测试):
public Image addTextToImage(BufferedImage i, String[] text) {
final int VERTICLE_PADDING_PIXELS = 5;
final int LEFT_MARGIN_PIXELS = 5;
FontMetrics fm = i.createGraphics().getFontMetrics();
int width = i.getWidth();
int height = i.getHeight()
+ (text.length * (fm.getHeight() + VERTICLE_PADDING_PIXELS));
for (String s : text) {
width = Math.max(width, fm.stringWidth(s) + LEFT_MARGIN_PIXELS);
}
BufferedImage result = new BufferedImage(i.getHeight(), width, height);
Graphics2D g = result.createGraphics();
g.drawImage(i, 0, 0, null);
for (int x = 0; x < text.length; x++) {
g.drawString(text[x], LEFT_MARGIN_PIXELS, i.getHeight() + (x + 1) *VERTICLE_PADDING_PIXELS + x*fm.getHeight());
}
return result;
}