Mailing Lists: Apple Mailing Lists
Image of Mac OS face in stamp
Re: Using QT to Capture iSight Image?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Using QT to Capture iSight Image?



Hi Joshua!

Can I use Quicktime Java to grab a video frame from the iSight camera on my Mac? I found the SGCapture2Disk example, but that's grabbing a movie. If someone could point me toward a simpler "take a picture" example (ideally into a BufferedImage), I'd really appreciate it.

-Joshua

yes, you can use QTJava to capture a single picture from your camera. In order to do that, you have to
- set up a SequenceGrabber
- supply the sequence grabber with a QDGraphics to store the image data
- grab the picture for example with Pict.fromSequenceGrabber()


If you are happy with the Picture, you are finished. If you really want to transfer the Quicktime image to a Java AWT BufferedImage, there is some work left. The basic idea is to create a Bufferede image from the raw data used for the QT-Picture.

Have a look at the following code, which works fine on my G4 PowerBook.

With best regards

Jochen Broz


import java.util.*;
import quicktime.*;
import quicktime.std.sg.*;
import quicktime.qd.*;
import quicktime.app.view.*;
import java.awt.*;
import java.awt.image.*;


public class Capture {

public static void main (String args[]) {
try{
QTSession.open();

// Configure the camera
QDRect cameraImageSize = new QDRect(640,480);
QDGraphics gWorld;
if ( quicktime.util.EndianOrder.isNativeLittleEndian() ) {
gWorld = new QDGraphics(QDConstants.k32BGRAPixelFormat, cameraImageSize);
} else {
gWorld = new QDGraphics(QDGraphics.kDefaultPixelFormat, cameraImageSize);
}
SequenceGrabber sg = new SequenceGrabber();
sg.setGWorld(gWorld,null);

// Grab the picture
Pict pict = Pict.fromSequenceGrabber(sg,cameraImageSize,0,0);

// If you are happy with a Quicktime Pict object - thats all. You might save it
// to disk for example using the pict.writeToFile()-Method

// If you want to have a BufferedImage, there is some work left:
int size = gWorld.getPixMap().getPixelData().getSize();
int intsPerRow = gWorld.getPixMap().getPixelData().getRowBytes()/4;
size = intsPerRow*cameraImageSize.getHeight();
final int[] pixelData = new int[size];
DataBuffer db = new DataBufferInt(pixelData, size);
ColorModel colorModel = new DirectColorModel(32, 0x00ff0000, 0x0000ff00, 0x000000ff);
int[] masks= {0x00ff0000, 0x0000ff00, 0x000000ff};
WritableRaster raster = Raster.createPackedRaster(db, cameraImageSize.getWidth(), cameraImageSize.getHeight(), intsPerRow, masks, null);
final BufferedImage image = new BufferedImage(colorModel, raster, false, null);

// Copy Picture data to the raw image
gWorld.getPixMap().getPixelData().copyToArray(0, pixelData, 0, pixelData.length);

// Fine - now we want to see the picture:
Frame myFrame = new Frame("Snapshot");
myFrame.add(
new Component(){
public void paint(Graphics g){
super.paint(g);
g.drawImage(image, 0, 0, this);
};
}
);
myFrame.setBounds(100, 100, cameraImageSize.getWidth(), cameraImageSize.getHeight());
myFrame.show();

}catch(Exception ex){
System.err.println("Got Exception:");
ex.printStackTrace();
}

QTSession.close();
}
}


_______________________________________________
Do not post admin requests to the list. They will be ignored.
QuickTime-java mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden




Visit the Apple Store online or at retail locations.
1-800-MY-APPLE

Contact Apple | Terms of Use | Privacy Policy

Copyright © 2011 Apple Inc. All rights reserved.