📄 imageanalyzer.java
字号:
item.setText(bundle.getString("Print"));
item.setAccelerator(SWT.MOD1 + 'P');
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
menuPrint();
}
});
new MenuItem(fileMenu, SWT.SEPARATOR);
// File -> Exit
item = new MenuItem(fileMenu, SWT.PUSH);
item.setText(bundle.getString("Exit"));
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
shell.close();
}
});
}
void createAlphaMenu(Menu menuBar) {
// Alpha menu
MenuItem item = new MenuItem(menuBar, SWT.CASCADE);
item.setText(bundle.getString("Alpha"));
Menu alphaMenu = new Menu(shell, SWT.DROP_DOWN);
item.setMenu(alphaMenu);
// Alpha -> K
item = new MenuItem(alphaMenu, SWT.PUSH);
item.setText("K");
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
menuComposeAlpha(ALPHA_CONSTANT);
}
});
// Alpha -> (K + x) % 256
item = new MenuItem(alphaMenu, SWT.PUSH);
item.setText("(K + x) % 256");
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
menuComposeAlpha(ALPHA_X);
}
});
// Alpha -> (K + y) % 256
item = new MenuItem(alphaMenu, SWT.PUSH);
item.setText("(K + y) % 256");
item.addSelectionListener(new SelectionAdapter() {
public void widgetSelected(SelectionEvent event) {
menuComposeAlpha(ALPHA_Y);
}
});
}
void menuComposeAlpha(int alpha_op) {
if (image == null)
return;
animate = false; // stop any animation in progress
Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
shell.setCursor(waitCursor);
imageCanvas.setCursor(waitCursor);
try {
if (alpha_op == ALPHA_CONSTANT) {
imageData.alpha = alpha;
} else {
imageData.alpha = -1;
switch (alpha_op) {
case ALPHA_X:
for (int y = 0; y < imageData.height; y++) {
for (int x = 0; x < imageData.width; x++) {
imageData.setAlpha(x, y, (x + alpha) % 256);
}
}
break;
case ALPHA_Y:
for (int y = 0; y < imageData.height; y++) {
for (int x = 0; x < imageData.width; x++) {
imageData.setAlpha(x, y, (y + alpha) % 256);
}
}
break;
default:
break;
}
}
displayImage(imageData);
} finally {
shell.setCursor(null);
imageCanvas.setCursor(crossCursor);
waitCursor.dispose();
}
}
/* Just use Image(device, filename) to load an image file. */
void menuLoad() {
animate = false; // stop any animation in progress
// Get the user to choose an image file.
FileDialog fileChooser = new FileDialog(shell, SWT.OPEN);
if (lastPath != null)
fileChooser.setFilterPath(lastPath);
fileChooser.setFilterExtensions(OPEN_FILTER_EXTENSIONS);
fileChooser.setFilterNames(OPEN_FILTER_NAMES);
String filename = fileChooser.open();
lastPath = fileChooser.getFilterPath();
if (filename == null)
return;
Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
shell.setCursor(waitCursor);
imageCanvas.setCursor(waitCursor);
try {
// Read the new image from the chosen file.
long startTime = System.currentTimeMillis();
Image newImage = new Image(display, filename);
loadTime = System.currentTimeMillis() - startTime; // don't include
// getImageData
// in load time
imageData = newImage.getImageData();
// Cache the filename.
currentName = filename;
fileName = filename;
// Fill in array and loader data.
loader = new ImageLoader();
imageDataArray = new ImageData[] { imageData };
loader.data = imageDataArray;
// Display the image.
imageDataIndex = 0;
displayImage(imageData);
} catch (SWTException e) {
showErrorDialog(bundle.getString("Loading_lc"), filename, e);
} catch (SWTError e) {
showErrorDialog(bundle.getString("Loading_lc"), filename, e);
} catch (OutOfMemoryError e) {
showErrorDialog(bundle.getString("Loading_lc"), filename, e);
} finally {
shell.setCursor(null);
imageCanvas.setCursor(crossCursor);
waitCursor.dispose();
}
}
void menuOpenFile() {
animate = false; // stop any animation in progress
// Get the user to choose an image file.
FileDialog fileChooser = new FileDialog(shell, SWT.OPEN);
if (lastPath != null)
fileChooser.setFilterPath(lastPath);
fileChooser.setFilterExtensions(OPEN_FILTER_EXTENSIONS);
fileChooser.setFilterNames(OPEN_FILTER_NAMES);
String filename = fileChooser.open();
lastPath = fileChooser.getFilterPath();
if (filename == null)
return;
Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
shell.setCursor(waitCursor);
imageCanvas.setCursor(waitCursor);
ImageLoader oldLoader = loader;
try {
loader = new ImageLoader();
if (incremental) {
// Prepare to handle incremental events.
loader.addImageLoaderListener(new ImageLoaderListener() {
public void imageDataLoaded(ImageLoaderEvent event) {
incrementalDataLoaded(event);
}
});
incrementalThreadStart();
}
// Read the new image(s) from the chosen file.
long startTime = System.currentTimeMillis();
imageDataArray = loader.load(filename);
loadTime = System.currentTimeMillis() - startTime;
if (imageDataArray.length > 0) {
// Cache the filename.
currentName = filename;
fileName = filename;
// If there are multiple images in the file (typically GIF)
// then enable the Previous, Next and Animate buttons.
previousButton.setEnabled(imageDataArray.length > 1);
nextButton.setEnabled(imageDataArray.length > 1);
animateButton.setEnabled(imageDataArray.length > 1 && loader.logicalScreenWidth > 0 && loader.logicalScreenHeight > 0);
// Display the first image in the file.
imageDataIndex = 0;
displayImage(imageDataArray[imageDataIndex]);
}
} catch (SWTException e) {
showErrorDialog(bundle.getString("Loading_lc"), filename, e);
loader = oldLoader;
} catch (SWTError e) {
showErrorDialog(bundle.getString("Loading_lc"), filename, e);
loader = oldLoader;
} catch (OutOfMemoryError e) {
showErrorDialog(bundle.getString("Loading_lc"), filename, e);
loader = oldLoader;
} finally {
shell.setCursor(null);
imageCanvas.setCursor(crossCursor);
waitCursor.dispose();
}
}
void menuOpenURL() {
animate = false; // stop any animation in progress
// Get the user to choose an image URL.
TextPrompter textPrompter = new TextPrompter(shell, SWT.APPLICATION_MODAL | SWT.DIALOG_TRIM);
textPrompter.setText(bundle.getString("OpenURLDialog"));
textPrompter.setMessage(bundle.getString("EnterURL"));
String urlname = textPrompter.open();
if (urlname == null)
return;
Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
shell.setCursor(waitCursor);
imageCanvas.setCursor(waitCursor);
ImageLoader oldLoader = loader;
try {
URL url = new URL(urlname);
InputStream stream = url.openStream();
loader = new ImageLoader();
if (incremental) {
// Prepare to handle incremental events.
loader.addImageLoaderListener(new ImageLoaderListener() {
public void imageDataLoaded(ImageLoaderEvent event) {
incrementalDataLoaded(event);
}
});
incrementalThreadStart();
}
// Read the new image(s) from the chosen URL.
long startTime = System.currentTimeMillis();
imageDataArray = loader.load(stream);
loadTime = System.currentTimeMillis() - startTime;
stream.close();
if (imageDataArray.length > 0) {
currentName = urlname;
fileName = null;
// If there are multiple images (typically GIF)
// then enable the Previous, Next and Animate buttons.
previousButton.setEnabled(imageDataArray.length > 1);
nextButton.setEnabled(imageDataArray.length > 1);
animateButton.setEnabled(imageDataArray.length > 1 && loader.logicalScreenWidth > 0 && loader.logicalScreenHeight > 0);
// Display the first image.
imageDataIndex = 0;
displayImage(imageDataArray[imageDataIndex]);
}
} catch (Exception e) {
showErrorDialog(bundle.getString("Loading_lc"), urlname, e);
loader = oldLoader;
} catch (OutOfMemoryError e) {
showErrorDialog(bundle.getString("Loading_lc"), urlname, e);
loader = oldLoader;
} finally {
shell.setCursor(null);
imageCanvas.setCursor(crossCursor);
waitCursor.dispose();
}
}
/*
* Called to start a thread that draws incremental images as they are
* loaded.
*/
void incrementalThreadStart() {
incrementalEvents = new Vector();
incrementalThread = new Thread("Incremental") {
public void run() {
// Draw the first ImageData increment.
while (incrementalEvents != null) {
// Synchronize so we don't try to remove when the vector is
// null.
synchronized (ImageAnalyzer.this) {
if (incrementalEvents != null) {
if (incrementalEvents.size() > 0) {
ImageLoaderEvent event = (ImageLoaderEvent) incrementalEvents.remove(0);
if (image != null)
image.dispose();
image = new Image(display, event.imageData);
imageData = event.imageData;
imageCanvasGC.drawImage(image, 0, 0, imageData.width, imageData.height, imageData.x, imageData.y, imageData.width, imageData.height);
} else {
yield();
}
}
}
}
display.wake();
}
};
incrementalThread.setDaemon(true);
incrementalThread.start();
}
/*
* Called when incremental image data has been loaded, for example, for
* interlaced GIF/PNG or progressive JPEG.
*/
void incrementalDataLoaded(ImageLoaderEvent event) {
// Synchronize so that we do not try to add while
// the incremental drawing thread is removing.
synchronized (this) {
incrementalEvents.addElement(event);
}
}
void menuSave() {
if (image == null)
return;
animate = false; // stop any animation in progress
// If the image file type is unknown, we can't 'Save',
// so we have to use 'Save As...'.
if (imageData.type == SWT.IMAGE_UNDEFINED || fileName == null) {
menuSaveAs();
return;
}
Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);
shell.setCursor(waitCursor);
imageCanvas.setCursor(waitCursor);
try {
// Save the current image to the current file.
loader.data = new ImageData[] { imageData };
loader.save(fileName, imageData.type);
} catch (SWTException e) {
showErrorDialog(bundle.getString("Saving_lc"), fileName, e);
} catch (SWTError e) {
showErrorDialog(bundle.getString("Saving_lc"), fileName, e);
} finally {
shell.setCursor(null);
imageCanvas.setCursor(crossCursor);
waitCursor.dispose();
}
}
void menuSaveAs() {
if (image == null)
return;
animate = false; // stop any animation in progress
// Get the user to choose a file name and type to save.
FileDialog fileChooser = new FileDialog(shell, SWT.SAVE);
fileChooser.setFilterPath(lastPath);
if (fileName != null) {
String name = fileName;
int nameStart = name.lastIndexOf(java.io.File.separatorChar);
if (nameStart > -1) {
name = name.substring(nameStart + 1);
}
fileChooser.setFileName(name);
}
fileChooser.setFilterExtensions(SAVE_FILTER_EXTENSIONS);
fileChooser.setFilterNames(SAVE_FILTER_NAMES);
String filename = fileChooser.open();
lastPath = fileChooser.getFilterPath();
if (filename == null)
return;
// Figure out what file type the user wants saved.
// We need to rely on the file extension because FileDialog
// does not have API for asking what filter type was selected.
int filetype = determineFileType(filename);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -