java运用ascii实现动画效果_花里胡哨1之网上炫酷的字符动画怎么写---------------java...

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值