Package

Source Code of GetImage

//
//  GetImage.java
//  VideoAnnotate
//
//  Created by Michael D. Fischer on 27/07/2006.
//  Copyright (c) 2006, Centre for Social Anthropology and Computing,
//  University of Kent. All rights reserved.
//
//
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions
//  are met:
//
//  Redistributions of source code must retain the above copyright
//  notice, this list of conditions and the following disclaimer.
//  Redistributions in binary form must reproduce the above copyright
//  notice, this list of conditions and the following disclaimer in the
//  documentation and/or other materials provided with the distribution.
//  Neither the name of the Centre for Social Anthropology and Computing,
//  University of Kent nor the names of its contributors may be used
//  to endorse or promote products derived from this software without
//  specific prior written permission.  THIS SOFTWARE IS PROVIDED BY THE
//  COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED
//  WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
//  DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS
//  BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY,
//  OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
//  OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
//  OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
//  SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
// 


import quicktime.*;
//import quicktime.app.*;
//import quicktime.app.players.*;
//import quicktime.app.display.*;
import quicktime.io.*;
import quicktime.std.*;
import quicktime.std.movies.*;
import quicktime.qd.*;
import quicktime.std.movies.media.*;
import quicktime.std.image.*;
import quicktime.app.view.GraphicsImporterDrawer;
import quicktime.app.view.QTImageProducer;
import java.awt.Dimension;
import java.awt.Image;
import java.awt.Toolkit;



public class GetImage {
 
  public GetImage() {
   
  }
 
  public void getImageFromMovie(Movie theMovie, String offsetTime) {
    try {
   
      QTSession.open();
    System.out.println(theMovie.getBounds());
   
    int theTime = (int) (Long.parseLong(offsetTime) * theMovie.getTimeScale());
    System.out.println("theTime: " + theTime);
    Pict pictImage = theMovie.getPict(theTime);
   
    System.out.println("PICT: " + pictImage);
   
    // Optionally you could just use the poster
    // image if it has been set by the person
    // encoding the video
    //pictImage = theMovie.getPosterPict();
    //System.out.println("PICT: " + pictImage);
   
    // Now encode the Macintosh PICT as a JPEG or PNG or ....
   
    quicktime.std.image.GraphicsExporter graphicsExporter =
      new quicktime.std.image.GraphicsExporter(
                           StdQTConstants.kQTFileTypeJPEG);
   
    graphicsExporter.setInputPicture(pictImage);
   
   
    QTFile exportFile = new QTFile("StillImage.jpg");
    graphicsExporter.setOutputFile(exportFile);
    System.out.println("graphicsExporter: " + graphicsExporter);
   
    // Do the export
    int size = graphicsExporter.doExport();
   
    byte[] jpegBytes = graphicsExporter.GraphicsExportReadOutputData(0, size);
    System.out.println("jpegBytes: " + jpegBytes);
   
   
   
    QTSession.close();
   
  } catch (Exception e) {
    QTSession.close();
    System.out.println(e);
    // System.exit(0);
  }
 
}
 
  public void getImageFromMovie(String url, String offsetTime) {
   
    try {
     
      QTSession.open();
      DataRef ref = new DataRef(url);
      System.out.println("DataRef: " + ref);
      if (ref == null) return;
     
      //Movie theMovie = Movie.fromDataRef(ref,quicktime.std.StdQTConstants4.newMovieAsyncOK);
Movie theMovie = Movie.fromDataRef(ref, 0);
System.out.println(theMovie.getBounds());

int theTime = Integer.parseInt(offsetTime) * theMovie.getTimeScale();
System.out.println("theTime: " + theTime);
Pict pictImage = theMovie.getPict(theTime);

System.out.println("PICT: " + pictImage);

// Optionally you could just use the poster
// image if it has been set by the person
// encoding the video
//pictImage = theMovie.getPosterPict();
//System.out.println("PICT: " + pictImage);

// Now encode the Macintosh PICT as a JPEG or PNG or ....

quicktime.std.image.GraphicsExporter graphicsExporter =
new quicktime.std.image.GraphicsExporter(
                     StdQTConstants.kQTFileTypeJPEG);

graphicsExporter.setInputPicture(pictImage);


QTFile exportFile = new QTFile("StillImage.jpg");
graphicsExporter.setOutputFile(exportFile);
System.out.println("graphicsExporter: " + graphicsExporter);

// Do the export
int size = graphicsExporter.doExport();

byte[] jpegBytes = graphicsExporter.GraphicsExportReadOutputData(0, size);
System.out.println("jpegBytes: " + jpegBytes);



QTSession.close();

    } catch (Exception e) {
      QTSession.close();
      System.out.println(e);
      // System.exit(0);
    }
  }



public static void main(String args[]) {
  if (args.length < 2) {
    System.out.println("You need to specify a URL and a location time in seconds when launching");
    return;
  }
  GetImage instance = new GetImage();
  instance.getImageFromMovie(args[0], args[1]);
}

/**
* updates the frame content from the Pict
*/
public static Image pict2Image (Pict itsContentPict) throws QTException {
  if (itsContentPict != null) {
    byte[] newPictBytes = new byte [(itsContentPict.getSize()) + 512];
    itsContentPict.copyToArray (0,newPictBytes,512,newPictBytes.length - 512);
    Pict displayPict = new Pict (newPictBytes);
    DataRef ref = new DataRef (displayPict,StdQTConstants.kDataRefQTFileTypeTag,"PICT");
    GraphicsImporter gi = new GraphicsImporter(StdQTConstants.kQTFileTypePicture);
    gi.setDataReference (ref);
    GraphicsImporterDrawer gid = new GraphicsImporterDrawer (gi);
    // Dimension imageSize = new Dimension (getPictRect().getWidth(), getPictRect().getHeight());
    Dimension imageSize = new Dimension (120, 80);
    QTImageProducer ip = new QTImageProducer (gid, imageSize);
    Image javaImage = Toolkit.getDefaultToolkit().createImage (ip);
    //ImagePanel imagePanel = new ImagePanel(javaImage);
    //getContentPane().add(imagePanel, BorderLayout.CENTER);
    //setSize(imageSize);
    // setVisible(true);
    return javaImage;
  } else {
    return null;
  }
}

public static Image getFrame(Movie theMovie, String offsetTime, boolean thumb) {
  try {
   
//    QTSession.open();
    System.out.println(theMovie.getBounds());
   
    int theTime = Integer.parseInt(offsetTime) * theMovie.getTimeScale();
    System.out.println("theTime: " + theTime);
    Pict pictImage = null;
    if (thumb) pictImage = theMovie.getPict(theTime).makeThumbnail(32); // 80x80
    else  pictImage = theMovie.getPict(theTime);
    System.out.println("PICT: " + pictImage);
   
    return pict2Image(pictImage);
   
    // Optionally you could just use the poster
    // image if it has been set by the person
    // encoding the video
    //pictImage = theMovie.getPosterPict();
    //System.out.println("PICT: " + pictImage);
   
//    QTSession.close();
   
  } catch (Exception e) {
//    QTSession.close();
    System.out.println(e);
    // System.exit(0);
  }
  return null;
 
}

public static Image getFrame(Movie theMovie, int offsetTime, boolean thumb) {
  try {
   
    //    QTSession.open();
    System.out.println(theMovie.getBounds());
   
    // int theTime = Integer.parseInt(offsetTime) * theMovie.getTimeScale();
    // System.out.println("theTime: " + theTime);
    Pict pictImage = null;
    if (thumb) pictImage = theMovie.getPict(offsetTime).makeThumbnail(32); // 80x80
    else  pictImage = theMovie.getPict(offsetTime);
    System.out.println("PICT: " + pictImage);
   
    return pict2Image(pictImage);
   
    // Optionally you could just use the poster
    // image if it has been set by the person
    // encoding the video
    //pictImage = theMovie.getPosterPict();
    //System.out.println("PICT: " + pictImage);
   
    //    QTSession.close();
   
  } catch (Exception e) {
    //    QTSession.close();
    System.out.println(e);
    // System.exit(0);
  }
  return null;
 
}

public static Image getSelectedFrame(Movie theMovie, MovieSelection ms, long offset, boolean thumb) {
  try {
   
    //    QTSession.open();
    System.out.println(theMovie.getBounds());
    int scale = ms.getSelectionScale();
    if (offset > ms.getSelectionDuration()) offset = ms.getSelectionDuration();
    int theTime = (int) (ms.getSelectionStart() + offset);
    // int theTime = Integer.parseInt(offsetTime) * theMovie.getTimeScale();
    // System.out.println("theTime: " + theTime);
    Pict pictImage = null;
    if (thumb) pictImage = theMovie.getPict(theTime).makeThumbnail(32); // 80x80
    else  pictImage = theMovie.getPict(theTime);
    System.out.println("PICT: " + pictImage);
   
    return pict2Image(pictImage);
   
    // Optionally you could just use the poster
    // image if it has been set by the person
    // encoding the video
    //pictImage = theMovie.getPosterPict();
    //System.out.println("PICT: " + pictImage);
   
    //    QTSession.close();
   
  } catch (Exception e) {
    //    QTSession.close();
    System.out.println(e);
    // System.exit(0);
  }
  return null;
 
}

TOP

Related Classes of GetImage

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.