Google Answers Logo
View Question
 
Q: Extracting Still Pictures from iSight camera using Java on Mac OS X ( Answered,   1 Comment )
Question  
Subject: Extracting Still Pictures from iSight camera using Java on Mac OS X
Category: Computers > Programming
Asked by: ericb01-ga
List Price: $25.00
Posted: 09 Sep 2004 04:38 PDT
Expires: 09 Oct 2004 04:38 PDT
Question ID: 398810
Is there a way to programmatically extract still pictures from the
Apple iSight Firewire camera using the Java programming language on
the Mac OS X platform (more specifically PowerBook G4)?  If so, how? 
Sample Code?

Request for Question Clarification by googleexpert-ga on 09 Sep 2004 07:28 PDT
Yes, there certainly seems to be a way to extract still pictures from
iSight using Java.

Although which version of Java and OS X you're using might be an issue.

Which version of Java and OS X will you be using this Java program?

Thanks

-googleexpert

Clarification of Question by ericb01-ga on 09 Sep 2004 09:44 PDT
Hi GoogleExpert,

My operating system is: 10.3.5

My version of java is 1.4.2

java version "1.4.2_05"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_05-141)
Java HotSpot(TM) Client VM (build 1.4.2-38, mixed mode)

Hope this helps,
Eric

Request for Question Clarification by googleexpert-ga on 09 Sep 2004 22:28 PDT
Hi Eric,
I found sample code and compiled the Java code.
I ran into a problem.

Problem: No iSight Camera to test code against.

I would like to know if you want the sample code and
if that qualifies as an answer to your question.

Clarification of Question by ericb01-ga on 10 Sep 2004 05:26 PDT
Is it possible for you to share the compiled Java classes with me and
I can test it with my iSight camera before I mark this question as
answered?

My objective is to incorporate the Java code into a larger specialized
system so I will definitely need the source code.

If the compiled Java classes work for me, then I can answer this
question as answered and then you can give me the actual Java source
files.
Is that acceptable?

Request for Question Clarification by googleexpert-ga on 10 Sep 2004 06:12 PDT
Here's what I found.

First, copy and unzip /System/Library/Java/Extensions/QTJava.zip to your
testing  directory to bypass any classpath issues.

Source code for the VideoCapture class
-----------
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.awt.image.DataBuffer;
import java.awt.image.WritableRaster;
import quicktime.QTRuntimeException;
import quicktime.QTRuntimeHandler;
import quicktime.QTSession;
import quicktime.qd.PixMap;
import quicktime.qd.QDConstants;
import quicktime.qd.QDGraphics;
import quicktime.qd.QDRect;
import quicktime.std.StdQTConstants;
import quicktime.std.sg.SequenceGrabber;
import quicktime.std.sg.SGVideoChannel;
import quicktime.util.RawEncodedImage;

public class VideoCapture {
    private SequenceGrabber grabber;
    private SGVideoChannel channel;
    private RawEncodedImage rowEncodedImage;

    private int width;
    private int height;
    private int videoWidth;

    private int[] pixels;
    private BufferedImage image;
    private WritableRaster raster;

    public VideoCapture(int width, int height) throws Exception {
        this.width = width;
        this.height = height;
        try {
            QTSession.open();
            QDRect bounds = new QDRect(width, height);
            QDGraphics graphics = new QDGraphics(bounds);
            grabber = new SequenceGrabber();
            grabber.setGWorld(graphics, null);
            channel = new SGVideoChannel(grabber);
            channel.setBounds(bounds);
            channel.setUsage(StdQTConstants.seqGrabPreview);
            channel.settingsDialog();
            grabber.prepare(true, false);
            grabber.startPreview();
            PixMap pixMap = graphics.getPixMap();
            rowEncodedImage = pixMap.getPixelData();

            videoWidth = width + (rowEncodedImage.getRowBytes() - width * 4) / 4;
            pixels = new int[videoWidth * height];
            image = new BufferedImage(
                videoWidth, height, BufferedImage.TYPE_INT_RGB);
            raster = WritableRaster.createPackedRaster(DataBuffer.TYPE_INT,
                videoWidth, height,
                new int[] { 0x00ff0000, 0x0000ff00, 0x000000ff }, null);
            raster.setDataElements(0, 0, videoWidth, height, pixels);
            image.setData(raster);
            QTRuntimeException.registerHandler(new QTRuntimeHandler() {
                public void exceptionOccurred(
                        QTRuntimeException e, Object eGenerator,
                        String methodNameIfKnown, boolean unrecoverableFlag) {
                    System.out.println("what should i do?");
                }
            });
        } catch (Exception e) {
            QTSession.close();
            throw e;
        }
    }

    public void dispose() {
        try {
            grabber.stop();
            grabber.release();
            grabber.disposeChannel(channel);
            image.flush();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            QTSession.close();
        }
    }

    public int getWidth() {
        return width;
    }

    public int getHeight() {
        return height;
    }

    public int getVideoWidth() {
        return videoWidth;
    }

    public int getVideoHeight() {
        return height;
    }

    public void getNextPixels(int[] pixels) throws Exception {
        grabber.idle();
        rowEncodedImage.copyToArray(0, pixels, 0, pixels.length);
    }

    public Image getNextImage() throws Exception {
        grabber.idle();
        rowEncodedImage.copyToArray(0, pixels, 0, pixels.length);
        raster.setDataElements(0, 0, videoWidth, height, pixels);
        image.setData(raster);
        return image;
    }
}
URL: http://gogo.tea-nifty.com/interpot/java/
:

I also found pages that merely serve as possible reference guides:
http://rsb.info.nih.gov/ij/plugins/qt-capture.html
Note: had trouble compiling, probably needed the entire ImageJ package.

http://www.oreillynet.com/pub/wlg/2933
http://www.oreillynet.com/cs/user/view/cs_msg/40741
http://www.onjava.com/lpt/a/4318

http://lists.apple.com/archives/quicktime-java/2003/Dec/15/howtocropanimagecaptured.txt
Login/Password: archives/archives

Please let me know if you have any problems compiling.

-googleexpert

Request for Question Clarification by googleexpert-ga on 13 Sep 2004 15:54 PDT
Hi ericb01,
I would like to know if the java sources worked for you.
Please let me know if you need any help.

Thanks.

-googleexpert

Clarification of Question by ericb01-ga on 13 Sep 2004 18:01 PDT
Sorry I didn't get a chance to get back to you sooner.  Yes, the code
worked.  There is a configuration window that pops up to let me change
the settings of the camera.

Have you any idea which line of code controls that functionality so I
can optionally display this configuration window?  If not, I suppose
my question is answered.

I have another question about capturing sound from an iSight camera. 
Not sure if that was convered on the website you referenced.

Thanks,
Eric
Answer  
Subject: Re: Extracting Still Pictures from iSight camera using Java on Mac OS X
Answered By: googleexpert-ga on 13 Sep 2004 19:48 PDT
 
Hi,
Thanks for getting back to me and for informing me of the working code.

About your popup configuration window, I believe commenting or taking
out the following line will not display a popup window:
/**** function for iSight Popup window 
channel.settingsDialog();
****/

Reference:
http://developer.apple.com/documentation/Java/Reference/1.4.1/Java141API_QTJ/quicktime/std/sg/SGChannel.html#settingsDialog()

Now, about capturing sound with Java, here's what I found(source code included):

Capturing Audio with Java Sound API:
http://java.sun.com/developer/JDCTechTips/2002/tt0319.html#tip1

Java Sound, Getting Started, Part 2, Capture Using Specified Mixer:
http://www.developer.com/java/other/article.php/1579071

Java Sound Resources: Examples: Audio Playback and Recording:
http://www.jsresources.org/examples/audio_playing_recording.html


I hope that helps you.

If you have anymore questions, please don't hesitate to ask me.

Thanks again.

-googleexpert

Clarification of Answer by googleexpert-ga on 13 Sep 2004 20:08 PDT
[search strategy]

"import java" isight
Comments  
Subject: Re: Extracting Still Pictures from iSight camera using Java on Mac OS X
From: tjbates-ga on 30 Jan 2005 09:09 PST
 
I am doing a final year project and was interested by this technique
of image capture and was wondering if you have had any luck getting it
to work?

I am trying to capture images from iSight convert to binary image then
input the data to an Multi-Layer Perceptron for anaysis and hopefully
it will return a output comparible to the input image. e.g. a picture
of an apple is shown to iSight and the MLP will look at the shape and
say its an apple.

If you managed to capute images from iSight i would be graful if you
could let me know how you did it and if you have any source that would
help me considerably.

Thanks,

Tom Bates

Important Disclaimer: Answers and comments provided on Google Answers are general information, and are not intended to substitute for informed professional medical, psychiatric, psychological, tax, legal, investment, accounting, or other professional advice. Google does not endorse, and expressly disclaims liability for any product, manufacturer, distributor, service or service provider mentioned or any opinion expressed in answers or comments. Please read carefully the Google Answers Terms of Service.

If you feel that you have found inappropriate content, please let us know by emailing us at answers-support@google.com with the question ID listed above. Thank you.
Search Google Answers for
Google Answers  


Google Home - Answers FAQ - Terms of Service - Privacy Policy