/** Example for using Sequence Grabbers DataProc to display video in java From: Jochen Broz > Date: Sun, 13 Nov 2005 01:07:31 +0100 In February I presented a way to display live video image from sequence grabber, which used the preview mode of the sequence grabber. See http://lists.apple.com/archives/quicktime-java/2005/Feb/ msg00062.html for details. In March Apple released an example which shows how to use the Quicktime Sequence Grabber and com.apple.eawt.CocoaComponent to display video in a QuickDraw port (Sub class of NSQuickDrawView) in a Java canvas running on Mac OS X. See http://developer.apple.com/samplecode/QDCocoaComponent/QDCocoaComponent.html for details. The main difference is, that they use the DataProc of the sequence grabber to push the video data into the display component. But all interesting parts are written in Objective-C. Here I would like to present a full java example, which uses a similar way to bring live video image onto the screen. Jochen Broz */ // DataProcTest.java // DataProcTest // // Created by Jochen Broz on 14.08.05. // Copyright (c) 2005 Jochen Broz. All rights reserved. import java.util.*; import quicktime.*; import quicktime.std.sg.*; import quicktime.std.*; import quicktime.qd.*; import quicktime.util.*; import quicktime.io.*; import quicktime.std.image.*; import quicktime.std.movies.*; import quicktime.std.qtcomponents.*; import quicktime.app.view.*; import java.awt.*; import java.awt.image.*; import javax.swing.*; public class DataProcTest { public static void main (String args[]) { try{ QTSession.open(); // Setting up the Sequence Grabber final SequenceGrabber sg = new SequenceGrabber(); final SGVideoChannel vc = new SGVideoChannel(sg); final QDRect cameraImageSize = new QDRect(320, 240); // vc.getSrcVideoBounds(); final QDGraphics gWorld=new QDGraphics(cameraImageSize); sg.setGWorld(gWorld, null); vc.setBounds(cameraImageSize); vc.setUsage(quicktime.std.StdQTConstants.seqGrabRecord ); vc.setFrameRate(0); final int myCodec = quicktime.std.StdQTConstants.kComponentVideoCodecType ; vc.setCompressorType(myCodec); // Setting up the buffered image 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); // Setting up a component, capable of displaying the image class MyComp extends Component{ public void paint(Graphics g){ super.paint(g); g.drawImage(image, 0, 0, this); }; }; final MyComp ret = new MyComp(); Frame myFrame = new Frame("Test"); myFrame.setBounds(100, 100, cameraImageSize.getWidth(), cameraImageSize.getHeight()); myFrame.add(ret); myFrame.show(); //Defining the data procedure which pushes the data into the image SGDataProc myDataProc = new SGDataProc(){ DSequence ds = null; final Matrix idMatrix=new Matrix(); byte[] rawData = new byte[QTImage.getMaxCompressionSize(gWorld, gWorld.getBounds(), 0, quicktime.std.StdQTConstants.codecLowQuality, myCodec, CodecComponent.anyCodec)]; RawEncodedImage ri = null; public int execute(SGChannel chan, QTPointerRef dataToWrite, int offset, int chRefCon, int time, int writeType){ if (chan instanceof SGVideoChannel) try{ ImageDescription id = vc.getImageDescription (); if(rawData==null) rawData = new byte [dataToWrite.getSize()]; RawEncodedImage ri = new RawEncodedImage (rawData); dataToWrite.copyToArray(0, rawData, 0, dataToWrite.getSize()); if(ds==null){ ds = new DSequence(id, ri, gWorld, cameraImageSize, idMatrix, null, 0, quicktime.std.StdQTConstants.codecNormalQuality, CodecComponent.anyCodec); }else{ ds.decompressFrameS(ri, quicktime.std.StdQTConstants.codecNormalQuality); } gWorld.getPixMap().getPixelData ().copyToArray(0, pixelData, 0, pixelData.length); ret.repaint(); return 0; }catch(Exception ex){ ex.printStackTrace(); return 1; } else return 1; } }; sg.setDataProc(myDataProc); // Preparing for output sg.setDataOutput(null, quicktime.std.StdQTConstants.seqGrabDontMakeMovie); sg.prepare(false, true); sg.startRecord(); // setting up a thread, to idle the sequence grabber Runnable idleCamera = new Runnable(){ public void run(){ try{ while(true){ sg.idleMore(); sg.update(null); } }catch(Exception ex){ ex.printStackTrace(); } } }; (new Thread(idleCamera)).start(); }catch(Exception ex){ ex.printStackTrace(); QTSession.close(); } } }