花里胡哨1之网上炫酷的字符动画怎么写—————java

时间:2020-9-2 作者:admin

思路:把视频的每一帧保存在一个图片,并把图片转成字符串输出,这里我用的是opencv库

public class day2 extends JFrame {
static final String ascii = "#u:, ";
static final int textWidth = 190;
static final int textHeight = 50;
Container container;
JTextArea jTextArea;
static {
    System.loadLibrary(Core.NATIVE_LIBRARY_NAME);
  } 
public day2() {
    setBounds(100, 100, 900, 600);
    setTitle("动画");
    setDefaultCloseOperation(EXIT_ON_CLOSE);
    container = getContentPane();
    jTextArea = new JTextArea();
    jTextArea.setFont(new Font("楷体", Font.PLAIN, 8));
    container.add(jTextArea);
    setResizable(false);
    setVisible(true);
}
public void put(String string) {
    VideoCapture capture = new VideoCapture(string.toString());    
    if (!capture.isOpened()) 
        return;
    HighGui.namedWindow("show");
    Mat img1 = new Mat();
    while (capture.read(img1)) {
    HighGui.imshow("show", img1);
    HighGui.waitKey(30);
        jTextArea.setText(imgTest(img1).toString());
    }
}
public StringBuilder imgTest(Mat img1) {
    Mat img2=new Mat();
    int index, gray;
    Imgproc.cvtColor(img1, img2, Imgproc.COLOR_RGB2GRAY);
    int x = img2.rows() / textHeight;
    int y = img2.cols() / textWidth;
    StringBuilder src = new StringBuilder();
    for (int i = 0; i < img2.rows(); i += x) {
        for (int j = 0; j < img2.cols(); j += y) {
            gray = (int) (img2.get(i, j)[0]);
            index = Math.round(gray * (ascii.length() + 1) / 255);
            src.append(index >= ascii.length() ? "." : String.valueOf(ascii.charAt(index)));
        }
        src.append("\n");
    }
    return src;
}
public static void main(String[] a) {
new day2().put("F:/tupian/a5.mp4");
}}

————————————————————Zzh

声明:本文内容由互联网用户自发贡献自行上传,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任。如果您发现有涉嫌版权的内容,欢迎进行举报,并提供相关证据,工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。