使用CV的视频截图 第一篇

  前端播放视频时需要视频截图来做封面,网上看了下相关的技术,找到一个最简单的截图保存和截图自动图片旋转回正的代码,现在记录下来

  

 
 
    public static String getImg(String videoPath)throws Exception{
        //保存地址
        String tempPath =System.getProperty("java.io.tmpdir")+File.separator;
        //视频加载在grabber
        FFmpegFrameGrabber grabber = new FFmpegFrameGrabber(videoPath);
        grabber.start();
        Frame frame = null;
        int length = grabber.getLengthInFrames();
        int index = 1;
        int select = 5;
        //截取视频有可能第一张为空(黑屏),截取第五张
        while (index <= length) {
            frame = grabber.grabImage();
            if (index >= select) {
                break;
            }
            index++;
        }
        //获取图片选装角度
        String rotate = grabber.getVideoMetadata("rotate");
        Java2DFrameConverter converter = new Java2DFrameConverter();
        BufferedImage bi = converter.getBufferedImage(frame);


        if (rotate != null) {
            //旋转图片
            bi = rotate(bi, Integer.parseInt(rotate));
        }
        //图片地址和图片写入
        String imgPath = tempPath+System.currentTimeMillis() + ".jpg";
        ImageIO.write(bi, "jpg", new File(imgPath));
        grabber.stop();
        //返回图片地址
        return imgPath;
    }


    public static BufferedImage rotate(BufferedImage src, int angel) {
        int src_width = src.getWidth();
        int src_height = src.getHeight();
        int type = src.getColorModel().getTransparency();
        Rectangle rect_des = calcRotatedSize(new Rectangle(new Dimension(src_width, src_height)), angel);
        BufferedImage bi = new BufferedImage(rect_des.width, rect_des.height, type);
        Graphics2D g2 = bi.createGraphics();
        g2.translate((rect_des.width - src_width) / 2, (rect_des.height - src_height) / 2);
        g2.rotate(Math.toRadians(angel), src_width / 2, src_height / 2);
        g2.drawImage(src, 0, 0, null);
        g2.dispose();
        return bi;
    }

    public static Rectangle calcRotatedSize(Rectangle src, int angel) {
        if (angel >= 90) {
            if(angel / 90 % 2 == 1) {
                int temp = src.height;
                src.height = src.width;
                src.width = temp;
            }
            angel = angel % 90;
        }
        double r = Math.sqrt(src.height * src.height + src.width * src.width) / 2;
        double len = 2 * Math.sin(Math.toRadians(angel) / 2) * r;
        double angel_alpha = (Math.PI - Math.toRadians(angel)) / 2;
        double angel_dalta_width = Math.atan((double) src.height / src.width);
        double angel_dalta_height = Math.atan((double) src.width / src.height);
        int len_dalta_width = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_width));
        int len_dalta_height = (int) (len * Math.cos(Math.PI - angel_alpha - angel_dalta_height));
        int des_width = src.width + len_dalta_width * 2;
        int des_height = src.height + len_dalta_height * 2;
        return new Rectangle(new Dimension(des_width, des_height));
    }

 

 

你可能感兴趣的:(使用CV的视频截图 第一篇)