⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 imageanalyzer.java

📁 SUN公司eclipse3.2.2经典例子
💻 JAVA
📖 第 1 页 / 共 5 页
字号:
			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);		if (filetype == SWT.IMAGE_UNDEFINED) {			MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);			box.setMessage(createMsg(bundle.getString("Unknown_extension"), 			                         filename.substring(filename.lastIndexOf('.') + 1)));			box.open();			return;		}				if (new java.io.File(filename).exists()) {			MessageBox box = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);			box.setMessage(createMsg(bundle.getString("Overwrite"), filename));			if (box.open() == SWT.CANCEL)				return;		}				Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);		shell.setCursor(waitCursor);		imageCanvas.setCursor(waitCursor);		try {			// Save the current image to the specified file.			boolean multi = false;			if (loader.data.length > 1) {				MessageBox box = new MessageBox(shell, SWT.ICON_QUESTION | SWT.YES | SWT.NO | SWT.CANCEL);				box.setMessage(createMsg(bundle.getString("Save_all"), new Integer(loader.data.length)));				int result = box.open();				if (result == SWT.CANCEL) return;				if (result == SWT.YES) multi = true;			}			/* If the image has transparency but the user has transparency turned off,			 * turn it off in the saved image. */			int transparentPixel = imageData.transparentPixel;			if (!multi && transparentPixel != -1 && !transparent) {				imageData.transparentPixel = -1;			}						if (!multi) loader.data = new ImageData[] {imageData};			loader.save(filename, filetype);						/* Restore the previous transparency setting. */			if (!multi && transparentPixel != -1 && !transparent) {				imageData.transparentPixel = transparentPixel;			}			// Update the shell title and file type label,			// and use the new file.			fileName = filename;			shell.setText(createMsg(bundle.getString("Analyzer_on"), filename));			typeLabel.setText(createMsg(bundle.getString("Type_string"), fileTypeString(filetype)));		} 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 menuSaveMaskAs() {		if (image == null || !showMask) return;		if (imageData.getTransparencyType() == SWT.TRANSPARENCY_NONE) 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) fileChooser.setFileName(fileName);		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);		if (filetype == SWT.IMAGE_UNDEFINED) {			MessageBox box = new MessageBox(shell, SWT.ICON_ERROR);			box.setMessage(createMsg(bundle.getString("Unknown_extension"), 			                         filename.substring(filename.lastIndexOf('.') + 1)));			box.open();			return;		}				if (new java.io.File(filename).exists()) {			MessageBox box = new MessageBox(shell, SWT.ICON_QUESTION | SWT.OK | SWT.CANCEL);			box.setMessage(createMsg(bundle.getString("Overwrite"), filename));			if (box.open() == SWT.CANCEL)				return;		}				Cursor waitCursor = new Cursor(display, SWT.CURSOR_WAIT);		shell.setCursor(waitCursor);		imageCanvas.setCursor(waitCursor);		try {			// Save the mask of the current image to the specified file.			ImageData maskImageData = imageData.getTransparencyMask();			loader.data = new ImageData[] {maskImageData};			loader.save(filename, filetype);					} 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();		}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -