准备工作:1.通过KMPlayer视频播放器截图 再通过读取图片取像素转字符播放
2.通过格式工厂将普通音频文件转为wav格式
代码部分:
BadApple.java:import java.awt.Color;
import java.awt.TextArea;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JFrame;
import javax.swing.JTextArea;
import org.omg.CORBA.PUBLIC_MEMBER;
public class BadApple {
public BadApple() {
JFrame frame = new JFrame();
frame.setSize(700, 560);
frame.setResizable(false);// 不可改变大小
frame.setAlwaysOnTop(true);
// frame.setLocationByPlatform(false);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);// 关闭方式
frame.setUndecorated(true);
JTextArea are = new JTextArea();
frame.add(are);
are.setBounds(0, 0,700, 560);
are.setFont(new java.awt.Font("黑体", 2, 14));
File file = new File("badapple/badapple.txt");
frame.setVisible(true);
try {
FileReader fr = new FileReader(file);
BufferedReader br = new BufferedReader(fr);// 缓存阅读
StringBuffer sb = new StringBuffer();
try {
int a = 0;
while (a < 229214) {
a++;
String str = br.readLine();
sb.append(str + "\n");
if (a % 35 == 0) {
try {
String str1 = sb.toString();
sb = new StringBuffer("");
are.setText(str1);
Thread.sleep(33);
} catch (InterruptedException e) {
e.printStackTrace();
}
} else if (a > 229214) {
are.setText("");
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
public static void main(String[] args) {
Music.play();
new BadApple();
System.exit(0);
}
}
ImageForString.java:
import java.awt.image.BufferedImage;
import java.io.IOException;
public class ImageForString {
private final static char[] asc = { '*', '.', ' ', ' ' };
public static StringBuilder imageToAscii(BufferedImage image)throws IOException{
StringBuilder strb=new StringBuilder();
int width=image.getWidth();
int height=image.getHeight();
for(int i=0;i<height;i++){
for(int j=0;j<width;j++){
int rgb=image.getRGB(j,i);
int R=(rgb&0xff0000)>>16;
int G=(rgb&0x00ff00)>>8;
int B=rgb&0x0000ff;
int gray=R+G+B;
int index=gray/255;
strb.append(asc[index]);
}
strb.append("\n");
}
return strb;
}
}
Music.java:import java.applet.Applet;
import java.applet.AudioClip;
import java.io.File;
import java.net.MalformedURLException;
import java.net.URL;
public class Music {
private static File musicFile;
private static URL musicurl;
private static AudioClip ac;
public static void play(){
musicFile = new File("badApple.wav");
try {
musicurl = musicFile.toURI().toURL();
} catch (MalformedURLException e) {
e.printStackTrace();
}
ac = Applet.newAudioClip(musicurl);
ac.play();
}
}我是在CSDN上看到的 有兴趣可以去学习了解一下效果:https://www.bilibili.com/video/av35989249/
|
|