我正在开发应用程序以在 netbeans 平台中加载 Dicom 图像..我开发了应用程序,但它给了我空指针异常..所以是否缺少任何 jsr api 或插件,我没有找到我错的地方..我添加了源代码请在这里分享你的想法
package org.netbeans.GuiImage;
import com.sun.media.jai.widget.DisplayJAI;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.image.BufferedImage;
import java.awt.image.RenderedImage;
import java.awt.image.renderable.ParameterBlock;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.util.Iterator;
import javax.imageio.*;
import javax.imageio.stream.FileImageInputStream;
import javax.imageio.stream.FileImageOutputStream;
import javax.media.jai.JAI;
import javax.media.jai.PlanarImage;
import javax.media.jai.RenderedOp;
import javax.swing.*;
import org.netbeans.api.settings.ConvertAsProperties;
import org.openide.awt.ActionID;
import org.openide.awt.ActionReference;
import org.openide.util.NbBundle;
import org.openide.windows.TopComponent;
import javax.swing.filechooser.FileFilter;
import javax.swing.filechooser.FileNameExtensionFilter;
@ConvertAsProperties(dtd = "-//org.netbeans.guiimage//run//EN",autostore = true)
@TopComponent.Description(preferredID = "run",
persistenceType = TopComponent.PERSISTENCE_ALWAYS)
@TopComponent.Registration(mode = "editor", openAtStartup = true)
@ActionID(category = "Window", id = "org.netbeans.guiimage.run")
@ActionReference(path = "Menu/Window" /*
* , position = 333
*/)
@TopComponent.OpenActionRegistration(displayName = "#CTL_runAction",
preferredID = "run")
public final class run extends TopComponent implements ActionListener{
private JPanel jPanel1 = new JPanel();
private JLabel jLabel1 = new JLabel();
private JTabbedPane jtp=new JTabbedPane();
private ImageIcon img = new ImageIcon("E:/tif/v1.TIF");
private JFrame jFrame1 = new JFrame();
private JButton jButton1 = new JButton("open");
private JButton jButton2 = new JButton("save");
private JFileChooser jfc = new JFileChooser();
private File filePath = new File("E:/Images/Image1.png");
private String filename;
private BufferedImage bi=null;
private FileInputStream fis;
private FileOutputStream fos;
private FileImageOutputStream fios;
int retVal;
private RenderedImage image=null;
private ImageReader reader;
private FileImageInputStream fiis;
private Iterator iter , witer;
private RenderedOp result=null;
private ImageWriter writer;
private DisplayJAI djai;
private JScrollPane jsp;
// private DicomFileDialog dicomFileDialog;
//private FileImageInputstream fiis = new FileImageInputStream(filePath);
FileNameExtensionFilter filter = new FileNameExtensionFilter(
"png,dcm Images", "dcm", "png");
public run(){
initComponents();
setName(NbBundle.getMessage(run.class,"CTL_run"));
setToolTipText(NbBundle.getMessage(run.class, "HINT_run"));
}
private void initComponents() {
jsp.add(jButton1);
jsp.add(jButton2);
jButton1.addActionListener(this);
jsp.setPreferredSize(new Dimension(300, 300));
javax.swing.GroupLayout jFrame1Layout = new javax.swing.GroupLayout(jFrame1.getContentPane());
jFrame1.getContentPane().setLayout(jFrame1Layout);
jFrame1Layout.setHorizontalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 400, Short.MAX_VALUE));
jFrame1Layout.setVerticalGroup(
jFrame1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addGap(0, 300, Short.MAX_VALUE));
javax.swing.GroupLayout layout = new javax.swing.GroupLayout(this);
this.setLayout(layout);
layout.setHorizontalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jsp, javax.swing.GroupLayout.DEFAULT_SIZE, 400, Short.MAX_VALUE));
layout.setVerticalGroup(
layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING).addComponent(jsp, javax.swing.GroupLayout.DEFAULT_SIZE, 300, Short.MAX_VALUE));
}
@Override
public void componentOpened() {
}
@Override
public void componentClosed() {
}
void readProperties(java.util.Properties p) {
// do the read
}
void writeProperties(java.util.Properties p) {
// handle the store
}
@Override
public void actionPerformed(ActionEvent e) {
if(e.getSource() == jButton1){
//Add a custom file filter and disable the default
//(Accept All) file filter.
jfc.addChoosableFileFilter(new FileFilter() {
@Override
public boolean accept(File filePath) {
if (filePath.isDirectory()) {
return (true);
}
String extension = Utils.getExtension(filePath);
if (extension != null) {
if (extension.equals(Utils.dicom)
|| extension.equals(Utils.dcm)
|| extension.equals(Utils.dic)
|| extension.equals(Utils.tif)) {
return true;
} else {
return false;
}
}
return false;
}
@Override
public String getDescription() {
return "*.dcm,"
+ "*.dicom,"
+ "*.TIF,"
+"*.tif,"
+ "*.dic";
}
});
jfc.setAcceptAllFileFilterUsed(false);
int retVal = jfc.showOpenDialog(this);
System.out.println(e.getActionCommand());
if(retVal == JFileChooser.APPROVE_OPTION){
filePath = jfc.getSelectedFile();
filename = filePath.getName();
System.out.println("Selected file:");
System.out.println(filename);
try {
fiis = new FileImageInputStream(new File(filename));
iter = ImageIO.getImageReaders(fiis);
reader = (ImageReader) iter.next();
reader.setInput(fiis);
image = reader.readAsRenderedImage(0, reader.getDefaultReadParam());
double scaleFactor = 0.5;
ParameterBlock params = new ParameterBlock();
params.addSource(image);
params.add(scaleFactor);
result = JAI.create("", params);
witer = ImageIO.getImageWritersByFormatName("DICOM");
writer = (ImageWriter) witer.next();
fios = new FileImageOutputStream(new File(filename));
writer.setOutput(fios);
writer.write(result);
djai.set(result);
jsp.setViewportView(djai);
}
catch(Exception ex){
//error dialog - invalid file type
System.out.println("Invalid file type, please try again");
JOptionPane.showMessageDialog(this, "File Not Recognized", "Error!", JOptionPane.ERROR_MESSAGE);
}
}
else if(retVal == JFileChooser.CANCEL_OPTION){
System.out.println("User cancelled operation");
}}
private void setResizable(boolean b) {
throw new UnsupportedOperationException("Not yet implemented");
}
}