chetan_8187 0 Newbie Poster

I am developing an application in Java that grab Image from the video.
I have the following code that grabs a frame every given no. of seconds but i want an Image afte each click
Even the given code does' nt work
Plz help its urgent
Thanks in advance
:

void grabMethod2(File file){
        if (videoComponent != null) {
            remove(videoComponent);
        }
        if (controlComponent != null) {
            remove(controlComponent);
        }
        if (play != null) {
            play.close();
            play = null;
        }
        if (prcsr != null) {
            prcsr.close();
            prcsr = null;
        }

        try {
            prcsr = Manager.createProcessor(file.toURL());

            sw = new StateWaiter(prcsr);
            prcsr.addControllerListener(sw);

            // Put the Processor into configured state.
            prcsr.configure();
            if (!sw.waitForState(prcsr.Configured)) {
                lStatus.setText("Failed to configure the processor.");
                return;
            }

            // So I can use it as a player.
            prcsr.setContentDescriptor(null);

            // Obtain the track controls.
            TrackControl tc[] = prcsr.getTrackControls();

            if (tc == null) {
                lStatus.setText("Failed to obtain track controls from the processor.");
                return;
            }

            // Search for the track control for the video track.
            TrackControl videoTrack = null;
            VideoFormat vformat = null;;

            for (int i = 0; i < tc.length; i++) {
                if (tc[i].getFormat() instanceof VideoFormat) {
                    vformat = (VideoFormat)tc[i].getFormat();
                    videoTrack = tc[i];
                    break;
                }
            }

            if (videoTrack == null) {
                lStatus.setText("The input media does not contain a video track.");
                return;
            }

            // Instantiate and set the frame access codec to the data flow path.
            try {
                int grabe=Integer.parseInt(choice2.getSelectedItem());
                ImageLoader.ImageWriterWrapper iww=(ImageLoader.ImageWriterWrapper)writers.get(choice1.getSelectedIndex());
                File dir=new File(tfOut.getText());
                dir.mkdirs();
                Codec codec[] = {new FrameWriterCodec(grabe,iww,vformat,imgload,dir)};
                videoTrack.setCodecChain(codec);
            }
            catch (UnsupportedPlugInException upe) {
                lStatus.setText("The process does not support effects.");
                return;
            }

            // Realize the processor.
            prcsr.prefetch();
            if (!sw.waitForState(prcsr.Prefetched)) {
                lStatus.setText("Failed to realize the processor.");
                return;
            }

            // Display the visual & control component if there's one.

            setLayout(new BorderLayout());

            videoComponent = prcsr.getVisualComponent();
            if(videoComponent!=null){
                add(videoComponent, BorderLayout.CENTER);
            }
            controlComponent = prcsr.getControlPanelComponent();
            if(controlComponent!=null){
                add(controlComponent, BorderLayout.SOUTH);
            }

            GainControl gc = prcsr.getGainControl();
            if(gc!=null){
                gc.setMute(true);
            }

            // Start the processor.
            prcsr.start();
            lStatus.setText("Grabbing frames...");

        }
        catch (Exception ex) {
            lStatus.setText(ex.getMessage());
        }

        validate();
        doLayout();
        pack();

    }




    void saveImage(Image img, File dir,ImageLoader.ImageWriterWrapper iwwrap){
        int in=1;
        if(img !=null){
            BufferedImage bimg=null;
            if (! (img instanceof BufferedImage)) {
                bimg = new BufferedImage(img.getWidth(null),
                    img.getHeight(null), BufferedImage.TYPE_INT_RGB);
                Graphics g = bimg.getGraphics();
                g.drawImage(img, 0, 0, null);
                g.dispose();
                img = bimg;
            }

         /*   String name=""+frameNum;
            while(name.length()<6){
                name="0"+name;
            }*/

           String name= "Image0"+in+"." + iwwrap.getFormat();
            imgload.saveImage( (BufferedImage) img, new File(dir,name),iwwrap.getWriter());
            if(bimg!=null){
                bimg.flush();
        }            


        }in=in+1;
    }


    /*********************************************************
     * Inner class.
     *
     * A pass-through codec to access to individual frames.
     *********************************************************/

    public class FrameWriterCodec implements Codec {

        ImageLoader.ImageWriterWrapper iwwrap=null;
        int grabEvery=0;
        BufferToImage b2i;
        ImageLoader imgload;
        File dir;

        public FrameWriterCodec(int grabEvery, ImageLoader.ImageWriterWrapper writer, VideoFormat format, ImageLoader imgLoad, File dir){
            iwwrap= writer;
            this.grabEvery=grabEvery;
            b2i = new BufferToImage(format);
            imgload=imgLoad;
            this.dir=dir;
        }

        /**
         * Callback to access individual video frames.
         */
        void accessFrame(Buffer frame) {
            long frameNum = frame.getSequenceNumber();
            if(frameNum%grabEvery==0){
                lStatus.setText("Grabbing frame "+frameNum+" using alt method");
                Image img = b2i.createImage(frame);
                if(img == null){
                    Format f = frame.getFormat();
                    if(f instanceof VideoFormat){
                        b2i = new BufferToImage( (VideoFormat) f);
                        img = b2i.createImage(frame);
                    }
                }

                saveImage(img,dir,iwwrap);
                if(img!=null){
                    img.flush();
                }
            }
        }

        /**
         * The code for a pass through codec.
         */

        // We'll advertize as supporting all video formats.
        protected Format supportedIns[] = new Format[] {
            new RGBFormat()
        };



        // We'll advertize as supporting all video formats.
        protected Format supportedOuts[] = new Format[] {
            new VideoFormat(null)
        };

        Format input = null, output = null;

        public String getName() {
            return "Frame Grabber Codec";
        }

        // No op.
        public void open() {
        }

        // No op.
        public void close() {
        }

        // No op.
        public void reset() {
        }

        public Format[] getSupportedInputFormats() {
            return supportedIns;
        }

        public Format[] getSupportedOutputFormats(Format in) {
            if (in == null)
                return supportedOuts;
            else {
                // If an input format is given, we use that input format
                // as the output since we are not modifying the bit stream
                // at all.
                Format outs[] = new Format[1];
                outs[0] = in;
                return outs;
            }
        }

        public Format setInputFormat(Format format) {
            input = format;
            return input;
        }

        public Format setOutputFormat(Format format) {
            output = format;
            return output;
        }

        public int process(Buffer in, Buffer out) {

            // This is the "Callback" to access individual frames.
            accessFrame(in);

            // Swap the data between the input & output.
            Object data = in.getData();
            in.setData(out.getData());
            out.setData(data);

            // Copy the input attributes to the output
            out.setFormat(in.getFormat());
            out.setLength(in.getLength());
            out.setOffset(in.getOffset());

            return BUFFER_PROCESSED_OK;
        }

        public Object[] getControls() {
            return new Object[0];
        }

        public Object getControl(String type) {
            return null;
        }
    }





    class StateWaiter implements ControllerListener {
        Object waitSync = new Object();
        boolean stateTransitionOK = true;
        Processor p;

        public StateWaiter(Processor p){
            this.p=p;
        }


        /**
         * Block until the processor has transitioned to the given state.
         * Return false if the transition failed.
         */
        boolean waitForState(int state) {
            synchronized (waitSync) {
                try {
                    while (p.getState() != state && stateTransitionOK)
                        waitSync.wait();
                } catch (Exception e) {}
            }
            return stateTransitionOK;
        }


        /**
         * Controller Listener.
         */
        public void controllerUpdate(ControllerEvent evt) {

            if (evt instanceof ConfigureCompleteEvent ||
                evt instanceof RealizeCompleteEvent ||
                evt instanceof PrefetchCompleteEvent) {
                synchronized (waitSync) {
                    stateTransitionOK = true;
                    waitSync.notifyAll();
                }
            } else if (evt instanceof ResourceUnavailableEvent) {
                synchronized (waitSync) {
                    stateTransitionOK = false;
                    waitSync.notifyAll();
                }
            }else if (evt instanceof EndOfMediaEvent) {
                lStatus.setText("DONE!");
            }
        }
    }
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.