All Downloads are FREE. Search and download functionalities are using the official Maven repository.

xworker.javacvswt.SWTFFmpegPlayer Maven / Gradle / Ivy

package xworker.javacvswt;

import org.bytedeco.javacv.FFmpegFrameGrabber;
import org.bytedeco.javacv.Java2DFrameConverter;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.graphics.ImageData;
import org.xmeta.ActionContext;
import org.xmeta.Thing;
import org.xmeta.World;
import org.xmeta.annotation.ActionField;
import xworker.javacv.FFMpegPlayer;
import xworker.swt.util.SwtUtils;
import xworker.swt.util.ThingCompositeCreator;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.InputStream;
import java.net.URL;

public class SWTFFmpegPlayer extends FFMpegPlayer{
    @ActionField
    public xworker.swt.xwidgets.ImageCanvas imageCanvas;
    @ActionField
    public org.eclipse.swt.widgets.Scale scale;

    public SWTFFmpegPlayer(){
        this.setFrameConverter(new Java2DFrameConverter());
    }

    public void setMediaSource(Object mediaSource){
        stop();

        if(this.getGrabber() != null){
            try {
                this.getGrabber().stop();
            }catch(FFmpegFrameGrabber.Exception ignored){
            }
        }

        FFmpegFrameGrabber grabber = null;
        if(mediaSource instanceof File){
            grabber = new FFmpegFrameGrabber((File) mediaSource);
        }else if (mediaSource instanceof String){
            grabber = new FFmpegFrameGrabber((String) mediaSource);
        }else if(mediaSource instanceof URL){
            grabber = new FFmpegFrameGrabber((URL) mediaSource);
        }else if(mediaSource instanceof InputStream){
            grabber = new FFmpegFrameGrabber((InputStream) mediaSource);
        }

        if(grabber != null) {
            grabber.setAudioChannels(2);
            this.setGrabber(grabber);

            imageCanvasResize();
        }
    }

    @Override
    public void drawImage(BufferedImage image) {
        if(scale != null && !scale.isDisposed()){
            ImageData imageData = SwtUtils.convertToSWT(image);
            if(imageData != null) {
                Image swtImage = new Image(scale.getDisplay(), imageData);
                imageCanvas.setImage(swtImage, true);
            }
        }
    }

    public void imageCanvasResize(){
        /*
        org.eclipse.swt.widgets.Canvas canvas = imageCanvas.getCanvas();
        org.eclipse.swt.graphics.Rectangle rec = canvas.getClientArea();
        if(getGrabber() != null){
            FFmpegFrameGrabber grabber = getGrabber();

            getGrabber().setImageHeight(rec.height);
            getGrabber().setImageWidth(rec.width);
        }*/
    }

    public void doPlay(){
        if(getGrabber() != null && scale != null && !scale.isDisposed()) {
            start();
        }
    }

    public void doPause(){
        pause();
    }

    public void doStop(){
        if(getGrabber() != null){
            stop();
        }
    }

    public void dispose(){
        doStop();
    }

    //xworker.javacv.swt.SWTFFMpegPlayer/@actions/@create
    public static Object create(ActionContext actionContext){
        Thing self = actionContext.getObject("self");
        SWTFFmpegPlayer player = new SWTFFmpegPlayer();

        ThingCompositeCreator cc = new ThingCompositeCreator(self, actionContext);
        Thing prototype = World.getInstance().getThing("xworker.javacv.swt.prototype.FFMpegPlayer/@mainComposite");
        cc.setCompositeThing(prototype);
        org.eclipse.swt.widgets.Composite composite = cc.create(player);

        actionContext.g().put(self.getMetadata().getName(), player);
        return composite;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy