import ij.*;
import ij.plugin.PlugIn;
import ij.text.*;
import java.awt.*;
import java.awt.event.*;
import java.awt.image.*;
import java.io.*;
import java.util.*;
import quicktime.qd.*;
import quicktime.*;
import quicktime.util.*;
import quicktime.io.*;
import quicktime.std.StdQTConstants;
import quicktime.std.sg.*;
import quicktime.app.sg.SGDrawer;
import quicktime.std.movies.*;
import quicktime.std.movies.media.UserData;
import quicktime.app.display.QTCanvas;
import quicktime.app.image.QTImageProducer;
import quicktime.app.image.ImagePresenter;
public class MQTCapture extends Frame implements StdQTConstants, ActionListener,
WindowListener {
private int kWidth;
private int kHeight;
private QTCanvas myQTCanvas;
private SGVideoChannel mVideo;
private SGDrawer mDrawable;
private SequenceGrabber mGrabber;
private QTFile mFile;
private Movie mMovie;
private Carousel_Imaging thePlugIn;
private Button cancelButton, startButton, cancelGrabButton;
private ImageProducer imgProducer;
private Pict pict;
private ImagePresenter imgPresenter;
private int grabIndex, repeatIndex=0;
private SequenceGrabber GrabGrabber;
private boolean DEBUG; private TextArea textArea;
public MQTCapture (String title, Carousel_Imaging thePlugIn) {
super (title);
this.thePlugIn = thePlugIn;
DEBUG = true;
try {
QTSession.open();
mGrabber = new SequenceGrabber();
mVideo = new SGVideoChannel(mGrabber);
QDRect bounds = mVideo.getSrcVideoBounds();
if (IJ.altKeyDown()) {
kWidth = bounds.getWidth()/2; kHeight = bounds.getHeight()/2;
myQTCanvas = new QTCanvas(QTCanvas.kPerformanceResize, 0.5f, 0.5f);
} else {
kWidth = bounds.getWidth(); kHeight = bounds.getHeight();
myQTCanvas = new QTCanvas(QTCanvas.kPerformanceResize, 1f, 1f);
}
Dimension screen = Toolkit.getDefaultToolkit().getScreenSize();
if (kHeight>(screen.height-40)) { kWidth =640;
kHeight = 480;
}
kWidth =640;
kHeight = 480;
myQTCanvas.setMaximumSize (new Dimension (bounds.getWidth(), bounds.getHeight()));
myQTCanvas.setPreferredSize (new Dimension (kWidth, kHeight));
myQTCanvas.setSize (new Dimension (kWidth, kHeight));
setLayout (new BorderLayout());
add ("Center", myQTCanvas);
Panel buttons = new Panel();
cancelButton = new Button(" Cancel Plugin ");
buttons.add(cancelButton);
cancelButton.addActionListener(this);
startButton = new Button (" Start Grab ");
cancelGrabButton = new Button (" Cancel Grab ");
buttons.add(cancelGrabButton);
cancelGrabButton.addActionListener(this);
buttons.add(startButton);
startButton.addActionListener(this);
add("South", buttons);
addWindowListener(this);
if (DEBUG) {
Panel p = new Panel();
textArea = new TextArea("Messages\n", 10, 60);
p.add(textArea);
add("North", p);
}
} catch (QTException e) {
int errorCode = e.errorCode();
String s = null;
if (errorCode == -9405){
s = "Is ADVC-100 swtiched on?\nShould be a blue light indicating power is on.\nQuit ImageJ and try again.\nIgnore all other errors.";
} else {
s = "Problem establishing the Image Stream from the camera.\nCheck all connections and make sure all components have power.\nQuit and restart ImageJ.\nIf this problem persists restart the computer.";
}
new TextWindow("Image Stream Error", s, 400, 300);
}
IJ.register(MQTCapture.class);
}
public void actionPerformed (ActionEvent event) {
if (event.getSource () == startButton) {
thePlugIn.tap();
}
else if (event.getSource()==cancelButton) {
thePlugIn.close();
}
else {
thePlugIn.close();
}
}
public void reportMessage(String message) {
if(DEBUG) {
textArea.append(message);
}
}
public void windowOpened(WindowEvent e) {
try{
mVideo.setBounds (new QDRect(kWidth, kHeight));
mVideo.setUsage (seqGrabPreview); mDrawable = new SGDrawer(mVideo);
myQTCanvas.setClient(mDrawable,true);
mGrabber.prepare(true,true);
mGrabber.startPreview();
if (DEBUG) {textArea.append("QuickTime started just fine.\n");}
} catch (Exception ee) {
printStackTrace(ee);
}
}
public void addMessage(String message) {
textArea.append(message + "\n");
}
void printStackTrace(Exception e) {
CharArrayWriter caw = new CharArrayWriter();
PrintWriter pw = new PrintWriter(caw);
e.printStackTrace(pw);
String s = caw.toString();
new TextWindow("Exception", s, 400, 300);
}
public Image grabFrame() {
Image img = null;
try {
pict = mGrabber.grabPict(new QDRect(kWidth, kHeight), 0, grabPictOffScreen);
imgPresenter = ImagePresenter.fromPict(pict);
QDRect rect = imgPresenter.getDisplayBounds();
Dimension d = new Dimension(rect.getWidth(), rect.getHeight());
imgProducer = new QTImageProducer (imgPresenter, d);
img = createImage (imgProducer);
grabIndex++;
} catch (QTException qte) {qte.printStackTrace();}
return img;
}
public void shutDown() { myQTCanvas.removeClient();
QTSession.close();
setVisible(false);
dispose();
}
public Carousel_Imaging getThePlugIn() {
return thePlugIn;
}
public void windowIconified (WindowEvent e) {}
public void windowClosing (WindowEvent e) {
shutDown ();}
public void windowClosed (WindowEvent e) {
shutDown ();}
public void windowDeiconified (WindowEvent e) {}
public void windowActivated (WindowEvent e) {}
public void windowDeactivated (WindowEvent e) {}
}