思路:把视频的每一帧保存在一个图片,并把图片转成字符串输出,这里我用的是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