每次运行时我都会遇到这些错误我认为我如何附加SWT的方式存在问题如何应用它
javax.imageio.IIOException: Can't read input file!
at javax.imageio.ImageIO.read(ImageIO.java:1301)
at test_guiiii.gui_rec.image_resize(gui_rec.java:160)
at test_guiiii.gui_rec$2.widgetSelected(gui_rec.java:117)
at org.eclipse.swt.widgets.TypedListener.handleEvent(TypedListener.java:248)
at org.eclipse.swt.widgets.EventTable.sendEvent(EventTable.java:84)
at org.eclipse.swt.widgets.Display.sendEvent(Display.java:4230)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1491)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1514)
at org.eclipse.swt.widgets.Widget.sendEvent(Widget.java:1499)
at org.eclipse.swt.widgets.Widget.notifyListeners(Widget.java:1299)
at org.eclipse.swt.widgets.Display.runDeferredEvents(Display.java:4072)
at org.eclipse.swt.widgets.Display.readAndDispatch(Display.java:3698)
at test_guiiii.gui_rec.open(gui_rec.java:57)
at test_guiiii.gui_rec.main(gui_rec.java:42)
每次我按下错误时都尝试调试我收到了类似的消息:
这是主类的完整代码:
package test_guiiii;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.graphics.Image;
import java.awt.Graphics2D;
//import java.awt.Image;
import java.awt.RenderingHints;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import javax.imageio.ImageIO;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Text;
import test_guiiii.Rec_det;
import org.eclipse.wb.swt.SWTResourceManager;
public class gui_rec {
//private static JFileChooser jfc = new JFileChooser();
protected Shell shlGui;
Display display = Display.getDefault();
String file_path= null;
String file_name = null;
private Text txt_open;
/**
* Launch the application.
* @param args
*/
public static void main(String[] args) {
try {
gui_rec window = new gui_rec();
window.open();
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* Open the window.
*/
public void open() {
//Display display = Display.getDefault();
createContents();
shlGui.open();
shlGui.layout();
while (!shlGui.isDisposed()) {
if (!display.readAndDispatch()) {
display.sleep();
}
}
}
/**
* Create contents of the window.
*/
protected void createContents() {
shlGui = new Shell(display, SWT.CLOSE | SWT.TITLE | SWT.MIN );
shlGui.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
shlGui.setSize(945, 566);
//shell.setMaximized(true);
shlGui.setText("Graphical User Interface");
//scrolledComposite.setMinSize(lbl_Image.computeSize(SWT.DEFAULT, SWT.DEFAULT));
Label lbl_image = new Label(shlGui, SWT.BORDER | SWT.CENTER);
lbl_image.setBackground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
lbl_image.setBounds(10, 10, 361, 465);
txt_open = new Text(shlGui, SWT.BORDER);
txt_open.setBounds(93, 493, 278, 25);
txt_open.setEditable(false);
Label lbl_rec = new Label(shlGui, SWT.BORDER | SWT.CENTER);
lbl_rec.setBackground(SWTResourceManager.getColor(SWT.COLOR_GRAY));
lbl_rec.setBounds(408, 10, 361, 465);
Button btnNewButton = new Button(shlGui, SWT.NONE);
btnNewButton.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
Rec_det detect = new Rec_det();
detect.det(file_path);
image_resize(lbl_image, "result_new.jpg");
lbl_rec.setImage(new Image(display, "saved.png"));;
}
});
btnNewButton.setBounds(530, 493, 112, 25);
btnNewButton.setText("initiate recognition");
Button btnOpen = new Button(shlGui, SWT.NONE);
btnOpen.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
FileDialog dlg = new FileDialog(shlGui, SWT.MULTI);
String fn = dlg.open();
if (fn != null) {
txt_open.setText(dlg.getFilterPath());
file_name = dlg.getFileName();
file_path = dlg.getFilterPath();
file_path = file_path+'\\'+file_name;
//System.out.println(file_path);
image_resize(lbl_image,file_path);
lbl_image.setImage(new Image(display, "saved.png"));;
}
}
});
btnOpen.setBounds(10, 493, 75, 25);
btnOpen.setText("Open");
Button btnOpenInNew = new Button(shlGui, SWT.NONE);
btnOpenInNew.addSelectionListener(new SelectionAdapter() {
@Override
public void widgetSelected(SelectionEvent e) {
BufferedImage img;
try {
img = ImageIO.read(new File("result_new.jpg"));
image_Open_nw nw = new image_Open_nw();
nw.Mouse(img);
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
});
btnOpenInNew.setBounds(788, 93, 141, 25);
btnOpenInNew.setText("Open in new window");
Label lblToViewThe = new Label(shlGui, SWT.NONE);
lblToViewThe.setAlignment(SWT.CENTER);
lblToViewThe.setBackground(SWTResourceManager.getColor(SWT.COLOR_DARK_GRAY));
lblToViewThe.setForeground(SWTResourceManager.getColor(SWT.COLOR_RED));
lblToViewThe.setBounds(788, 11, 141, 65);
lblToViewThe.setText("To view the output image\r\nof the Recognition process\r\nmore clearly and for the \r\npost processing ");
}
protected void image_resize( Label lbl_image, String file_path){
try {
BufferedImage img1 = ImageIO.read(new File(file_path));
int h = img1.getHeight(null);
int w = img1.getWidth(null);
int temp=0,temp1 = 0;
for(int i=1; i< 100; i++){
temp = h/i;
temp1 = w/i;
//System.out.println(temp);
if(temp < lbl_image.getSize().y && temp1 < lbl_image.getSize().x){
i = 102;
}
}
BufferedImage resizedImg = new BufferedImage(temp1, temp, BufferedImage.TRANSLUCENT);
Graphics2D g2 = resizedImg.createGraphics();
g2.setRenderingHint(RenderingHints.KEY_INTERPOLATION, RenderingHints.VALUE_INTERPOLATION_BILINEAR);
g2.drawImage(img1, 0, 0,temp1, temp, null);
g2.dispose();
File outputfile = new File("saved.png");
ImageIO.write(resizedImg, "png", outputfile);
} catch (IOException e2) {
// TODO Auto-generated catch block
e2.printStackTrace();
}
}
}