我必须在同一个包中使用java文件:
* Pdf2Dcm.java:
package dicomizer;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.Enumeration;
import java.util.List;
import java.util.Properties;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.GnuParser;
import org.apache.commons.cli.HelpFormatter;
import org.apache.commons.cli.OptionBuilder;
import org.apache.commons.cli.Options;
import org.apache.commons.cli.ParseException;
import org.dcm4che2.data.BasicDicomObject;
import org.dcm4che2.data.DicomObject;
import org.dcm4che2.data.Tag;
import org.dcm4che2.data.UID;
import org.dcm4che2.data.VR;
import org.dcm4che2.io.DicomOutputStream;
import org.dcm4che2.util.UIDUtils;
/**
* @author gunter zeilinger(gunterze@gmail.com)
* @version $Revision: 12456 $ $Date: 2009-11-26 13:51:53 +0100 (Thu, 26 Nov 2009) $
* @since Apr 1, 2006
*
*/
public class Pdf2Dcm extends Interface {
private static final String USAGE =
"pdf2dcm [Options] <pdffile> <dcmfile>";
private static final String DESCRIPTION =
"Encapsulate PDF Document into DICOM Object.\nOptions:";
private static final String EXAMPLE =
"pdf2dcm -c pdf2dcm.cfg report.pdf report.dcm\n" +
"=> Encapulate PDF Document report.pdf into DICOM Object stored to " +
"report.dcm using DICOM Attribute values specified in Configuration " +
"file pdf2dcm.cfg.";
private String transferSyntax = UID.ExplicitVRLittleEndian;
private String charset = "ISO_IR 100";
private int bufferSize = 8192;
private Properties cfg = new Properties();
public Pdf2Dcm() {
try {
cfg.load(Pdf2Dcm.class.getResourceAsStream("pdf2dcm.cfg"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}
/* public Pdf2Dcm(String filename,String path) {
this.filename = filename;
this.path = path;
try {
cfg.load(Pdf2Dcm.class.getResourceAsStream("pdf2dcm.cfg"));
} catch (Exception e) {
throw new RuntimeException(e);
}
}*/
public final void setCharset(String charset) {
this.charset = charset;
}
public final void setBufferSize(int bufferSize) {
if (bufferSize < 64) {
throw new IllegalArgumentException("bufferSize: " + bufferSize);
}
this.bufferSize = bufferSize;
}
public final void setTransferSyntax(String transferSyntax) {
this.transferSyntax = transferSyntax;
}
private void loadConfiguration(File cfgFile) throws IOException {
Properties tmp = new Properties(cfg);
InputStream in = new BufferedInputStream(new FileInputStream(cfgFile));
try {
tmp.load(in);
} finally {
in.close();
}
cfg = tmp;
}
public void convert(File pdfFile, File dcmFile) throws IOException {
DicomObject attrs = new BasicDicomObject();
attrs.putString(Tag.SpecificCharacterSet, VR.CS, charset);
attrs.putSequence(Tag.ConceptNameCodeSequence);
for (Enumeration en = cfg.propertyNames(); en.hasMoreElements();) {
String key = (String) en.nextElement();
attrs.putString(Tag.toTagPath(key), null, cfg.getProperty(key));
}
ensureUID(attrs, Tag.StudyInstanceUID);
ensureUID(attrs, Tag.SeriesInstanceUID);
ensureUID(attrs, Tag.SOPInstanceUID);
Date now = new Date();
attrs.putDate(Tag.InstanceCreationDate, VR.DA, now);
attrs.putDate(Tag.InstanceCreationTime, VR.TM, now);
attrs.initFileMetaInformation(transferSyntax);
FileInputStream pdfInput = new FileInputStream(pdfFile);
FileOutputStream fos = new FileOutputStream(dcmFile);
BufferedOutputStream bos = new BufferedOutputStream(fos);
DicomOutputStream dos = new DicomOutputStream(bos);
try {
dos.writeFileMetaInformation(attrs);
dos.writeDataset(attrs.subSet(Tag.SpecificCharacterSet,
Tag.EncapsulatedDocument), transferSyntax);
int pdfLen = (int) pdfFile.length();
dos.writeHeader(Tag.EncapsulatedDocument, VR.OB, (pdfLen+1)&~1);
byte[] b = new byte[bufferSize];
int r;
while ((r = pdfInput.read(b)) > 0) {
dos.write(b, 0, r);
}
if ((pdfLen&1) != 0) {
dos.write(0);
}
dos.writeDataset(attrs.subSet(Tag.EncapsulatedDocument, -1),
transferSyntax);
} finally {
dos.close();
pdfInput.close();
}
}
private void ensureUID(DicomObject attrs, int tag) {
if (!attrs.containsValue(tag)) {
attrs.putString(tag, VR.UI, UIDUtils.createUID());
}
}
public static void main(String[] args) {
try {
//CommandLine cl = parse(args);
Pdf2Dcm pdf2Dcm = new Pdf2Dcm();
//if (cl.hasOption("ivrle")) {
// pdf2Dcm.setTransferSyntax(UID.ImplicitVRLittleEndian);
//}
//if (cl.hasOption("cs")) {
// pdf2Dcm.setCharset(cl.getOptionValue("cs"));
//}
//if (cl.hasOption("bs")) {
// pdf2Dcm.setBufferSize(Integer.parseInt(cl.getOptionValue("bs")));
//}
//if (cl.hasOption("c")) {
// pdf2Dcm.loadConfiguration(new File(cl.getOptionValue("c")));
//}
//if (cl.hasOption("uid")) {
// UIDUtils.setRoot(cl.getOptionValue("uid"));
//}
//List argList = cl.getArgList();
//File pdfFile = new File((String) argList.get(0));
File pdfFile = new File(filename);
//File pdfFile = new File("C:\\Users\\Dell\\Desktop\\input\\demande.docx");
//File dcmFile = new File((String) argList.get(1));
File dcmFile = new File(path);
// File dcmFile = new File("C:\\Users\\Dell\\Desktop\\output\\demande.dcm");
// System.out.println(filename);
//System.out.println(path);
long start = System.currentTimeMillis();
pdf2Dcm.convert(pdfFile, dcmFile);
long fin = System.currentTimeMillis();
System.out.println("Encapsulated " + pdfFile + " to " + dcmFile
+ " in " + (fin - start) + "ms.");
} catch (IOException e) {
e.printStackTrace();
}
}
private static CommandLine parse(String[] args) {
Options opts = new Options();
OptionBuilder.withArgName("charset");
OptionBuilder.hasArg();
OptionBuilder.withDescription(
"Specific Character Set, ISO_IR 100 by default");
opts.addOption(OptionBuilder.create("cs"));
OptionBuilder.withArgName("size");
OptionBuilder.hasArg();
OptionBuilder.withDescription(
"Buffer size used for copying PDF to DICOM file, 8192 by default");
opts.addOption(OptionBuilder.create("bs"));
OptionBuilder.withArgName("file");
OptionBuilder.hasArg();
OptionBuilder.withDescription(
"Configuration file specifying DICOM attribute values");
opts.addOption(OptionBuilder.create("c"));
opts.addOption("ivrle", false, "use Implicit VR Little Endian instead " +
"Explicit VR Little Endian Transfer Syntax for DICOM encoding.");
opts.addOption("h", "help", false, "print this message");
OptionBuilder.withArgName("prefix");
OptionBuilder.hasArg();
OptionBuilder.withDescription("Generate UIDs with given prefix," +
"1.2.40.0.13.1.<host-ip> by default.");
opts.addOption(OptionBuilder.create("uid"));
opts.addOption("V", "version", false,
"print the version information and exit");
CommandLine cl = null;
try {
cl = new GnuParser().parse(opts, args);
} catch (ParseException e) {
exit("pdf2dcm: " + e.getMessage());
throw new RuntimeException("unreachable");
}
if (cl.hasOption('V')) {
Package p = Pdf2Dcm.class.getPackage();
System.out.println("pdf2dcm v" + p.getImplementationVersion());
System.exit(0);
}
if (cl.hasOption('h') || cl.getArgList().size() != 2) {
HelpFormatter formatter = new HelpFormatter();
formatter.printHelp(USAGE, DESCRIPTION, opts, EXAMPLE);
System.exit(0);
}
return cl;
}
private static void exit(String msg) {
System.err.println(msg);
System.err.println("Try 'pdf2dcm -h' for more information.");
System.exit(1);
}
void convert(String filename, String path) {
throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
}
}
* Interface.java:
package dicomizer;
import java.io.File;
import javax.swing.JFileChooser;
/**
*
* @author Dell
*/
public class Interface extends javax.swing.JFrame{
public static String path;
//String[] file;
public static String filename;
String extension;
String aetitle;
String hostName;
int portNumber;
/**
* Creates new form Interface
*/
public Interface() {
initComponents();
}
/**
* This method is called from within the constructor to initialize the form.
* WARNING: Do NOT modify this code. The content of this method is always
* regenerated by the Form Editor.
*/
@SuppressWarnings("unchecked")
// <editor-fold defaultstate="collapsed" desc="Generated Code">
private void initComponents() {
jTabbedPane1 = new javax.swing.JTabbedPane();
jPanel2 = new javax.swing.JPanel();
jLabel1 = new javax.swing.JLabel();
jLabel2 = new javax.swing.JLabel();
jLabel3 = new javax.swing.JLabel();
aet = new javax.swing.JTextField();
host = new javax.swing.JTextField();
port = new javax.swing.JTextField();
echo = new javax.swing.JButton();
sav = new javax.swing.JButton();
jScrollPane1 = new javax.swing.JScrollPane();
jTextArea1 = new javax.swing.JTextArea();
jPanel1 = new javax.swing.JPanel();
jTextField4 = new javax.swing.JTextField();
jTextField5 = new javax.swing.JTextField();
ConvertToDicomFile = new javax.swing.JButton();
jLabel4 = new javax.swing.JLabel();
jLabel5 = new javax.swing.JLabel();
browse = new javax.swing.JButton();
save = new javax.swing.JButton();
setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
setTitle("DICOMIZER");
jLabel1.setText("AET :");
jLabel2.setText("HOST :");
jLabel3.setText("PORT :");
aet.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
aetActionPerformed(evt);
}
});
host.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
hostActionPerformed(evt);
}
});
port.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
portActionPerformed(evt);
}
});
echo.setText("ECHO");
sav.setText("SAVE");
jTextArea1.setColumns(20);
jTextArea1.setRows(5);
jScrollPane1.setViewportView(jTextArea1);
javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(jPanel2);
jPanel2.setLayout(jPanel2Layout);
jPanel2Layout.setHorizontalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(27, 27, 27)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 321, Short.MAX_VALUE)
.addGap(45, 45, 45))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jLabel1)
.addComponent(jLabel2)
.addComponent(jLabel3))
.addGap(18, 18, 18)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(port, javax.swing.GroupLayout.DEFAULT_SIZE, 243, Short.MAX_VALUE)
.addComponent(host)
.addComponent(aet))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(14, 14, 14)
.addComponent(echo)
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(sav)
.addGap(61, 61, 61))))
);
jPanel2Layout.setVerticalGroup(
jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel2Layout.createSequentialGroup()
.addGap(42, 42, 42)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel1)
.addComponent(aet, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(29, 29, 29)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel2)
.addComponent(host, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGap(35, 35, 35)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel3)
.addComponent(port, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
.addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 38, Short.MAX_VALUE)
.addGroup(jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(sav)
.addComponent(echo))
.addGap(35, 35, 35)
.addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 118, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGap(19, 19, 19))
);
jTabbedPane1.addTab("Configuration", jPanel2);
ConvertToDicomFile.setText("Convert To DICOM File");
ConvertToDicomFile.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
ConvertToDicomFileActionPerformed(evt);
}
});
jLabel4.setText("file to convert");
jLabel5.setText("destination folder");
browse.setText("Browse");
browse.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
browseActionPerformed(evt);
}
});
save.setText("Save");
save.addActionListener(new java.awt.event.ActionListener() {
public void actionPerformed(java.awt.event.ActionEvent evt) {
saveActionPerformed(evt);
}
});
javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
jPanel1.setLayout(jPanel1Layout);
jPanel1Layout.setHorizontalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addContainerGap()
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
.addComponent(ConvertToDicomFile, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel5)
.addGap(18, 18, 18)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, 186, javax.swing.GroupLayout.PREFERRED_SIZE))
.addGroup(jPanel1Layout.createSequentialGroup()
.addComponent(jLabel4)
.addGap(31, 31, 31)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, 190, javax.swing.GroupLayout.PREFERRED_SIZE))))
.addGap(18, 18, 18)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
.addComponent(browse, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
.addComponent(save, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
.addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
);
jPanel1Layout.setVerticalGroup(
jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(jPanel1Layout.createSequentialGroup()
.addGap(45, 45, 45)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel4)
.addComponent(jTextField4, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(browse))
.addGap(24, 24, 24)
.addComponent(ConvertToDicomFile)
.addGap(37, 37, 37)
.addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
.addComponent(jLabel5)
.addComponent(jTextField5, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
.addComponent(save))
.addContainerGap(224, Short.MAX_VALUE))
);
jTabbedPane1.addTab("Dicomizer", jPanel1);
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
getContentPane().setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
.addContainerGap()
.addComponent(jTabbedPane1)
.addContainerGap())
);
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
.addComponent(jTabbedPane1, javax.swing.GroupLayout.Alignment.TRAILING)
);
pack();
}// </editor-fold>
private void browseActionPerformed(java.awt.event.ActionEvent evt) {
JFileChooser chooser = new JFileChooser();
chooser.showOpenDialog(null);
File f =chooser.getSelectedFile();
filename = f.getAbsolutePath();
jTextField4.setText(filename);
int index = filename.lastIndexOf(".");
extension = filename.substring(index+1,filename.length());
//file = filename.;
//System.out.println(extension);
//System.out.println(filename);
}
private void ConvertToDicomFileActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
if (extension.equals("xlsx") || extension.equals("pdf") || extension.equals("docx"))
{
**// here I want to to call the file Pdf2Dcm.java**
}
else if (extension.equals("png") || extension.equals("jpg"))
{
}
else
{
System.out.println("Format not supported");
}
}
private void saveActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
JFileChooser chooser = new JFileChooser();
chooser.showSaveDialog(null);
File f = chooser.getCurrentDirectory();
path =f.getAbsolutePath();
int index= filename.lastIndexOf("'\'");
path=path+filename.substring(index+1, filename.lastIndexOf("."))+".dcm";
jTextField5.setText(path);
//System.out.println(path);
}
private void aetActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//aetitle = aet.getAETiltle();
}
private void hostActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//hostName = host.getHostname();
}
private void portActionPerformed(java.awt.event.ActionEvent evt) {
// TODO add your handling code here:
//portNumber = port.getPort();
}
/**
* @param args the command line arguments
*/
public static void main(String args[]) {
/* Set the Nimbus look and feel */
//<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
/* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
* For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html
*/
try {
for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
if ("Nimbus".equals(info.getName())) {
javax.swing.UIManager.setLookAndFeel(info.getClassName());
break;
}
}
} catch (ClassNotFoundException ex) {
java.util.logging.Logger.getLogger(Interface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (InstantiationException ex) {
java.util.logging.Logger.getLogger(Interface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (IllegalAccessException ex) {
java.util.logging.Logger.getLogger(Interface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
} catch (javax.swing.UnsupportedLookAndFeelException ex) {
java.util.logging.Logger.getLogger(Interface.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
}
//</editor-fold>
/* Create and display the form */
java.awt.EventQueue.invokeLater(new Runnable() {
@Override
public void run() {
new Interface().setVisible(true);
}
});
}
// Variables declaration - do not modify
private javax.swing.JButton ConvertToDicomFile;
private javax.swing.JTextField aet;
private javax.swing.JButton browse;
private javax.swing.JButton echo;
private javax.swing.JTextField host;
private javax.swing.JLabel jLabel1;
private javax.swing.JLabel jLabel2;
private javax.swing.JLabel jLabel3;
private javax.swing.JLabel jLabel4;
private javax.swing.JLabel jLabel5;
private javax.swing.JPanel jPanel1;
private javax.swing.JPanel jPanel2;
private javax.swing.JScrollPane jScrollPane1;
private javax.swing.JTabbedPane jTabbedPane1;
private javax.swing.JTextArea jTextArea1;
private javax.swing.JTextField jTextField4;
private javax.swing.JTextField jTextField5;
private javax.swing.JTextField port;
private javax.swing.JButton sav;
private javax.swing.JButton save;
// End of variables declaration
}
在&#34; private void ConvertToDicomFileActionPerformed&#34; Interface.java 我想调用文件Pdf2Dcm.java来运行。 我该如何调用&#34;私有void ConvertToDicomFileActionPerformed&#34;中的Pdf2Dcm.java? Interface.java?
答案 0 :(得分:1)
在private void ConvertToDicomFileActionPerformed
拨打电话:
Pdf2Dcm.main(args);