Java 将进度条与方法的执行相关联

Java 将进度条与方法的执行相关联,java,javafx,progress-bar,Java,Javafx,Progress Bar,如何将进度条的进度与我知道执行时间的方法的执行联系起来? 这是我的控制器类的代码 @FXML void elaboraVideo(ActionEvent event) throws IOException { if(control == false) { Alert alert = new Alert(AlertType.INFORMATION); alert.setTitle("Impossibile proseguire"); al

如何将进度条的进度与我知道执行时间的方法的执行联系起来? 这是我的控制器类的代码

@FXML
void elaboraVideo(ActionEvent event) throws IOException {

    if(control == false) {

        Alert alert = new Alert(AlertType.INFORMATION);
        alert.setTitle("Impossibile proseguire");
        alert.setHeaderText(null);
        alert.setContentText("Nessun video selezionato!");
        alert.show();

    } else {

        long inizio = System.currentTimeMillis();
        videoM.workVideo(absolutePath);
        long fine = System.currentTimeMillis();
        long time=(inizio-fine)/1000;
        load.setProgress(time);

    }

}
该方法是workVideo,它位于另一个类中

这是workVideo的代码,它可以正常工作

ConfigGeneralDetector confDetector = new ConfigGeneralDetector();
            confDetector.threshold = 1;
            confDetector.maxFeatures = 300;
            confDetector.radius = 3;

            // Use a KLT tracker
            PointTracker<ImageFloat32> tracker = FactoryPointTracker.klt(new int[]{1,2,4,8},confDetector,3,
                    ImageFloat32.class,ImageFloat32.class);

            // This estimates the 2D image motion
            // An Affine2D_F64 model also works quite well.
            ImageMotion2D<ImageFloat32,Homography2D_F64> motion2D =
                    FactoryMotion2D.createMotion2D(220,3,2,30,0.6,0.5,false,tracker,new Homography2D_F64());

            // wrap it so it output color images while estimating motion from gray
            ImageMotion2D<MultiSpectral<ImageFloat32>,Homography2D_F64> motion2DColor =
                    new MsToGrayMotion2D<ImageFloat32,Homography2D_F64>(motion2D,ImageFloat32.class);

            // This fuses the images together
            StitchingFromMotion2D<MultiSpectral<ImageFloat32>,Homography2D_F64>
                    stitch = FactoryMotion2D.createVideoStitchMS(0.5, motion2DColor, ImageFloat32.class);

            // Load an image sequence
            MediaManager media = DefaultMediaManager.INSTANCE;
            String fileName = UtilIO.pathExample(path);
            SimpleImageSequence<MultiSpectral<ImageFloat32>> video =
                    media.openVideo(fileName, ImageType.ms(3, ImageFloat32.class));

            MultiSpectral<ImageFloat32> frame = video.next();

            // shrink the input image and center it
            Homography2D_F64 shrink = new Homography2D_F64(0.5,0,frame.width/4,0,0.5,frame.height/4,0,0,1);
            shrink = shrink.invert(null);

            // The mosaic will be larger in terms of pixels but the image will be scaled down.
            // To change this into stabilization just make it the same size as the input with no shrink.
            stitch.configure(frame.width,frame.height,shrink);
            // process the first frame
            stitch.process(frame);

            // Create the GUI for displaying the results + input image
            ImageGridPanel gui = new ImageGridPanel(1,2);
            //gui.setImage(0,0,new BufferedImage(frame.width,frame.height,BufferedImage.TYPE_INT_RGB));
            gui.setImage(0,1,new BufferedImage(frame.width,frame.height,BufferedImage.TYPE_INT_RGB));
            gui.setPreferredSize(new Dimension(frame.width*2,frame.height*2));
            gui.setBackground(Color.BLACK);

            ShowImages.showWindow(gui,"Example Mosaic", true);

            boolean enlarged = false;

            // process the video sequence one frame at a time
            while( video.hasNext() ) {
                frame = video.next();
                if( !stitch.process(frame) )
                    throw new RuntimeException("You should handle failures");

                // if the current image is close to the image border recenter the mosaic
                StitchingFromMotion2D.Corners corners = stitch.getImageCorners(frame.width,frame.height,null);
                if( nearBorder(corners.p0,stitch) || nearBorder(corners.p1,stitch) ||
                        nearBorder(corners.p2,stitch) || nearBorder(corners.p3,stitch) ) {
                    stitch.setOriginToCurrent();

                    // only enlarge the image once
                    if( !enlarged ) {
                        enlarged = true;
                        // double the image size and shift it over to keep it centered
                        int widthOld = stitch.getStitchedImage().width;
                        int heightOld = stitch.getStitchedImage().height;

                        int widthNew = widthOld*2;
                        int heightNew = heightOld*2;

                        int tranX = (widthNew-widthOld)/2;
                        int tranY = (heightNew-heightOld)/2;

                        Homography2D_F64 newToOldStitch = new Homography2D_F64(1,0,-tranX,0,1,-tranY,0,0,1);

                        stitch.resizeStitchImage(widthNew, heightNew, newToOldStitch);
                        gui.setImage(0, 1, new BufferedImage(widthNew, heightNew, BufferedImage.TYPE_INT_RGB));
                    }
                    corners = stitch.getImageCorners(frame.width,frame.height,null);
                }
                // display the mosaic
                ConvertBufferedImage.convertTo(frame,gui.getImage(0, 0),true);
                ConvertBufferedImage.convertTo(stitch.getStitchedImage(), gui.getImage(0, 1),true);

                // draw a red quadrilateral around the current frame in the mosaic
                Graphics2D g2 = gui.getImage(0,1).createGraphics();
                g2.setColor(Color.RED);
                g2.drawLine((int)corners.p0.x,(int)corners.p0.y,(int)corners.p1.x,(int)corners.p1.y);
                g2.drawLine((int)corners.p1.x,(int)corners.p1.y,(int)corners.p2.x,(int)corners.p2.y);
                g2.drawLine((int)corners.p2.x,(int)corners.p2.y,(int)corners.p3.x,(int)corners.p3.y);
                g2.drawLine((int)corners.p3.x,(int)corners.p3.y,(int)corners.p0.x,(int)corners.p0.y);

                gui.repaint();
                // throttle the speed just in case it's on a fast computer
                BoofMiscOps.pause(50);

            }
            ImageIO.write(gui.getImage(0, 1), "jpg", new File("C:/Users/Administrator/Desktop/provaaa.jpg"));
            Image img = gui.getImage(0, 1);
            return img;
ConfigGeneralDetector confDetector=new ConfigGeneralDetector();
阈值=1;
confDetector.maxFeatures=300;
半径=3;
//使用KLT跟踪器
PointTracker tracker=FactoryPointTracker.klt(新的int[]{1,2,4,8},confDetector,3,
ImageFloat32.class,ImageFloat32.class);
//这将估计2D图像运动
//仿射2D_F64模型也可以很好地工作。
图像运动2D运动2D=
createMotion2D(220,3,2,30,0.6,0.5,false,tracker,new Homography2D_F64());
//对其进行包装,以便在根据灰度估计运动时输出彩色图像
ImageMotion2D运动2dColor=
新的MsToGrayMotion2D(motion2D,ImageFloat32.class);
//这将图像融合在一起
从运动2D拼接
stitch=FactoryMotion2D.createVideoStitchMS(0.5,motion2DColor,ImageFloat32.class);
//加载图像序列
MediaManager媒体=DefaultMediaManager.INSTANCE;
字符串文件名=UtilIO.pathExample(路径);
SimpleImage视频序列=
openVideo(文件名,ImageType.ms(3,ImageFloat32.class));
多光谱帧=video.next();
//缩小输入图像并使其居中
同形图2D_F64收缩=新同形图2D_F64(0.5,0,帧宽度/4,0,0.5,帧高度/4,0,0,1);
收缩=收缩。反转(null);
//马赛克的像素会更大,但图像会缩小。
//要将其更改为稳定,只需使其与输入大小相同,且无收缩。
缝合.配置(框架.宽度,框架.高度,收缩);
//处理第一帧
缝合工艺(框架);
//创建用于显示结果和输入图像的GUI
ImageGridPanel gui=新ImageGridPanel(1,2);
//setImage(0,0,新的BufferedImage(frame.width,frame.height,BufferedImage.TYPE_INT_RGB));
setImage(0,1,新的BufferedImage(frame.width,frame.height,BufferedImage.TYPE_INT_RGB));
setPreferredSize(新尺寸(框宽*2,框高*2));
gui.setBackground(颜色:黑色);
ShowImages.showWindow(gui,“示例马赛克”,true);
布尔放大=假;
//一次处理一帧视频序列
while(video.hasNext()){
frame=video.next();
if(!缝合过程(框架))
抛出新的RuntimeException(“您应该处理失败”);
//如果当前图像接近图像边框,请重新居中马赛克
StitchingFromMotion2D.Corners=stitch.getImageCorners(frame.width,frame.height,null);
if(nearBorder(corners.p0,缝合)| | nearBorder(corners.p1,缝合)||
近边框(角点p2,缝合)| |近边框(角点p3,缝合)){
stitch.setOriginToCurrent();
//只放大一次图像
如果(!放大){
放大=真;
//将图像大小增加一倍,并将其移到中间位置
int widthOld=stitch.getStitchDimage().width;
int heightOld=stitch.getStitchDimage().height;
int widthNew=widthOld*2;
int heightNew=heightOld*2;
int-tranX=(宽度新宽度旧)/2;
int tranY=(新高度)/2;
同形图2D_F64 newToOldStitch=新同形图2D_F64(1,0,-tranX,0,1,-tranY,0,0,1);
缝合。调整缝合图像(宽度新、高度新、新工具缝合);
setImage(0,1,新BuffereImage(widthNew,heightNew,BuffereImage.TYPE_INT_RGB));
}
拐角=stitch.getImageCorners(帧.宽度,帧.高度,null);
}
//展示马赛克
ConvertBufferedImage.convertTo(frame,gui.getImage(0,0),true);
ConvertBuffereImage.convertTo(stitch.GetStitchDimage(),gui.getImage(0,1),true);
//围绕马赛克中的当前帧绘制一个红色四边形
Graphics2D g2=gui.getImage(0,1).createGraphics();
g2.设置颜色(颜色为红色);
g2.画线((int)角点.p0.x,(int)角点.p0.y,(int)角点.p1.x,(int)角点.p1.y);
g2.绘制线((int)corners.p1.x,(int)corners.p1.y,(int)corners.p2.x,(int)corners.p2.y);
g2.绘制线((int)corners.p2.x,(int)corners.p2.y,(int)corners.p3.x,(int)corners.p3.y);
g2.画线((int)角点。p3.x,(int)角点。p3.y,(int)角点。p0.x,(int)角点。p0.y);
gui.repaint();
//控制速度,以防它在高速计算机上运行
BoofMiscOps.暂停(50);
}
write(gui.getImage(0,1),“jpg”,新文件(“C:/Users/Administrator/Desktop/provaaa.jpg”);
Image img=gui.getImage(0,1);
返回img;

这里不太清楚您的意思。在方法实际完成之前,您不知道该方法的执行时间,因此您不能真正希望在它执行时使进度条前进。还是我误解了你的意图?除非你有办法知道,否则我想把进度条推进到执行方法的末尾