package org.jeecg.modules.opencv.utils;

import org.opencv.core.Core;
import org.opencv.core.Mat;
import org.opencv.core.Size;
import org.opencv.imgcodecs.Imgcodecs;
import org.opencv.imgproc.Imgproc;

import java.io.File;

public class Test {
    public static void main(String[] args) {
        VideoProcessing.grabberVideoFramer("D:\\data\\model\\模板视频.mp4", "D:\\Data\\model\\src\\");
    }
    public static void initVideo() {
        System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
        String srcPath = "D:\\data\\images\\";
        String fillPath = "D:\\data\\fill\\";
        String targetPath = "D:\\data\\video\\";
        //1:切分视频
//        VideoProcessing.grabberVideoFramer("D://data//video.mp4",srcPath);
//        VideoProcessing.grabberVideoFramer("D://data//fill.mp4",fillPath);
        //2:合并图片
        int srcPhoneFrameNumFrom = 163;
        int changeSenceFrameNumFrom = 234;
        //2.1 复制原始视频无变化图片
        for (int i = 1; i < srcPhoneFrameNumFrom; i++) {
            Mat srcNoChangeImage = Imgcodecs.imread(srcPath + i + ".jpg");
            Imgcodecs.imwrite(targetPath + i + ".jpg", srcNoChangeImage);
        }
        String getImageName = "";
        //2.2 填充手机内部图片画面
        for (int i = 163; i < changeSenceFrameNumFrom; i++) {
            String backImg = srcPath + (i) + ".jpg";
            String phoneImg = fillPath + (i - 162) + ".jpg";
            String targetImg = targetPath + i + ".jpg";
            Perspective.perpectiveImg(backImg, phoneImg, targetImg);
            getImageName = targetImg;
        }
        //2.3 转场动画
        Mat image = Imgcodecs.imread(getImageName, Imgcodecs.IMREAD_COLOR);
        //(1)径向模糊图片
        Mat radialBlur = ImageUtils.radialBlur(image);
        Imgcodecs.imwrite(targetPath + changeSenceFrameNumFrom + ".jpg", radialBlur);
        changeSenceFrameNumFrom++;
        //(2)放大后径向模糊图片
        Mat bigImamge = new Mat();
        Imgproc.resize(image, bigImamge, new Size(image.cols() * 1.25, image.rows() * 1.25));
        Mat destBigImamge = ImageUtils.cutFromCenter(bigImamge, image.width(), image.height());
        Mat radialBlurDestBigImamge = ImageUtils.radialBlur(destBigImamge);
        Imgcodecs.imwrite(targetPath + changeSenceFrameNumFrom + ".jpg", radialBlurDestBigImamge);
        changeSenceFrameNumFrom++;
        //(3)缩小之后重复平铺加径向模糊
        Mat smallImage = new Mat();
        Mat halfImage = new Mat();
        Mat wholeImage = new Mat();
        Imgproc.resize(image, smallImage, new Size(image.cols() / 2, image.rows() / 2));
        ImageUtils.hconcat(smallImage, smallImage, halfImage);
        ImageUtils.vconcat(halfImage, halfImage, wholeImage);
        wholeImage = ImageUtils.radialBlur(wholeImage);
        Imgcodecs.imwrite(targetPath + changeSenceFrameNumFrom + ".jpg", wholeImage);
        changeSenceFrameNumFrom++;
        //复制填充视频无变化图片
        for (int i = changeSenceFrameNumFrom; i < 100000; i++) {
            int checkNum = i - 3 - srcPhoneFrameNumFrom + 1;
            String fillImagePath = fillPath + checkNum + ".jpg";
            File file = new File(fillImagePath);
            if (file.exists()) {
                Mat srcMat = Imgcodecs.imread(fillImagePath);
                Imgcodecs.imwrite(targetPath + i + ".jpg", srcMat);
            } else {
                break;
            }
        }
    }

}
