This commit is contained in:
gobbo_a
2017-03-17 11:14:30 +01:00
parent 9a2bb56569
commit 6565e930be
2 changed files with 152 additions and 161 deletions

View File

@@ -82,12 +82,12 @@ import org.apache.commons.math3.stat.descriptive.DescriptiveStatistics;
*
*/
public class ScreenPanel extends Panel {
final String CONFIG_FOLDER = "/afs/psi.ch/intranet/SF/Applications/config/camtool";
final String CONFIG_FOLDER = "/afs/psi.ch/intranet/SF/Applications/config/camtool_n";
final String CAMERA_DEVICE_NAME = "CurrentCamera";
boolean useCamtoolStats = true;
String userOverlaysConfigFile;
String userOverlaysConfigFile;
ColormapSource camera;
String cameraName;
String cameraConfigJson;
@@ -124,7 +124,6 @@ public class ScreenPanel extends Panel {
x_fit_gauss_function = getCamtoolDoubleArray("x_fit_gauss_function");
y_profile = getCamtoolDoubleArray("y_profile");
y_fit_gauss_function = getCamtoolDoubleArray("y_fit_gauss_function");
}
}
Data data;
@@ -172,7 +171,7 @@ public class ScreenPanel extends Panel {
if (App.hasArgument("usr_ov")) {
try {
userOverlaysConfigFile =App.getArgumentValue("usr_ov");
userOverlaysConfigFile = App.getArgumentValue("usr_ov");
} catch (Exception ex) {
ex.printStackTrace();
}
@@ -238,7 +237,7 @@ public class ScreenPanel extends Panel {
}
}
});
renderer.getPopupMenu().add(menuSaveStack);
renderer.getPopupMenu().addSeparator();
renderer.getPopupMenu().add(menuSetROI);
@@ -278,7 +277,7 @@ public class ScreenPanel extends Panel {
imageBufferOverlay.setAnchor(Overlay.ANCHOR_VIEWPORT_TOP_RIGHT);
if (MainFrame.isDark()) {
textState.setEnabled(true);
}
}
}
@Override
@@ -288,7 +287,7 @@ public class ScreenPanel extends Panel {
boolean direct = App.getArgumentValue("ct").equals("0") || App.getArgumentValue("ct").equalsIgnoreCase("false");
buttonCamtool.setSelected(!direct);
buttonDirect.setSelected(direct);
}
}
}
@Override
@@ -324,8 +323,8 @@ public class ScreenPanel extends Panel {
usingCamtool = buttonCamtool.isSelected();
updateCameraList();
comboCameras.setEnabled(true);
setComboCameraSelection(-1);
setComboCameraSelection(-1);
if (comboCameras.getModel().getSize() > 0) {
try {
if (App.hasArgument("cam")) {
@@ -339,67 +338,68 @@ public class ScreenPanel extends Panel {
}
startTimer(1000);
}
DefaultComboBoxModel getCameraListFromFolder(){
DefaultComboBoxModel getCameraListFromFolder() {
File[] cameraConfigFiles = new File[0];
cameraConfigFiles = IO.listFiles(CONFIG_FOLDER, new String[]{"json"});
Arrays.sort(cameraConfigFiles, (a, b) -> a.compareTo(b));
DefaultComboBoxModel model = new DefaultComboBoxModel();
for (File file : cameraConfigFiles) {
String prefix = IO.getPrefix(file);
if (!prefix.startsWith("#")) {
if (!prefix.startsWith("#") && !prefix.endsWith("_parameters")) {
model.addElement(prefix);
}
}
return model;
}
DefaultComboBoxModel getCameraListFromCamtool() throws IOException, InterruptedException{
DefaultComboBoxModel getCameraListFromCamtool() throws IOException, InterruptedException {
DefaultComboBoxModel model = new DefaultComboBoxModel();
Camtool camtool = new Camtool(CAMERA_DEVICE_NAME);
try{
Camtool camtool = new Camtool(CAMERA_DEVICE_NAME);
try {
camtool.initialize();
List<String> cameras = camtool.getCameras();
Collections.sort(cameras);
for (String camera:cameras){
for (String camera : cameras) {
model.addElement(camera);
}
//model.addElement(Camtool.SIMULATION);
} finally{
} finally {
camtool.close();
}
return model;
}
boolean updatingCameraSelection;
void setComboCameraSelection(Object selection){
void setComboCameraSelection(Object selection) {
updatingCameraSelection = true;
try{
try {
comboCameras.setSelectedItem(selection);
} finally{
} finally {
updatingCameraSelection = false;
}
}
boolean usingCamtool;
void updateCameraList(){
void updateCameraList() {
try {
String selected = (String) comboCameras.getSelectedItem();
DefaultComboBoxModel model= usingCamtool ? getCameraListFromCamtool() : getCameraListFromFolder();
DefaultComboBoxModel model = usingCamtool ? getCameraListFromCamtool() : getCameraListFromFolder();
if (App.hasArgument("cam")) {
String cam = App.getArgumentValue("cam");
if (model.getIndexOf(cam) < 0) {
model.addElement(cam);
}
}
}
comboCameras.setModel(model);
if (selected!=null){
if (selected != null) {
setComboCameraSelection(selected);
}
} catch (Exception ex) {
ex.printStackTrace();
} finally{
} finally {
updateStop();
}
}
@@ -432,96 +432,85 @@ public class ScreenPanel extends Panel {
public static class CameraConfig {
public HashMap kwargs;
public ArrayList args;
public ArrayList links;
public String type;
public HashMap<String, Object> state;
public CameraConfig image_source;
public HashMap<String, CameraConfig> subcomponents;
public HashMap camera;
public ArrayList<Integer> getCalibration() {
return (ArrayList<Integer>) state.get("calibration");
public HashMap getCalibration() {
return (HashMap) camera.get("calibration");
}
public ArrayList<Integer> getCalibrationRefMarker() {
return (ArrayList<Integer>) getCalibration().get("reference_marker");
}
public double getCalOffsetX() {
ArrayList<Integer> calibration = getCalibration();
ArrayList<Integer> calibration = getCalibrationRefMarker();
double ret = -(calibration.get(0) + calibration.get(2)) / 2;
return ret;
}
public double getCalOffsetY() {
ArrayList<Integer> calibration = getCalibration();
ArrayList<Integer> calibration = getCalibrationRefMarker();
double ret = -(calibration.get(1) + calibration.get(3)) / 2;
return ret;
}
public double getScaleX() {
ArrayList<Integer> calibration = getCalibration();
ArrayList<Integer> calibration = getCalibrationRefMarker();
double width = Math.abs(calibration.get(2) - calibration.get(0));
return getCalibrationWidth() / width;
}
public double getScaleY() {
ArrayList<Integer> calibration = getCalibration();
ArrayList<Integer> calibration = getCalibrationRefMarker();
double height = Math.abs(calibration.get(3) - calibration.get(1));
return getCalibrationHeight() / height;
}
public Double getCalibrationHeight() {
return (Double) state.get("calibration_height");
return (Double) getCalibration().get("reference_marker_height");
}
public Double getCalibrationWidth() {
return (Double) state.get("calibration_width");
return (Double) getCalibration().get("reference_marker_width");
}
public Double getCalibrationHorizontalAngle() {
return (Double) state.get("calibration_horizontal_angle");
return (Double) getCalibration().get("horizontal_camera_angle");
}
public Double getCalibrationVerticalAngle() {
return (Double) state.get("calibration_vertical_angle");
return (Double) getCalibration().get("vertical_camera_angle");
}
public Boolean getMirrorX() {
return (Boolean) state.get("mirror_x");
public boolean getMirrorX() {
Boolean ret = (Boolean) camera.get("mirror_x");
return (ret == null) ? false : ret;
}
public Boolean getMirrorY() {
return (Boolean) state.get("mirror_y");
public boolean getMirrorY() {
Boolean ret = (Boolean) camera.get("mirror_y");
return (ret == null) ? false : ret;
}
public Integer getRotate() {
return (Integer) state.get("rotate");
}
public ArrayList<Integer> getOrigin() {
return (ArrayList<Integer>) state.get("origin");
public int getRotate() {
Integer ret = (Integer) camera.get("rotate");
return (ret == null) ? 0 : ret;
}
public ArrayList<Integer> getRoi() {
return (ArrayList<Integer>) state.get("roi");
return (ArrayList<Integer>) camera.get("roi");
}
public Boolean getRoiEnable() {
if ((state.get("roi_enable") == null) || (state.get("roi") == null)) {
if ((camera.get("roi_enable") == null) || (camera.get("roi") == null)) {
return false;
}
return (Boolean) state.get("roi_enable");
}
public ArrayList<Double> getUnitSize() {
return (ArrayList<Double>) state.get("unit_size");
}
public Integer getRun() {
return (Integer) state.get("run");
return (Boolean) camera.get("roi_enable");
}
}
Thread devicesInitTask;
void setCamera(String cameraName) throws IOException, InterruptedException {
System.out.println("Setting camera: " + cameraName + " [" + (buttonCamtool.isSelected() ? "camtool" : "direct") + "]");
//renderer.removeOverlay(errorOverlay);
@@ -539,6 +528,7 @@ public class ScreenPanel extends Panel {
camera = null;
}
renderer.setDevice(null);
renderer.setShowReticle(false);
renderer.removeOverlays(fitOv);
renderer.removeOverlays(userOv);
@@ -548,8 +538,8 @@ public class ScreenPanel extends Panel {
boolean changed = !String.valueOf(cameraName).equals(this.cameraName);
this.cameraName = cameraName;
if (changed){
if ( (devicesInitTask!=null) && (devicesInitTask.isAlive())){
if (changed) {
if ((devicesInitTask != null) && (devicesInitTask.isAlive())) {
devicesInitTask.interrupt();
}
if (screen != null) {
@@ -559,12 +549,12 @@ public class ScreenPanel extends Panel {
if (filter != null) {
filter.close();
filter = null;
}
}
}
if (cameraName==null){
if (cameraName == null) {
return;
}
try {
Path configFile = Paths.get(CONFIG_FOLDER, cameraName + ".json");
cameraConfigJson = configFile.toFile().exists() ? new String(Files.readAllBytes(configFile)) : null;
@@ -582,8 +572,8 @@ public class ScreenPanel extends Panel {
} else {
if ((cameraConfigJson == null) && (buttonDirect.isSelected())) {
throw new Exception("Cannot open camera config file: " + configFile.toFile());
}
}
camera = new PsiCamera(CAMERA_DEVICE_NAME, cameraName);
config = (CameraConfig) JsonSerializer.decode(cameraConfigJson, CameraConfig.class);
camera.getConfig().flipHorizontally = config.getMirrorX();
@@ -655,14 +645,15 @@ public class ScreenPanel extends Panel {
public void onImage(Object o, BufferedImage bi, Data data) {
if (bi != null) {
if (firstImage) {
if ((renderer.getMode() == RendererMode.Zoom) || (renderer.getMode() == RendererMode.Fixed)) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
if ((renderer.getMode() == RendererMode.Zoom) || (renderer.getMode() == RendererMode.Fixed)) {
centralizeRenderer();
}
});
}
checkReticle();
}
});
firstImage = false;
}
renderer.setProfileSize(Math.min(bi.getWidth(), bi.getHeight()));
@@ -690,7 +681,7 @@ public class ScreenPanel extends Panel {
public void onError(Object o, Exception excptn) {
}
});
} catch (Exception ex) {
ex.printStackTrace();
showException(ex);
@@ -705,20 +696,20 @@ public class ScreenPanel extends Panel {
renderer.addOverlay(errorOverlay);
}
} finally {
checkReticle();
//checkReticle();
onTimer();
}
if (changed){
}
if (changed) {
comboScreen.setModel(new DefaultComboBoxModel());
comboFilter.setModel(new DefaultComboBoxModel());
//Parallelizing initialization
devicesInitTask = new Thread(() -> {
try {
screen = new DiscretePositioner("CurrentScreen", cameraName + ":SET_SCREEN1_POS", cameraName + ":GET_SCREEN1_POS");
screen.setMonitored(true);
screen.initialize();
screen.initialize();
DefaultComboBoxModel model = new DefaultComboBoxModel();
for (String pos : screen.getPositions()) {
model.addElement(pos);
@@ -737,7 +728,7 @@ public class ScreenPanel extends Panel {
try {
filter = new DiscretePositioner("CurrentFilter", cameraName + ":SET_FILTER", cameraName + ":GET_FILTER");
filter.setMonitored(true);
filter.initialize();
filter.initialize();
DefaultComboBoxModel model = new DefaultComboBoxModel();
for (String pos : filter.getPositions()) {
model.addElement(pos);
@@ -748,12 +739,11 @@ public class ScreenPanel extends Panel {
System.err.println(ex.getMessage());
filter = null;
}
comboFilter.setEnabled(filter != null);
comboFilter.setEnabled(filter != null);
valueFilter.setDevice(filter);
});
});
devicesInitTask.start();
}
}
@@ -850,37 +840,37 @@ public class ScreenPanel extends Panel {
}
updatingColormap = false;
}
boolean updatingCamtoolControls;
void updateCamtoolControls(){
if ((camera!=null) && (camera instanceof Camtool)) {
void updateCamtoolControls() {
if ((camera != null) && (camera instanceof Camtool)) {
updatingCamtoolControls = true;
try{
try {
checkBackground.setSelected(((Camtool) camera).getBackgroundSubtraction());
Double threshold = ((Camtool) camera).getThreshold();
checkThreshold.setSelected(threshold != null);
spinnerThreshold.setValue((threshold == null) ? 0 : threshold);
} catch (Exception ex) {
}
}
updatingCamtoolControls = false;
}
}
boolean isCameraStopped(){
if ((camera!=null) && (camera instanceof Camtool)) {
if (!((Camtool) camera).isPipelineStarted()){
boolean isCameraStopped() {
if ((camera != null) && (camera instanceof Camtool)) {
if (!((Camtool) camera).isPipelineStarted()) {
return true;
}
}
}
return ((camera == null) || (camera.isClosed()) || !buttonStop.isEnabled());
}
void updateStop(){
buttonStop.setEnabled(comboCameras.getSelectedItem()!=null);
buttonStop.setText(isCameraStopped() ? "Start": "Stop");
void updateStop() {
buttonStop.setEnabled(comboCameras.getSelectedItem() != null);
buttonStop.setText(isCameraStopped() ? "Start" : "Stop");
}
@Override
@@ -1130,6 +1120,7 @@ public class ScreenPanel extends Panel {
}
class UserOverlay {
String name;
Overlay obj;
String[] channels;
@@ -1139,7 +1130,7 @@ public class ScreenPanel extends Panel {
void parseUserOverlays() {
Properties userOverlays = new Properties();
userOverlayConfig = new ArrayList<>();
if (userOverlaysConfigFile!=null){
if (userOverlaysConfigFile != null) {
try {
try (FileInputStream in = new FileInputStream(getContext().getSetup().expandPath(userOverlaysConfigFile))) {
userOverlays.load(in);
@@ -2127,8 +2118,8 @@ public class ScreenPanel extends Panel {
}// </editor-fold>//GEN-END:initComponents
private void comboCamerasActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboCamerasActionPerformed
try {
if (!updatingCameraSelection){
try {
if (!updatingCameraSelection) {
if (!comboCameras.isEnabled()) {
throw new Exception("Invalid state");
}
@@ -2139,7 +2130,7 @@ public class ScreenPanel extends Panel {
new Thread(new Runnable() {
@Override
public void run() {
if (requestCameraListUpdate){
if (requestCameraListUpdate) {
requestCameraListUpdate = false;
try {
updateCameraList();
@@ -2258,24 +2249,24 @@ public class ScreenPanel extends Panel {
private void buttonGrabBackgroundActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonGrabBackgroundActionPerformed
try {
if ((camera!=null) && (camera instanceof Camtool)) {
if ((camera != null) && (camera instanceof Camtool)) {
if (SwingUtils.showOption(getTopLevel(), "Background", "Do you want to capture background now?", OptionType.YesNo) == OptionResult.Yes) {
boolean laserOn = getLaserState();
boolean rendering = (!camera.isClosed());
if (rendering){
if (rendering) {
camera.close();
}
if (laserOn) {
setLaserState(false);
}
try{
try {
System.out.println("Grabbing background for: " + cameraName);
((Camtool) camera).grabBackground(cameraName, 5);
} finally {
if (laserOn) {
setLaserState(true);
}
if (rendering){
if (rendering) {
comboCamerasActionPerformed(null);
}
updateStop();
@@ -2367,7 +2358,7 @@ public class ScreenPanel extends Panel {
}//GEN-LAST:event_buttonZoom05ActionPerformed
private void buttonCamtoolActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonCamtoolActionPerformed
if (!usingCamtool){
if (!usingCamtool) {
usingCamtool = true;
requestCameraListUpdate = true;
}
@@ -2375,21 +2366,21 @@ public class ScreenPanel extends Panel {
}//GEN-LAST:event_buttonCamtoolActionPerformed
private void buttonDirectActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonDirectActionPerformed
if (usingCamtool){
if (usingCamtool) {
usingCamtool = false;
requestCameraListUpdate = true;
requestCameraListUpdate = true;
}
comboCamerasActionPerformed(null);
}//GEN-LAST:event_buttonDirectActionPerformed
private void comboScreenActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_comboScreenActionPerformed
comboScreen.setEnabled(false);
new Thread(new Runnable() {
@Override
public void run() {
ChannelInteger setpoint = null;
try {
try {
int index = comboScreen.getSelectedIndex();
setpoint = new ChannelInteger(null, cameraName + ":SET_SCREEN1_POS");
setpoint.initialize();
@@ -2397,18 +2388,18 @@ public class ScreenPanel extends Panel {
setpoint.write(index);
//Must be threaded to control the laser because of sleep in setLaserState
/*
boolean laserOn = getLaserState();
if (laserOn) {
setLaserState(false);
}
try {
setpoint.write(index);
} finally {
if (laserOn) {
setLaserState(true);
}
}
*/
boolean laserOn = getLaserState();
if (laserOn) {
setLaserState(false);
}
try {
setpoint.write(index);
} finally {
if (laserOn) {
setLaserState(true);
}
}
*/
}
screen.read();
} catch (Exception ex) {
@@ -2417,7 +2408,7 @@ public class ScreenPanel extends Panel {
comboScreen.setEnabled(true);
if (setpoint != null) {
setpoint.close();
}
}
}
}
}).start();
@@ -2449,7 +2440,7 @@ public class ScreenPanel extends Panel {
}//GEN-LAST:event_buttonZoom2ActionPerformed
private void spinnerThresholdonChange(javax.swing.event.ChangeEvent evt) {//GEN-FIRST:event_spinnerThresholdonChange
if (updatingCamtoolControls){
if (updatingCamtoolControls) {
return;
}
try {
@@ -2458,31 +2449,31 @@ public class ScreenPanel extends Panel {
}
} catch (Exception ex) {
showException(ex);
updateCamtoolControls();
updateCamtoolControls();
}
}//GEN-LAST:event_spinnerThresholdonChange
private void checkBackgroundActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkBackgroundActionPerformed
if (updatingCamtoolControls){
if (updatingCamtoolControls) {
return;
}
try {
if ((camera instanceof Camtool) && (((Camtool) camera).isPipelineStarted())) {
((Camtool) camera).setBackgroundSubtraction(checkBackground.isSelected());
}
} catch (Exception ex) {
showException(ex);
updateCamtoolControls();
} catch (Exception ex) {
showException(ex);
updateCamtoolControls();
//There is a bug in camtool: it will flag bg extraction as on:
updatingCamtoolControls = true;
checkBackground.setSelected(false);
updatingCamtoolControls = false;
}
}//GEN-LAST:event_checkBackgroundActionPerformed
private void checkThresholdActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_checkThresholdActionPerformed
if (updatingCamtoolControls){
if (updatingCamtoolControls) {
return;
}
try {
@@ -2490,24 +2481,24 @@ public class ScreenPanel extends Panel {
spinnerThreshold.setEnabled(checkThreshold.isSelected());
((Camtool) camera).setThreshold(checkThreshold.isSelected() ? (Double) spinnerThreshold.getValue() : null);
}
} catch (Exception ex) {
} catch (Exception ex) {
showException(ex);
updateCamtoolControls();
updateCamtoolControls();
}
}//GEN-LAST:event_checkThresholdActionPerformed
private void buttonStopActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_buttonStopActionPerformed
try {
if (buttonStop.getText().equals("Stop")){
if ((camera!=null) && !camera.isClosed()){
if (buttonStop.getText().equals("Stop")) {
if ((camera != null) && !camera.isClosed()) {
camera.close();
}
} else {
if (isCameraStopped()){
} else {
if (isCameraStopped()) {
comboCamerasActionPerformed(null);
}
}
} catch (Exception ex) {
} catch (Exception ex) {
showException(ex);
} finally {
updateStop();