| /******************************************************************************* |
| * Copyright (c) 2018 Agence spatiale canadienne / Canadian Space Agency |
| * All rights reserved. This program and the accompanying materials |
| * are made available under the terms of the Eclipse Public License v1.0 |
| * which accompanies this distribution, and is available at |
| * http://www.eclipse.org/legal/epl-v10.html |
| * |
| * Contributors: |
| * Pierre Allard - initial API and implementation |
| * Regent L'Archeveque |
| * |
| * SPDX-License-Identifier: EPL-1.0 |
| * |
| *******************************************************************************/ |
| package org.eclipse.apogy.addons.sensors.imaging.camera.composites; |
| |
| import java.awt.Color; |
| import java.awt.Font; |
| import java.io.File; |
| import java.util.List; |
| |
| import org.eclipse.apogy.addons.sensors.imaging.AbstractCamera; |
| import org.eclipse.apogy.addons.sensors.imaging.ApogyAddonsSensorsImagingPackage; |
| import org.eclipse.apogy.addons.sensors.imaging.ImageSnapshot; |
| import org.eclipse.apogy.addons.sensors.imaging.RectifiedImageSnapshot; |
| import org.eclipse.apogy.addons.sensors.imaging.camera.ApogyAddonsSensorsImagingCameraPackage; |
| import org.eclipse.apogy.addons.sensors.imaging.camera.CameraImageAnnotation; |
| import org.eclipse.apogy.addons.sensors.imaging.camera.CameraOverlay; |
| import org.eclipse.apogy.addons.sensors.imaging.camera.CameraTool; |
| import org.eclipse.apogy.addons.sensors.imaging.camera.CameraViewConfiguration; |
| import org.eclipse.apogy.addons.sensors.imaging.camera.CameraViewDisplayRotation; |
| import org.eclipse.apogy.addons.sensors.imaging.camera.DrawnCameraOverlay; |
| import org.eclipse.apogy.addons.sensors.imaging.camera.ImageCameraOverlay; |
| import org.eclipse.apogy.addons.sensors.imaging.camera.ImageFilter; |
| import org.eclipse.apogy.addons.sensors.imaging.camera.OverlayAlignment; |
| import org.eclipse.apogy.addons.sensors.imaging.camera.ToolTipTextProvider; |
| import org.eclipse.apogy.addons.sensors.imaging.camera.listeners.CameraViewConfigurationListener; |
| import org.eclipse.apogy.common.emf.transaction.ApogyCommonTransactionFacade; |
| import org.eclipse.apogy.common.images.AbstractEImage; |
| import org.eclipse.apogy.common.images.EImagesUtilities; |
| import org.eclipse.apogy.common.images.ImageAlignment; |
| import org.eclipse.apogy.common.images.ui.composites.ImageDisplayComposite; |
| import org.eclipse.core.runtime.IProgressMonitor; |
| import org.eclipse.core.runtime.IStatus; |
| import org.eclipse.core.runtime.Status; |
| import org.eclipse.core.runtime.jobs.Job; |
| import org.eclipse.emf.common.notify.Adapter; |
| import org.eclipse.emf.common.notify.Notification; |
| import org.eclipse.emf.common.notify.impl.AdapterImpl; |
| import org.eclipse.swt.SWT; |
| import org.eclipse.swt.custom.ScrolledComposite; |
| import org.eclipse.swt.events.DisposeEvent; |
| import org.eclipse.swt.events.DisposeListener; |
| import org.eclipse.swt.events.MouseEvent; |
| import org.eclipse.swt.events.MouseListener; |
| import org.eclipse.swt.events.MouseMoveListener; |
| import org.eclipse.swt.events.PaintEvent; |
| import org.eclipse.swt.events.PaintListener; |
| import org.eclipse.swt.graphics.Cursor; |
| import org.eclipse.swt.graphics.ImageData; |
| import org.eclipse.swt.graphics.Point; |
| import org.eclipse.swt.layout.FillLayout; |
| import org.eclipse.swt.layout.GridData; |
| import org.eclipse.swt.widgets.Composite; |
| import org.slf4j.Logger; |
| import org.slf4j.LoggerFactory; |
| |
| public class CameraViewConfigurationComposite extends Composite { |
| |
| private static final Logger Logger = LoggerFactory.getLogger(CameraViewConfigurationComposite.class); |
| |
| public static final int DEFAULT_IMAGE_WIDTH = 640; |
| public static final int DEFAULT_IMAGE_HEIGHT = 480; |
| |
| private CameraViewConfigurationListener cameraViewConfigurationListener = null; |
| private Adapter cameraAdapter; |
| |
| // private Adapter cameraViewConfigurationAdapter; |
| |
| protected CameraViewConfiguration cameraViewConfiguration; |
| protected AbstractCamera camera = null; |
| |
| private boolean displayRectifiedImage = false; |
| |
| private ScrolledComposite scrolledComposite = null; |
| private ImageDisplayComposite imageDisplayComposite = null; |
| |
| private ImageSnapshot lastImageSnapshot = null; |
| |
| private MouseListener mouseListener; |
| private MouseMoveListener mouseMoveListener; |
| private int mouseButton = 0; |
| |
| private boolean busy = false; |
| |
| public CameraViewConfigurationComposite(Composite parent, int style) { |
| super(parent, style); |
| setLayout(new FillLayout(SWT.HORIZONTAL)); |
| addDisposeListener(new DisposeListener() { |
| |
| @Override |
| public void widgetDisposed(DisposeEvent e) { |
| if (getCameraViewConfiguration() != null) { |
| disposeOfAnnotations(); |
| } |
| |
| setCameraViewConfiguration(null); |
| } |
| }); |
| |
| this.scrolledComposite = new ScrolledComposite(this, SWT.H_SCROLL | SWT.V_SCROLL); |
| this.scrolledComposite.setExpandHorizontal(true); |
| this.scrolledComposite.setExpandVertical(true); |
| |
| this.imageDisplayComposite = new ImageDisplayComposite(this.scrolledComposite, SWT.BORDER); |
| GridData gridData = new GridData(SWT.CENTER, SWT.CENTER, false, false); |
| this.imageDisplayComposite.setLayoutData(gridData); |
| |
| // Register listeners to the mouse. |
| this.imageDisplayComposite.addImageMouseListener(getMouseListener()); |
| this.imageDisplayComposite.addImageMouseMoveListener(getMouseMoveListener()); |
| |
| this.scrolledComposite.setContent(this.imageDisplayComposite); |
| |
| this.imageDisplayComposite.setSize(getImageSize()); |
| this.scrolledComposite.setMinSize(getImageSize()); |
| |
| this.imageDisplayComposite |
| .setImageData(EImagesUtilities.INSTANCE.convertToImageData(getNoDataImage().asBufferedImage())); |
| this.imageDisplayComposite |
| .setImageData(EImagesUtilities.INSTANCE.convertToImageData(getNoDataImage().asBufferedImage())); |
| this.imageDisplayComposite.fitImage(); |
| |
| // Disable hidding of tooltip on mouse down. |
| this.imageDisplayComposite.getToolTip().setHideOnMouseDown(false); |
| |
| this.addPaintListener(new PaintListener() { |
| @Override |
| public void paintControl(PaintEvent e) { |
| // imageDisplayComposite.setSize(getImageSize()); |
| // scrolledComposite.setMinSize(getImageSize()); |
| // imageDisplayComposite.fitImage(); |
| } |
| }); |
| |
| setDisplaySize(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT); |
| |
| Cursor cursor = new Cursor(getDisplay(), SWT.CURSOR_CROSS); |
| this.imageDisplayComposite.setCursor(cursor); |
| } |
| |
| public boolean isDisplayRectifiedImage() { |
| return this.displayRectifiedImage; |
| } |
| |
| public void setDisplayRectifiedImage(boolean displayRectifiedImage) { |
| this.displayRectifiedImage = displayRectifiedImage; |
| forceImageUpdate(); |
| } |
| |
| public CameraViewConfiguration getCameraViewConfiguration() { |
| return this.cameraViewConfiguration; |
| } |
| |
| public void setCameraViewConfiguration(CameraViewConfiguration newCameraViewConfiguration) { |
| if (this.cameraViewConfiguration != null) { |
| disposeOfAnnotations(); |
| } |
| |
| this.cameraViewConfiguration = newCameraViewConfiguration; |
| |
| getCameraViewConfigurationListener().setCameraViewConfiguration(newCameraViewConfiguration); |
| |
| if (newCameraViewConfiguration != null) { |
| setCamera(newCameraViewConfiguration.getCamera()); |
| |
| setDisplaySize(newCameraViewConfiguration.getImageWidth(), newCameraViewConfiguration.getImageHeight()); |
| setDisplayRectifiedImage(newCameraViewConfiguration.isDisplayRectifiedImage()); |
| } else { |
| setDisplaySize(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT); |
| setDisplayRectifiedImage(false); |
| setCamera(null); |
| } |
| } |
| |
| protected AbstractCamera getCamera() { |
| return this.camera; |
| } |
| |
| protected void setCamera(AbstractCamera newCamera) { |
| // Unregister adapter to previous camera if applicable. |
| if (this.camera != null) { |
| this.camera.eAdapters().remove(getCameraAdapter()); |
| } |
| |
| // Clear last image snapshot if the camera has changed. |
| if (this.camera != newCamera) { |
| this.lastImageSnapshot = null; |
| } |
| |
| this.busy = false; |
| this.camera = newCamera; |
| |
| // Unregister adapter to previous camera if applicable. |
| if (newCamera != null) { |
| newCamera.eAdapters().add(getCameraAdapter()); |
| updateImageSnapshot(newCamera.getLatestImageSnapshot()); |
| } else { |
| // Clear the image. |
| getDisplay().asyncExec(new Runnable() { |
| @Override |
| public void run() { |
| setDisplaySize(DEFAULT_IMAGE_WIDTH, DEFAULT_IMAGE_HEIGHT); |
| CameraViewConfigurationComposite.this.imageDisplayComposite.setImageData( |
| EImagesUtilities.INSTANCE.convertToImageData(getNoDataImage().asBufferedImage())); |
| } |
| }); |
| } |
| |
| // Updates camera tools. |
| updateToolsCamera(newCamera); |
| } |
| |
| protected void disposeOfAnnotations() { |
| // Dispose of all Overlays. |
| disposeOfOverlays(); |
| |
| // Dispose of all Filters. |
| disposeOfFilters(); |
| |
| // Dispose of all tools. |
| disposeOfCameraTools(); |
| } |
| |
| protected void forceImageUpdate() { |
| updateImageSnapshot(this.lastImageSnapshot); |
| } |
| |
| protected void updateImageSnapshot(ImageSnapshot newImageSnapshot) { |
| // If not busy processing the previous image. |
| if (!this.busy && getCameraViewConfiguration() != null) { |
| try { |
| // Gets the camera image. |
| AbstractEImage cameraImage = null; |
| |
| if (newImageSnapshot != null) { |
| this.busy = true; |
| |
| // Updates image count. |
| if (newImageSnapshot != this.lastImageSnapshot) { |
| ApogyCommonTransactionFacade.INSTANCE.basicSet(getCameraViewConfiguration(), |
| ApogyAddonsSensorsImagingCameraPackage.Literals.CAMERA_VIEW_CONFIGURATION__IMAGE_COUNT, |
| getCameraViewConfiguration().getImageCount() + 1); |
| } |
| |
| if (isDisplayRectifiedImage() && newImageSnapshot instanceof RectifiedImageSnapshot) { |
| RectifiedImageSnapshot rectifiedImageSnapshot = (RectifiedImageSnapshot) newImageSnapshot; |
| cameraImage = rectifiedImageSnapshot.getRectifiedImage(); |
| } else { |
| cameraImage = newImageSnapshot.getImage(); |
| } |
| } |
| |
| // If no camera image is available, use no data image. |
| if (cameraImage == null) { |
| cameraImage = getNoDataImage(); |
| } else { |
| cameraImage = applyFilters(cameraImage); |
| } |
| |
| // Updates tools. |
| updateToolsImageSnapshot(newImageSnapshot); |
| |
| // Resize the camera image to match the display size. |
| Point imageSize = getImageSize(); |
| AbstractEImage resizedCameraImage = EImagesUtilities.INSTANCE.resize(cameraImage, imageSize.x, |
| imageSize.y); |
| |
| // Applies overlays. |
| AbstractEImage displayedImage = applyOverlays(resizedCameraImage); |
| |
| // Rotate image to match display orientation |
| final AbstractEImage rotatedImage = applyCameraViewDisplayRotation( |
| getCameraViewConfiguration().getCameraViewDisplayRotation(), displayedImage); |
| |
| // Displays the image. |
| updateDisplayedImage(rotatedImage); |
| |
| // Save images if required. |
| if (newImageSnapshot != this.lastImageSnapshot) { |
| saveImages(resizedCameraImage, displayedImage); |
| } |
| |
| this.lastImageSnapshot = newImageSnapshot; |
| } catch (Throwable t) { |
| Logger.error(t.getMessage(), t); |
| this.busy = false; |
| } |
| } |
| } |
| |
| protected void saveImages(final AbstractEImage rawImage, final AbstractEImage displayedImage) { |
| if (getCameraViewConfiguration() != null) { |
| if (getCameraViewConfiguration().isImageAutoSaveEnable()) { |
| AbstractEImage imageToSave = null; |
| |
| if (getCameraViewConfiguration().isSaveImageWithOverlays()) { |
| imageToSave = displayedImage; |
| } else { |
| imageToSave = rawImage; |
| } |
| |
| if (imageToSave != null) { |
| final long timeStamp = System.currentTimeMillis(); |
| final AbstractEImage image = imageToSave; |
| Job job = new Job("Save Image") { |
| @Override |
| protected IStatus run(IProgressMonitor monitor) { |
| // Creates image file name. |
| String fileName = getCameraViewConfiguration().getName().replaceAll(" ", "_") + "_" |
| + Long.toString(timeStamp) + ".png"; |
| String filePath = getCameraViewConfiguration().getImageAutoSaveFolderPath() + File.separator |
| + fileName; |
| |
| try { |
| // Saves image. |
| EImagesUtilities.INSTANCE.saveImageAsPNG(filePath, image); |
| |
| return Status.OK_STATUS; |
| } catch (Throwable t) { |
| Logger.error("Failed to save image to <" + filePath + "> !", t); |
| return Status.OK_STATUS; |
| } |
| } |
| }; |
| job.schedule(); |
| } |
| } |
| } |
| } |
| |
| protected AbstractEImage applyFilters(final AbstractEImage cameraImage) { |
| AbstractEImage filteredImage = EImagesUtilities.INSTANCE.copy(cameraImage); |
| |
| if (getCameraViewConfiguration() != null) { |
| for (ImageFilter filter : getCameraViewConfiguration().getFilterList().getImageFilters()) { |
| if (filter.isEnabled()) { |
| filteredImage = filter.filter(getCameraViewConfiguration().getCamera(), filteredImage); |
| } |
| } |
| } |
| |
| return filteredImage; |
| } |
| |
| protected AbstractEImage applyOverlays(AbstractEImage cameraImage) { |
| AbstractEImage displayedImage = null; |
| |
| if (getCameraViewConfiguration() != null) { |
| AbstractCamera camera = getCameraViewConfiguration().getCamera(); |
| displayedImage = EImagesUtilities.INSTANCE.copy(cameraImage); |
| |
| if (camera != null) { |
| // For all CameraImageAnnotation that are CameraOverlay. |
| List<CameraImageAnnotation> cameraImageAnnotationList = getCameraViewConfiguration() |
| .getCameraImageAnnotations(); |
| for (CameraImageAnnotation cameraImageAnnotation : cameraImageAnnotationList) { |
| if (cameraImageAnnotation instanceof CameraOverlay) { |
| CameraOverlay cameraOverlay = (CameraOverlay) cameraImageAnnotation; |
| |
| try { |
| // Process only overlay that are visible. |
| if (cameraOverlay.isVisible()) { |
| int overlayWidth = getCameraViewConfiguration().getImageWidth(); |
| int overlayHeight = getCameraViewConfiguration().getImageHeight(); |
| |
| OverlayAlignment overlayAlignment = cameraOverlay.getOverlayAlignment(); |
| |
| if (cameraOverlay instanceof DrawnCameraOverlay) { |
| DrawnCameraOverlay drawnCameraOverlay = (DrawnCameraOverlay) cameraOverlay; |
| |
| // Applies the overlay. |
| displayedImage = drawnCameraOverlay.applyOverlay(camera, displayedImage, |
| overlayAlignment, overlayWidth, overlayHeight); |
| } else if (cameraOverlay instanceof ImageCameraOverlay) { |
| if (cameraOverlay.getOverlayAlignment() != OverlayAlignment.CENTER) { |
| overlayWidth = (int) Math |
| .floor((getCameraViewConfiguration().getImageWidth() * 0.5f)); |
| overlayHeight = (int) Math |
| .floor((getCameraViewConfiguration().getImageHeight() * 0.5f)); |
| } |
| |
| ImageCameraOverlay imageCameraOverlay = (ImageCameraOverlay) cameraOverlay; |
| |
| // Gets the overlay image. |
| AbstractEImage overlayImage = imageCameraOverlay.getOverlay(camera, |
| overlayAlignment, overlayWidth, overlayHeight); |
| |
| // Applies overlay image onto camera image. |
| ImageAlignment alignment = ImageAlignment.CENTER; |
| switch (cameraOverlay.getOverlayAlignment().getValue()) { |
| case OverlayAlignment.LOWER_LEFT_CORNER_VALUE: |
| alignment = ImageAlignment.LOWER_LEFT_CORNER; |
| break; |
| |
| case OverlayAlignment.LOWER_RIGHT_CORNER_VALUE: |
| alignment = ImageAlignment.LOWER_RIGHT_CORNER; |
| break; |
| |
| case OverlayAlignment.UPPER_LEFT_CORNER_VALUE: |
| alignment = ImageAlignment.UPPER_LEFT_CORNER; |
| break; |
| |
| case OverlayAlignment.UPPER_RIGHT_CORNER_VALUE: |
| alignment = ImageAlignment.UPPER_RIGHT_CORNER; |
| break; |
| |
| default: |
| alignment = ImageAlignment.CENTER; |
| break; |
| } |
| |
| displayedImage = EImagesUtilities.INSTANCE.superPoseImages(displayedImage, |
| overlayImage, false, alignment); |
| } |
| } |
| } catch (Throwable t) { |
| Logger.error("Error while applying overlay <" + cameraOverlay + ">!", t); |
| } |
| } |
| } |
| } |
| } else { |
| displayedImage = cameraImage; |
| } |
| |
| return displayedImage; |
| } |
| |
| protected AbstractEImage applyCameraViewDisplayRotation(CameraViewDisplayRotation cameraViewDisplayRotation, |
| AbstractEImage originalImage) { |
| AbstractEImage rotatedImage = null; |
| |
| switch (cameraViewDisplayRotation.getValue()) { |
| case CameraViewDisplayRotation.NONE_VALUE: |
| rotatedImage = originalImage; |
| break; |
| |
| case CameraViewDisplayRotation.CW_90_DEG_VALUE: |
| rotatedImage = EImagesUtilities.INSTANCE.rotate(originalImage, (Math.PI / 2.0), true); |
| break; |
| |
| case CameraViewDisplayRotation.CCW_90_DEG_VALUE: |
| rotatedImage = EImagesUtilities.INSTANCE.rotate(originalImage, (-Math.PI / 2.0), true); |
| break; |
| |
| case CameraViewDisplayRotation.CW_180_DEG_VALUE: |
| rotatedImage = EImagesUtilities.INSTANCE.rotate(originalImage, (Math.PI), true); |
| break; |
| |
| default: |
| break; |
| } |
| |
| return rotatedImage; |
| } |
| |
| protected void updateDisplayedImage(final AbstractEImage newImage) { |
| if ((this.imageDisplayComposite != null) && !this.imageDisplayComposite.isDisposed()) { |
| if (newImage != null) { |
| final ImageData imgData = EImagesUtilities.INSTANCE.convertToImageData(newImage.asBufferedImage()); |
| |
| getDisplay().asyncExec(new Runnable() { |
| @Override |
| public void run() { |
| try { |
| if (newImage.asBufferedImage() != null) { |
| CameraViewConfigurationComposite.this.imageDisplayComposite.setSize(newImage.getWidth(), |
| newImage.getHeight()); |
| CameraViewConfigurationComposite.this.scrolledComposite.setMinSize(newImage.getWidth(), |
| newImage.getHeight()); |
| |
| CameraViewConfigurationComposite.this.imageDisplayComposite.setImageData(imgData); |
| CameraViewConfigurationComposite.this.imageDisplayComposite.fitImage(); |
| } else { |
| } |
| CameraViewConfigurationComposite.this.busy = false; |
| } catch (Throwable t) { |
| CameraViewConfigurationComposite.this.busy = false; |
| } |
| } |
| }); |
| } |
| } |
| } |
| |
| protected void setDisplaySize(final int width, final int height) { |
| if (this.imageDisplayComposite != null && !this.imageDisplayComposite.isDisposed()) { |
| getDisplay().asyncExec(new Runnable() { |
| @Override |
| public void run() { |
| if (!CameraViewConfigurationComposite.this.imageDisplayComposite.isDisposed()) { |
| CameraViewConfigurationComposite.this.imageDisplayComposite.setSize(width, height); |
| CameraViewConfigurationComposite.this.scrolledComposite.setMinSize(width, height); |
| CameraViewConfigurationComposite.this.imageDisplayComposite.fitImage(); |
| } |
| } |
| }); |
| } |
| } |
| |
| protected Adapter getCameraAdapter() { |
| if (this.cameraAdapter == null) { |
| this.cameraAdapter = new AdapterImpl() { |
| @Override |
| public void notifyChanged(Notification msg) { |
| |
| if (getCameraViewConfiguration() != null |
| && getCameraViewConfiguration().getCamera() == msg.getNotifier()) { |
| if (msg.getFeatureID( |
| AbstractCamera.class) == ApogyAddonsSensorsImagingPackage.ABSTRACT_CAMERA__LATEST_IMAGE_SNAPSHOT) { |
| ImageSnapshot newImageSnapshot = (ImageSnapshot) msg.getNewValue(); |
| updateImageSnapshot(newImageSnapshot); |
| } |
| } |
| } |
| }; |
| } |
| |
| return this.cameraAdapter; |
| } |
| |
| protected void updateToolsImageSnapshot(final ImageSnapshot imageSnapshot) { |
| if (getCameraViewConfiguration() != null) { |
| for (CameraTool cameraTool : getCameraViewConfiguration().getToolList().getTools()) { |
| try { |
| cameraTool.updateImageSnapshot(imageSnapshot); |
| } catch (Throwable t) { |
| Logger.error(t.getMessage(), t); |
| } |
| } |
| } |
| } |
| |
| protected void updateToolsCamera(final AbstractCamera newCamera) { |
| if (getCameraViewConfiguration() != null) { |
| for (CameraTool cameraTool : getCameraViewConfiguration().getToolList().getTools()) { |
| try { |
| cameraTool.initializeCamera(newCamera); |
| } catch (Throwable t) { |
| Logger.error(t.getMessage(), t); |
| } |
| } |
| } |
| } |
| |
| protected void disposeOfOverlays() { |
| if (getCameraViewConfiguration() != null) { |
| for (CameraOverlay cameraOverlay : getCameraViewConfiguration().getOverlayList().getOverlays()) { |
| try { |
| cameraOverlay.dispose(); |
| } catch (Throwable t) { |
| Logger.error(t.getMessage(), t); |
| } |
| } |
| } |
| } |
| |
| protected void disposeOfFilters() { |
| if (getCameraViewConfiguration() != null) { |
| for (ImageFilter imageFilter : getCameraViewConfiguration().getFilterList().getImageFilters()) { |
| try { |
| imageFilter.dispose(); |
| } catch (Throwable t) { |
| Logger.error(t.getMessage(), t); |
| } |
| } |
| } |
| } |
| |
| protected void disposeOfCameraTools() { |
| if (getCameraViewConfiguration() != null) { |
| for (CameraTool cameraTool : getCameraViewConfiguration().getToolList().getTools()) { |
| try { |
| cameraTool.dispose(); |
| } catch (Throwable t) { |
| Logger.error(t.getMessage(), t); |
| } |
| } |
| } |
| } |
| |
| protected Point getImageSize() { |
| int width = 0; |
| int height = 0; |
| |
| if (getCameraViewConfiguration() != null) { |
| width = getCameraViewConfiguration().getImageWidth(); |
| height = getCameraViewConfiguration().getImageHeight(); |
| } else { |
| width = DEFAULT_IMAGE_WIDTH; |
| height = DEFAULT_IMAGE_HEIGHT; |
| } |
| |
| return new Point(width, height); |
| } |
| |
| protected AbstractEImage getNoDataImage() { |
| Point size = getImageSize(); |
| |
| String text = ""; |
| java.awt.Font font = new Font(Font.SANS_SERIF, Font.PLAIN, 24); |
| java.awt.Color textColor = new Color(255, 0, 0); |
| java.awt.Color backgroundColor = new Color(255, 0, 0); |
| |
| if (getCameraViewConfiguration() == null) { |
| text = "No Camera View Configuration Selected !"; |
| textColor = new Color(255, 0, 0); |
| backgroundColor = new Color(0, 0, 0); |
| } else if (getCamera() == null) { |
| text = "The Camera is not initialized."; |
| textColor = new Color(255, 255, 0); |
| backgroundColor = new Color(0, 0, 0); |
| } else if (getCamera().getLatestImageSnapshot() == null) { |
| text = "No image available."; |
| textColor = new Color(0, 255, 0); |
| backgroundColor = new Color(0, 0, 0); |
| } |
| |
| AbstractEImage backgroundImage = EImagesUtilities.INSTANCE.createUniformColorImage(size.x, size.y, |
| backgroundColor.getRed(), backgroundColor.getGreen(), backgroundColor.getBlue(), 255); |
| AbstractEImage textImage = EImagesUtilities.INSTANCE.createTextImage(text, font, textColor, backgroundColor, 2); |
| AbstractEImage image = EImagesUtilities.INSTANCE.applyOverlay(backgroundImage, textImage, false); |
| |
| return image; |
| } |
| |
| protected CameraViewConfigurationListener getCameraViewConfigurationListener() { |
| if (this.cameraViewConfigurationListener == null) { |
| this.cameraViewConfigurationListener = new CameraViewConfigurationListener(null) { |
| @Override |
| protected void cameraChanged(AbstractCamera newCamera) { |
| setCamera(newCamera); |
| } |
| |
| @Override |
| protected void displayRectifiedChanged(boolean newRectified) { |
| setDisplayRectifiedImage(newRectified); |
| } |
| |
| @Override |
| protected void imageSizeChanged(int newImageWidth, int newImageHeigh) { |
| setDisplaySize(newImageWidth, newImageHeigh); |
| } |
| |
| @Override |
| protected void imageFiltersChanged() { |
| forceImageUpdate(); |
| } |
| |
| @Override |
| protected void overlaysChanged() { |
| forceImageUpdate(); |
| } |
| |
| @Override |
| protected void toolsChanged() { |
| forceImageUpdate(); |
| } |
| }; |
| } |
| return this.cameraViewConfigurationListener; |
| } |
| |
| protected MouseListener getMouseListener() { |
| if (this.mouseListener == null) { |
| this.mouseListener = new MouseListener() { |
| @Override |
| public void mouseUp(MouseEvent e) { |
| CameraViewConfigurationComposite.this.mouseButton = 0; |
| } |
| |
| @Override |
| public void mouseDown(MouseEvent e) { |
| if (getCamera() != null) { |
| // Updates mouseButton |
| CameraViewConfigurationComposite.this.mouseButton = e.button; |
| |
| Point point = convertToCurrentImageCoordinates(e); |
| |
| AbstractEImage cameraImage = null; |
| if (getCamera().getLatestImageSnapshot() != null) { |
| cameraImage = getCamera().getLatestImageSnapshot().getImage(); |
| } |
| |
| // Sends the event to all active tools. |
| for (CameraTool cameraTool : getCameraViewConfiguration().getToolList().getTools()) { |
| if (cameraTool.isVisible()) { |
| try { |
| cameraTool.positionSelected(cameraImage, |
| CameraViewConfigurationComposite.this.mouseButton, point.x, point.y); |
| } catch (Throwable t) { |
| Logger.error(t.getMessage(), t); |
| } |
| } |
| } |
| |
| // Gets the tool tip text |
| String toolTipText = getToolTipText(getCamera(), |
| CameraViewConfigurationComposite.this.lastImageSnapshot, |
| CameraViewConfigurationComposite.this.mouseButton, point.x, point.y); |
| |
| // Set the toolTipText. |
| CameraViewConfigurationComposite.this.imageDisplayComposite.getToolTip().setText(toolTipText); |
| |
| // Forces the toolTip to be displayed. |
| CameraViewConfigurationComposite.this.imageDisplayComposite.getToolTip() |
| .show(new Point(e.x, e.y)); |
| } |
| } |
| |
| @Override |
| public void mouseDoubleClick(MouseEvent e) { |
| // Updates mouseButton |
| CameraViewConfigurationComposite.this.mouseButton = e.button; |
| } |
| }; |
| } |
| return this.mouseListener; |
| } |
| |
| protected MouseMoveListener getMouseMoveListener() { |
| if (this.mouseMoveListener == null) { |
| this.mouseMoveListener = new MouseMoveListener() { |
| @Override |
| public void mouseMove(MouseEvent e) { |
| if (getCamera() != null) { |
| Point point = convertToCurrentImageCoordinates(e); |
| |
| AbstractEImage cameraImage = null; |
| if (getCamera().getLatestImageSnapshot() != null) { |
| cameraImage = getCamera().getLatestImageSnapshot().getImage(); |
| } |
| |
| // Sends the event to all active tools. |
| for (CameraTool cameraTool : getCameraViewConfiguration().getToolList().getTools()) { |
| if (cameraTool.isVisible()) { |
| try { |
| cameraTool.mouseMoved(cameraImage, |
| CameraViewConfigurationComposite.this.mouseButton, point.x, point.y); |
| } catch (Throwable t) { |
| Logger.error(t.getMessage(), t); |
| } |
| } |
| } |
| |
| // Gets the tool tip text |
| String toolTipText = getToolTipText(getCamera(), |
| CameraViewConfigurationComposite.this.lastImageSnapshot, |
| CameraViewConfigurationComposite.this.mouseButton, point.x, point.y); |
| |
| // Set the toolTipText. |
| CameraViewConfigurationComposite.this.imageDisplayComposite.getToolTip().setText(toolTipText); |
| } |
| } |
| }; |
| } |
| return this.mouseMoveListener; |
| } |
| |
| protected String getToolTipText(AbstractCamera camera, ImageSnapshot imageSnapshot, int mouseButton, int x, int y) { |
| // Gets the tool tip text |
| String toolTipText = null; |
| |
| List<CameraImageAnnotation> annotations = getCameraViewConfiguration().getCameraImageAnnotations(); |
| for (CameraImageAnnotation annotation : annotations) { |
| if (annotation.isVisible()) { |
| if (annotation instanceof ToolTipTextProvider) { |
| ToolTipTextProvider toolTipTextProvider = (ToolTipTextProvider) annotation; |
| String annotationToolTipText = toolTipTextProvider.getToolTipText(getCamera(), |
| this.lastImageSnapshot, mouseButton, x, y); |
| |
| if (annotationToolTipText != null) { |
| String prefix = new String(); |
| if (annotation.getName() != null) { |
| prefix = annotation.getName() + " : "; |
| } else { |
| prefix = "? : "; |
| } |
| |
| if (toolTipText != null) { |
| toolTipText += "\n" + prefix + annotationToolTipText; |
| } else { |
| toolTipText = new String(); |
| toolTipText += prefix + annotationToolTipText; |
| } |
| } |
| } |
| } |
| } |
| |
| return toolTipText; |
| } |
| |
| protected Point convertToCurrentImageCoordinates(MouseEvent e) { |
| Point point = this.imageDisplayComposite.convertMouseEventPositionToImagePosition(e); |
| Point currentImageSize = getImageSize(); |
| |
| int x = point.x; |
| int y = point.y; |
| |
| if (this.lastImageSnapshot != null && this.lastImageSnapshot.getImage() != null) { |
| CameraViewDisplayRotation cameraViewDisplayRotation = CameraViewDisplayRotation.NONE; |
| if (getCameraViewConfiguration() != null) { |
| cameraViewDisplayRotation = getCameraViewConfiguration().getCameraViewDisplayRotation(); |
| } |
| |
| double xRatio = 0.0; |
| double yRatio = 0.0; |
| |
| switch (cameraViewDisplayRotation.getValue()) { |
| case CameraViewDisplayRotation.NONE_VALUE: |
| xRatio = ((double) point.x / (double) currentImageSize.x); |
| yRatio = ((double) point.y / (double) currentImageSize.y); |
| break; |
| |
| case CameraViewDisplayRotation.CW_90_DEG_VALUE: |
| xRatio = (double) point.y / (double) currentImageSize.x; |
| yRatio = 1.0 - ((double) point.x / (double) currentImageSize.y); |
| break; |
| |
| case CameraViewDisplayRotation.CCW_90_DEG_VALUE: |
| xRatio = 1.0 - (double) point.y / (double) currentImageSize.x; |
| yRatio = (double) point.x / (double) currentImageSize.y; |
| break; |
| |
| case CameraViewDisplayRotation.CW_180_DEG_VALUE: |
| xRatio = 1.0 - ((double) point.x / (double) currentImageSize.x); |
| yRatio = 1.0 - ((double) point.y / (double) currentImageSize.y); |
| break; |
| |
| default: |
| break; |
| } |
| |
| x = (int) Math.round(xRatio * this.lastImageSnapshot.getImage().getWidth()); |
| y = (int) Math.round(yRatio * this.lastImageSnapshot.getImage().getHeight()); |
| |
| } |
| |
| return new Point(x, y); |
| } |
| } |