📄 faxtest.java
字号:
// RFax
// Copyright (C)
//
// java4less@confluencia.net
// All rights reserved
import com.java4less.rfax.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class FaxTest extends Frame implements FaxStatusListener,IFaxReceiver {
Button btnEnd = new Button();
Label label1 = new Label();
Label label3 = new Label();
TextField faxNumber = new TextField();
TextField txtPort = new TextField();
TextArea txt = new TextArea();
Button btnSend = new Button();
Label lblStatus = new Label();
Choice cCLass = new Choice();
Label label4 = new Label();
Choice cDialing = new Choice();
Label label5 = new Label();
TextField txtInit = new TextField();
Label label6 = new Label();
boolean d=true;
Choice cFlow = new Choice();
Label label7 = new Label();
Choice cFlowControl = new Choice();
Label label8 = new Label();
JCheckBox chkHTML = new JCheckBox();
FaxModem m=null;
Panel panel1 = new Panel();
Button btnReceive = new Button();
Button btnStop = new Button();
Label label2 = new Label();
public FaxTest() {
try {
jbInit();
}
catch(Exception e) {
e.printStackTrace();
}
this.lblStatus.setForeground(java.awt.Color.blue);
this.txtInit.setText("ATV1Q0");
this.setBackground(java.awt.Color.lightGray);
cCLass.add("Class 1");
cCLass.add("Class 2");
cCLass.add("Class 2.0");
cDialing.add("Tone");
cDialing.add("Pulse");
cFlow.add("");
cFlow.add("-- RtsCts --");
cFlow.add("AT+FLO=2");
cFlow.add("AT&K3");
cFlow.add("AT&\\Q3");
cFlow.add("AT&F1&H1&R2&B1");
cFlow.add("-- XonXoff --");
cFlow.add("AT+FLO=1");
cFlow.add("AT&K4");
cFlow.add("AT&\\Q4");
cFlowControl.add("RtsCts");
cFlowControl.add("Xon/Xoff");
cFlowControl.add("none");
this.setSize(500,429);
this.setLocation((Toolkit.getDefaultToolkit().getScreenSize().width-getSize().width)/2,(Toolkit.getDefaultToolkit().getScreenSize().height-getSize().height)/2);
}
private void jbInit() throws Exception {
btnEnd.setLabel("Quit");
btnEnd.setBounds(new Rectangle(240, 339, 89, 27));
btnEnd.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnEnd_actionPerformed(e);
}
});
this.setLayout(null);
label1.setCursor(null);
label1.setText("Fax number");
label1.setBounds(new Rectangle(5, 152, 135, 17));
label3.setText("Port (COM1,COM2 ...)");
label3.setBounds(new Rectangle(5, 33, 134, 17));
faxNumber.setBounds(new Rectangle(144, 154, 190, 21));
txtPort.setText("COM1");
txtPort.setBounds(new Rectangle(144, 33, 97, 21));
txt.setText("Enter here text to send");
txt.setBounds(new Rectangle(5, 185, 489, 134));
btnSend.setActionCommand("SEND");
btnSend.setLabel("Send Fax");
btnSend.setBounds(new Rectangle(144, 339, 89, 27));
btnSend.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnSend_actionPerformed(e);
}
});
this.setTitle("RFax test");
lblStatus.setText("Inactive...");
lblStatus.setBounds(new Rectangle(5, 319, 327, 17));
cCLass.setBounds(new Rectangle(144, 59, 97, 21));
label4.setText("Modem class");
label4.setBounds(new Rectangle(5, 59, 123, 17));
cDialing.setBounds(new Rectangle(391, 36, 97, 21));
label5.setText("Dialing");
label5.setBounds(new Rectangle(252, 36, 97, 17));
txtInit.setBounds(new Rectangle(144, 130, 344, 21));
label6.setBounds(new Rectangle(5, 130, 134, 17));
label6.setText("Modem init. string");
cFlow.setBounds(new Rectangle(144, 107, 188, 21));
label7.setBounds(new Rectangle(5, 107, 133, 17));
label7.setText("Flow Control command");
cFlowControl.setBounds(new Rectangle(143, 84, 97, 21));
label8.setBounds(new Rectangle(5, 84, 97, 17));
label8.setText("Flow Control");
chkHTML.setText("HTML");
chkHTML.setToolTipText("");
chkHTML.setBackground(Color.lightGray);
chkHTML.setHorizontalAlignment(SwingConstants.RIGHT);
chkHTML.setBounds(new Rectangle(408, 322, 86, 25));
panel1.setBounds(new Rectangle(4, 371, 513, 54));
panel1.setLayout(null);
btnReceive.setLabel("Receive");
btnReceive.setBounds(new Rectangle(140, 18, 88, 27));
btnReceive.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnReceive_actionPerformed(e);
}
});
btnStop.setLabel("Stop");
btnStop.setBounds(new Rectangle(236, 18, 92, 27));
btnStop.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(ActionEvent e) {
btnStop_actionPerformed(e);
}
});
label2.setText("Fax reception:");
label2.setBounds(new Rectangle(3, 22, 105, 17));
this.add(txt, null);
this.add(btnEnd, null);
this.add(btnSend, null);
this.add(lblStatus, null);
this.add(chkHTML, null);
this.add(txtPort, null);
this.add(label3, null);
this.add(faxNumber, null);
this.add(label1, null);
this.add(txtInit, null);
this.add(label6, null);
this.add(label7, null);
this.add(cFlow, null);
this.add(cFlowControl, null);
this.add(label8, null);
this.add(cCLass, null);
this.add(label4, null);
this.add(cDialing, null);
this.add(label5, null);
this.add(panel1, null);
panel1.add(btnStop, null);
panel1.add(label2, null);
panel1.add(btnReceive, null);
}
/**
* send fax
*/
void btnSend_actionPerformed(ActionEvent e) {
btnReceive.setEnabled(false);
btnStop.setEnabled(false);
this.lblStatus.setForeground(java.awt.Color.blue);
String log="";
this.setCursor(Cursor.WAIT_CURSOR);
this.lblStatus.setText("Preparing...");
FaxProducer p=null;
if (this.chkHTML.isSelected()) {
p= new HtmlFaxProducer();
String[] pag=new String[1];
pag[0]=this.txt.getText();
((HtmlFaxProducer) p).text=pag;
((HtmlFaxProducer) p).pageImage=this.createImage(800,550);
}
else {
p= new TextFaxProducer();
((TextFaxProducer) p).text=this.txt.getText();
//p.linesPage=3;
((TextFaxProducer) p).pageImage=this.createImage(800,550);
((TextFaxProducer) p).prepare();
}
m=new FaxModem();
// scale and center image
m.getEncoder().scaleFactor=2;
m.getEncoder().centerImage=true;
//m.getEncoder().completePage=true;
// modem configuration
m.directBitOrder=true;
m.log=true;
m.debug=d;
m.faxClass=1;
m.resolution=m.RESOLUTION_NORMAL;
m.timeout=60;
if (cCLass.getSelectedIndex()==1) m.faxClass=2;
if (cCLass.getSelectedIndex()==2) m.faxClass=20;
m.dialTone=true;
if (cDialing.getSelectedIndex()==1) m.dialTone=false;
String tmps=cFlow.getSelectedItem();
if (tmps.indexOf("--")>=0) cFlow.select(0);
// rts cts
if (cFlowControl.getSelectedIndex()==0) {
m.flowControl=m.FLOWCONTROL_RTSCTS;
m.ATFlowControlRTSCTS=""; // no command needed
if (cFlow.getSelectedIndex()>0) {
m.ATFlowControlRTSCTS=cFlow.getSelectedItem();
}
}
// xon /xoff
if (cFlowControl.getSelectedIndex()==1) {
m.flowControl=m.FLOWCONTROL_XONXOFF;
m.ATFlowControlXONXOFF=""; // no command needed
if (cFlow.getSelectedIndex()>0) {
m.ATFlowControlXONXOFF=cFlow.getSelectedItem();
}
}
// no flow control
if (cFlowControl.getSelectedIndex()==2) {
m.flowControl=m.FLOWCONTROL_NONE;
m.ATFlowControlNone=""; // no command needed
if (cFlow.getSelectedIndex()>0) {
m.ATFlowControlNone=cFlow.getSelectedItem();
}
}
m.setPortName(this.txtPort.getText());
m.AtFBOR=true;
if (txtInit.getText().length()>0) m.setInitString(txtInit.getText());
// status listener
m.listener=this;
boolean success=false;
// open port and send
if (m.open(p))
//m.createFaxFiles(p);
//success=true;
//this.lblStatus.setText("Fax successfully sent.");
if (m.sendFax(this.faxNumber.getText())) {
success=true;
this.lblStatus.setText("Fax successfully sent.");
}
// set error text
if (!success) {
this.lblStatus.setForeground(java.awt.Color.red);
this.lblStatus.setText("Error: " + m.lastError);
if (m.hangCode>0) this.lblStatus.setText("Error: " + m.lastError + "(hang up code " + m.hangCode + ")");
}
m.close();
this.setCursor(Cursor.getDefaultCursor());
// save log debug information
try {
java.io.FileOutputStream fo=new java.io.FileOutputStream("log.txt");
fo.write(m.logStr.getBytes());
fo.close();
} catch (Exception e1) {
}
btnReceive.setEnabled(true);
btnStop.setEnabled(true);
}
/**
* exit program
*/
void btnEnd_actionPerformed(ActionEvent e) {
System.exit(0);
}
/**
* Entry point
*/
public static void main(String[] args) {
/* Capabilities cap= new Capabilities();
byte[] b= new byte[3];
b[0]=0;
b[1]=0x46;
b[2]=(byte) 0xB8;
cap.decodeCapabilities(b);
b= cap.encodeCapabilities();*/
FaxTest f=new FaxTest();
if (args.length>0)
if (args[0].compareTo("-d")==0) f.d=true;
f.setVisible(true);
}
/**
* receive transmision status
*/
public void faxProgress(int status,int page) {
if (status==FaxStatusListener.ST_CLOSE) this.lblStatus.setText("Closing...");
if (status==FaxStatusListener.ST_CONNECTING) this.lblStatus.setText("Connecting...");
if (status==FaxStatusListener.ST_CONVERT_FIlES) this.lblStatus.setText("Converting fax files...");
if (status==FaxStatusListener.ST_INIT_MODEM) this.lblStatus.setText("Initializing modem...");
if (status==FaxStatusListener.ST_OPEN_PORT) this.lblStatus.setText("Opening port...");
if (status==FaxStatusListener.ST_SEND_PAGE) this.lblStatus.setText("Sending page " + (int) (page +1));
if (status==FaxStatusListener.ST_REC_PAGE) this.lblStatus.setText("Receiving page " + (int) (page +1));
if (status==FaxStatusListener.ST_REC_CALL) this.lblStatus.setText("Call detected...");
if (status==FaxStatusListener.ST_WAIT_CALL) this.lblStatus.setText("Waiting for call...");
this.paintAll(this.getGraphics());
}
void btnReceive_actionPerformed(ActionEvent e) {
btnEnd.setEnabled(false);
btnSend.setEnabled(false);
this.lblStatus.setForeground(java.awt.Color.blue);
m=new FaxReceiverModem();
m.faxClass=1;
m.AtFBOR=true;
// modem configuration
m.log=true;
m.debug=d;
m.faxClass=1;
m.resolution=m.RESOLUTION_NORMAL;
m.timeout=60;
String tmps=cFlow.getSelectedItem();
if (tmps.indexOf("--")>=0) cFlow.select(0);
// rts cts
if (cFlowControl.getSelectedIndex()==0) {
m.flowControl=m.FLOWCONTROL_RTSCTS;
m.ATFlowControlRTSCTS=""; // no command needed
if (cFlow.getSelectedIndex()>0) {
m.ATFlowControlRTSCTS=cFlow.getSelectedItem();
}
}
// xon /xoff
if (cFlowControl.getSelectedIndex()==1) {
m.flowControl=m.FLOWCONTROL_XONXOFF;
m.ATFlowControlXONXOFF=""; // no command needed
if (cFlow.getSelectedIndex()>0) {
m.ATFlowControlXONXOFF=cFlow.getSelectedItem();
}
}
// no flow control
if (cFlowControl.getSelectedIndex()==2) {
m.flowControl=m.FLOWCONTROL_NONE;
m.ATFlowControlNone=""; // no command needed
if (cFlow.getSelectedIndex()>0) {
m.ATFlowControlNone=cFlow.getSelectedItem();
}
}
m.setPortName(this.txtPort.getText());
m.AtFBOR=true;
if (txtInit.getText().length()>0) m.setInitString(txtInit.getText());
// status listener
m.listener=this;
boolean success=false;
((FaxReceiverModem) m).faxReceiver=this;
if (((FaxReceiverModem) m).openReception()) ((FaxReceiverModem) m).waitForFax();
}
/**
* stop waiting for wait.
*/
void btnStop_actionPerformed(ActionEvent e) {
if (m!=null) {
m.close();
// save log debug information
try {
java.io.FileOutputStream fo=new java.io.FileOutputStream("logReception.txt");
fo.write(m.logStr.getBytes());
fo.close();
} catch (Exception e1) {
e1.printStackTrace();
}
}
this.lblStatus.setForeground(java.awt.Color.blue);
lblStatus.setText("Inactive...");
btnSend.setEnabled(true);
btnEnd.setEnabled(true);
}
/**
* Fax Receiver interface
*/
public void faxReceived(String callerId, boolean success,String file,int pages) {
System.out.println("");
System.out.println("Fax from " +callerId);
System.out.println("Success " +success);
System.out.println("File " +file);
System.out.println("");
System.out.println("");
this.txt.setText("");
this.txt.setText("Fax received from: " +callerId+"\n File: "+file+"\n Success: "+ success);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -