根据视频获取视频截图、并判断截图中图片某个位置为白色

来源:互联网 发布:淘宝客推广到哪里 编辑:程序博客网 时间:2024/05/17 14:26

调用示例:////视频分辨率int xx = 720;int yy = 480;
                        //视频截图文件(彩色) String screenshotsPath = "D:\\jpg\\to.jpg";
                        //ffmpeg路径 用于在视频中截取图片 视频截图功能
                        //对于选定的视频,截取指定时间的缩略图。视频抓图,获取静态图和动态图,不提倡抓gif文件;因为抓出的gif文件大而播放不流畅。
                        processImg(ffmpegPath, tm.get(tm.firstKey()).getPath(), xx, yy, timeing, screenshotsPath);//视频截取


/**

     * 根据视频取得截图   由于视频截图需要点时间,建议调用结束后  线程休息十秒
     */
    ////视频分辨率int xx = 720;int yy = 480;
    //视频截图文件(彩色) String screenshotsPath = "D:\\jpg\\to.jpg";
    //ffmpeg路径 用于在视频中截取图片 视频截图功能
    //对于选定的视频,截取指定时间的缩略图。视频抓图,获取静态图和动态图,不提倡抓gif文件;因为抓出的gif文件大而播放不流畅。
    //processImg(ffmpegPath, tm.get(tm.firstKey()).getPath(), xx, yy, timeing, screenshotsPath);
    ////ffmpeg路径 用于在视频中截取图片 视频截图功能
    //对于选定的视频,截取指定时间的缩略图。视频抓图,获取静态图和动态图,不提倡抓gif文件;因为抓出的gif文件大而播放不流畅。
    //private static final String ffmpegPath = "D:\\GaoPin\\2014-02-27\\ffmpeg-20131021-git-712eff4-win32-static\\bin\\ffmpeg.exe";
    public static void processImg(String ffmpge,String fromvedio,Integer width,Integer height,Integer timeing,String toimage) throws Exception{
        List<String> commend = new java.util.ArrayList<String>();
        commend.add(ffmpge);  // 添加转换工具路径
        commend.add("-i");  // 添加参数"-i",该参数指定要转换的文件
        commend.add(fromvedio); // 添加要转换格式的视频文件的路径
        commend.add("-y");
        commend.add("-f");
        commend.add("image2");
        commend.add("-ss"); // 添加参数"-ss",该参数指定截取的起始时间
        commend.add(String.valueOf(timeing)); // 添加起始时间为第17秒
        commend.add("-t"); // 添加参数"-t",该参数指定持续时间
        commend.add("0.001"); // 添加持续时间为1毫秒
        commend.add("-s"); // 添加参数"-s",该参数指定截取的图片大小
        commend.add(width+"x"+height); // 添加截取的图片大小为350*240
        commend.add(toimage);  // 添加截取的图片的保存路径
        try {
            ProcessBuilder builder = new ProcessBuilder();
            builder.command(commend);
            builder.start();
//            System.out.println("截取图片完成");
        } catch (Exception e) {
            System.out.println("processImg() Exception:" + e.getMessage());
            throw e;
        }

    }   



/**
     * 传入图片  判断是否在对面坐标下图片为颜色为白色
     * //视频截图文件(彩色)
        private static final String screenshotsPath = "D:\\jpg\\to.jpg";
        //Image和BufferedImage的主要作用就是将一副图片加载到内存中。
        //Java将一副图片加载到内存中的方法是:
        BufferedImage image = ImageIO.read(new File(screenshotsPath));  传入参数示例
        BufferedImage image_01 = image.getSubimage(193, 425, 36, 23);//截取现有图片的一部分,作为新的图片 //前两个值是坐标位置X、Y,后两个是长和宽  截取图片某一个部分
     * @param image
     * @return
     */
    public static boolean checkHaoma(BufferedImage image) {
        try {
            for (int i=0; i<haoma.length; i++) {
                if (haoma[i].matches("[\\d]+") == false) {
                    return false;
                } else {
                    if (Integer.parseInt(haoma[i]) <= 0 || Integer.parseInt(haoma[i]) >= 12) {
                        return false;
                    }
                    if (haoma[i].length() != 2) {
                        return false;
                    }
                }
            }
            //验证三个点是否为白色,白色即为开奖数字,非白色即为识别错误
            for (int i=0; i<haoma.length; i++) {
                int[] pixel = new int[3];
                if ("01".equals(haoma[i])) {
                    pixel[0] = image.getRGB(354, 50);
                    pixel[1] = image.getRGB(354, 55);
                    pixel[2] = image.getRGB(354, 60);
                } else if ("02".equals(haoma[i])) {
                    pixel[0] = image.getRGB(438, 86);
                    pixel[1] = image.getRGB(439, 86);
                    pixel[2] = image.getRGB(440, 86);
                } else if ("03".equals(haoma[i])) {
                    pixel[0] = image.getRGB(510, 139);
                    pixel[1] = image.getRGB(510, 140);
                    pixel[2] = image.getRGB(510, 141);
                } else if ("04".equals(haoma[i])) {
                    pixel[0] = image.getRGB(523, 204);
                    pixel[1] = image.getRGB(523, 205);
                    pixel[2] = image.getRGB(523, 205);
                } else if ("05".equals(haoma[i])) {
                    pixel[0] = image.getRGB(492, 286);
                    pixel[1] = image.getRGB(492, 287);
                    pixel[2] = image.getRGB(492, 288);
                } else if ("06".equals(haoma[i])) {
                    pixel[0] = image.getRGB(408, 327);
                    pixel[1] = image.getRGB(408, 328);
                    pixel[2] = image.getRGB(408, 329);
                } else if ("07".equals(haoma[i])) {
                    pixel[0] = image.getRGB(315, 324);
                    pixel[1] = image.getRGB(316, 324);
                    pixel[2] = image.getRGB(317, 324);
                } else if ("08".equals(haoma[i])) {
                    pixel[0] = image.getRGB(236, 284);
                    pixel[1] = image.getRGB(236, 285);
                    pixel[2] = image.getRGB(236, 286);
                } else if ("09".equals(haoma[i])) {
                    pixel[0] = image.getRGB(209, 216);
                    pixel[1] = image.getRGB(209, 217);
                    pixel[2] = image.getRGB(209, 218);
                } else if ("10".equals(haoma[i])) {
                    pixel[0] = image.getRGB(207, 135);
                    pixel[1] = image.getRGB(207, 136);
                    pixel[2] = image.getRGB(207, 137);
                } else if ("11".equals(haoma[i])) {
                    pixel[0] = image.getRGB(263, 76);
                    pixel[1] = image.getRGB(263, 77);
                    pixel[2] = image.getRGB(263, 78);
                }
                int[][] rgb = new int[3][3];
                rgb[0][0] = (pixel[0] & 0xff0000 ) >> 16;
                rgb[0][1] = (pixel[0] & 0xff00 ) >> 8;
                rgb[0][2] = (pixel[0] & 0xff );
                rgb[1][0] = (pixel[1] & 0xff0000 ) >> 16;
                rgb[1][1] = (pixel[1] & 0xff00 ) >> 8;
                rgb[1][2] = (pixel[1] & 0xff );
                rgb[2][0] = (pixel[2] & 0xff0000 ) >> 16;
                rgb[2][1] = (pixel[2] & 0xff00 ) >> 8;
                rgb[2][2] = (pixel[2] & 0xff );
                if (rgb[0][0] <= 200 || rgb[0][1] <= 200 || rgb[0][2] <= 200 ||
                    rgb[1][0] <= 200 || rgb[1][1] <= 200 || rgb[1][2] <= 200 ||
                    rgb[2][0] <= 200 || rgb[2][1] <= 200 || rgb[2][2] <= 200) {
                    return false;
                }
            }
        }  catch (Exception e) {
            System.out.println("checkHaoma() Exception:" + e.getMessage());
            return false;
        }
        return true;
    }



0 0
原创粉丝点击