我在我的java页面上放了两个按钮来打印和发送电子邮件。我已经解决了电子邮件要求
if (e.getSource().equals(btnEmail)){
dialog.setVisible(true); // to visible the dialog
try {
Robot robot = new Robot();
String format = "jpg";
final String fileName = "C:\\xxxxxxxx--" + format;
Rectangle screenRect = new Rectangle(Toolkit.getDefaultToolkit().getScreenSize());
BufferedImage screenFullImage = robot.createScreenCapture(screenRect);
File file = new File(fileName);
ImageIO.write(screenFullImage, format, file);
while (true){
boolean exists = file.exists();
if (!exists){ // It returns false if File or directory does not exist
return;
} else {
String to = "xxxx@hotmail.com";
String username = "xxxx@gmail.com";
final String password = "xxxx";
//Get the session object
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);//change accordingly
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject("Printout from Java Exe");
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("Printout");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// Part two is attachment
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(file);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(file.getName());
multipart.addBodyPart(messageBodyPart);
// Send the complete message parts
message.setContent(multipart);
// Send message
Transport.send(message);
System.out.println("Sent message successfully....");
} catch (MessagingException e ?????1) {
throw new RuntimeException(e1);
}
break;
}
}
} catch (AWTException | IOException ex) {
System.err.println(ex);
}
dialog.setVisible(false); // set visibility to false
} else if (e.getSource().equals(btnPrint)){
// This is where I'm stuck - Should be easy - just output a JPEG from disk to Printer ?????
}
这就是我被困的地方 - 应该很容易 - 只需将JPEG从磁盘输出到打印机?????
答案 0 :(得分:0)
我已经使用了这个程序,它调用了对话框 - 我没有打印机,所以试着看看它是否实际打印 - 用bim代替screenFullImage:
class HelloWorldPrinter implements Printable, ActionListener {
public int print(Graphics g, PageFormat pf, int page) throws
PrinterException {
if (page > 0) { /* We have only one page, and 'page' is zero-based */
return NO_SUCH_PAGE;
}
/* User (0,0) is typically outside the imageable area, so we must
* translate by the X and Y values in the PageFormat to avoid clipping
*/
Graphics2D g2d = (Graphics2D)g;
g2d.translate(pf.getImageableX(), pf.getImageableY());
/* Now we perform our rendering */
// g.drawString("Hello world!", 100, 100);
BufferedImage bim=null;
try {
bim=ImageIO.read(new File("../black.jpg"));
}
catch (Exception ex) { System.err.println("error in bim "+ex); }
g.drawImage(bim, 0, 0, null);
/* tell the caller that this page is part of the printed document */
return PAGE_EXISTS;
}
public void actionPerformed(ActionEvent e) {
PrinterJob job = PrinterJob.getPrinterJob();
job.setPrintable(this);
boolean ok = job.printDialog();
if (ok) {
try {
job.print();
} catch (PrinterException ex) {
/* The job did not successfully complete */
}
}
}
public static void m() {
UIManager.put("swing.boldMetal", Boolean.FALSE);
JFrame f = new JFrame("Hello World Printer");
f.addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {System.exit(0);}
});
JButton printButton = new JButton("Print Hello World");
printButton.addActionListener(new HelloWorldPrinter());
f.add("Center", printButton);
f.pack();
f.setVisible(true);
}
}
-
更改
public static void m() {
到
public static void main(String[] args) {
和
BufferedImage bim=null;
try {
bim=ImageIO.read(new File("...")); // fill in whatever name the file is saved in the whole path e.g. c:/mypath/filename
}
catch (Exception ex) { System.err.println("error in bim "+ex); }
g.drawImage(bim, 0, 0, null);
要打印较小尺寸的图像,请尝试使用
BufferedImage b0=new BufferedImage(bim.getWidth()/2, bim.getHeight()/2, BufferedImage.TYPE_INT_RGB);
Graphics g0=b0.getGraphics();
g0.drawImage(bim, b0.getWidth(), b0.getHeight(), null);
现在打印此图片而不是bim:
g.drawImage(b0, 0, 0, null);