服务器之家

服务器之家 > 正文

java实现抖音代码舞源码

时间:2021-07-31 12:02     来源/作者:cole_白

本文实例为大家分享了java实现抖音代码舞源码,供大家参考,具体内容如下

client.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
package com.dance;
 
import java.awt.container;
import java.awt.eventqueue;
import java.awt.toolkit;
import java.awt.event.actionevent;
import java.awt.event.actionlistener;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.fileinputstream;
import java.io.fileoutputstream;
import java.io.ioexception;
 
import javax.imageio.imageio;
import javax.swing.jfilechooser;
import javax.swing.jframe;
import javax.swing.jmenu;
import javax.swing.jmenubar;
import javax.swing.jmenuitem;
import javax.swing.jpanel;
/**
 * //gif图片路径,推荐使用像素在400*400以内的
 * //gif图片生成路径,这里没有整合animatedgifencoder包
 * //所以生成的是一些jpg图片,需要自己借助其他工具整个成gif
 * //如果有条件可以继续改进,直接生成一个gif文件
* @classname: client
* @description: todo(这里用一句话描述这个类的作用)
* @author baitp
* @date 2018年12月27日
*
 */
public class client {
 
 public static string input = "2.gif";
 public static string output = "d://chargif//";
 public static void main(string[] args) {
 eventqueue.invokelater(new runnable() {
 
 @override
 public void run() {
 mainframe mainframe = new mainframe();
 //创建并添加菜单栏
  jmenubar menubar = new jmenubar();
  jmenu menufile = new jmenu("gif图片");
  menubar.add(menufile);
  jmenuitem itemsave = new jmenuitem("上传");
//  itemsave.setaccelerator(keystroke.getkeystroke(keyevent.vk_s,inputevent.ctrl_mask));
  menufile.add(itemsave);
  mainframe.setjmenubar(menubar);
 mainframe.setdefaultcloseoperation(jframe.exit_on_close);
 mainframe.setvisible(true);
 itemsave.addactionlistener(new actionlistener() {
 @override
 public void actionperformed(actionevent e) {
 jfilechooser fd = new jfilechooser();
 //fd.setfileselectionmode(jfilechooser.directories_only);
 fd.showopendialog(null);
 fd.setfileselectionmode(0);
 file f = fd.getselectedfile();
 string url =client.class.getclassloader().getresource("").getfile()+"image/"+f.getname();
 file outfile = new file(url);
 try {
 fileinputstream fi = new fileinputstream(f);
 fileoutputstream out =new fileoutputstream(outfile);
 //创建搬运工具
   byte datas[] = new byte[1024*8];
   //创建长度
   int len = 0;
   //循环读取数据
   while((len = fi.read(datas))!=-1){
   out.write(datas,0,len);
   }
   client.input=f.getname();
   //3.释放资源
   fi.close();
   out.close();
 } catch (exception e1) {
 e1.printstacktrace();
 }
 }
 });
 }
 });
 }
}
 
@suppresswarnings("serial")
class mainframe extends jframe {
 private int x;
 private int y;
 public static int width = 0;
 public static int height = 0;
 {
 toolkit kit = toolkit.getdefaulttoolkit();
 int screen_width = kit.getscreensize().width;
 int screen_height = kit.getscreensize().height;
 x = (screen_width-width)/2;
 y = (screen_height-height)/2;
 }
 public mainframe() {
 settitle("intlstar code dance 666");
 initbounds();
 creategif();
 setbounds(x, y, width, height);
 setresizable(false);
 seticonimage(null);
 
 jpanel panel = new gamepanel();
 container container = getcontentpane();
 container.add(panel);
 }
 //将字符图片输出到指定目录
 public void creategif() {
 bufferedimage[] charimgs = imgtocharacter.getcharimgs();
 try {
 for (int i=0; i<charimgs.length; i++) {
 file file = new file(client.class.getclassloader().getresource("").getfile()+"chargif/"+i+".jpg");
 imageio.write(charimgs[i], "jpg", file);
 }
 } catch (ioexception e) {
 e.printstacktrace();
 }
 system.out.println("图片输出完成!");
 }
 public void initbounds() {
 imgtocharacter.readgif();
 imgtocharacter.draw();
 bufferedimage[] charimgs = imgtocharacter.getcharimgs();
 int max_width = 0;
 int max_height = 0;
 for (bufferedimage img : charimgs) {
 if(img.getwidth() > max_width)
 max_width = img.getwidth();
 if(img.getheight() > max_height)
 max_height = img.getheight();
 }
 width = max_width;
 height = max_height;
 }
}

gamepanel.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
package com.dance;
 
import java.awt.color;
import java.awt.graphics;
import java.awt.image.bufferedimage;
 
import javax.swing.jpanel;
 
@suppresswarnings("serial")
public class gamepanel extends jpanel{
 
 private int fps = 6;
 private bufferedimage[] charimgs;
 int count = 0;
 private bufferedimage background = null;
 public gamepanel() {
 charimgs = imgtocharacter.getcharimgs();
 initbackgroudimg();
 thread t = new mythread();
 t.start();
 }
 public void initbackgroudimg() {
 background = new bufferedimage(mainframe.width, mainframe.height, bufferedimage.type_int_rgb);
 graphics gb = background.getgraphics();
 gb.setcolor(color.white);
 gb.fillrect(0, 0, background.getwidth(), background.getheight());
 gb.dispose();
 }
 @override
 public void paint(graphics g) {
 super.paint(g);
 int size = charimgs.length;
 int index = count % size;
 bufferedimage img = charimgs[index];
 int w = img.getwidth();
 int h = img.getheight();
// int x = (mainframe.width - w)/2;
// int y = (mainframe.height - h)/2;
 //背景涂白
 g.setcolor(color.white);
 g.fillrect(0, 0, mainframe.width, mainframe.height);
 g.setcolor(color.black);
 // 3/4为显示比例,考虑到有的gif图片太大,所以缩小至一定比例在窗口中显示
 g.drawimage(img, 0, mainframe.height - h,w*3/4, h*3/4, null);
 count++;
 }
 
 class mythread extends thread {
 
 long starttime;
 long endtime;
 long sleeptime;
 long spendtime;
 long period = 1000/fps;
 @override
 public void run() {
 while(true) {
 starttime = system.currenttimemillis();
 repaint();
 endtime = system.currenttimemillis();
 spendtime = endtime - starttime;
 sleeptime = period - spendtime;
 if(sleeptime < 0)
 sleeptime = 2;
 try {
 thread.sleep(sleeptime);
 } catch (interruptedexception e) {
 e.printstacktrace();
 }
 
 }
 }
 
 }
}

imgtocharacter.java

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
package com.dance;
 
import java.awt.color;
import java.awt.font;
import java.awt.graphics;
import java.awt.image.bufferedimage;
import java.io.file;
import java.io.ioexception;
import java.util.iterator;
 
import javax.imageio.imageio;
import javax.imageio.imagereader;
import javax.imageio.stream.imageinputstream;
 
public class imgtocharacter {
 
 private static bufferedimage[] realimgs;
 private static bufferedimage[] charimgs;
 private static int size = 4;
 private static string[] chars= {" ",".",":",";","-","~","1","i","o","r","a",
  "2","c","3","b","n","q","k","x","s","x",
  "7","z","o","8","#","$","%","&","m","b",
  "w","@","@"};
 
 public static void draw() {
 int interval = 16777215 / (chars.length-1) ;
 bufferedimage onerealimg = null;
 int index = 0;
 while(index < realimgs.length) {
 onerealimg = realimgs[index];
 int width = onerealimg.getwidth();
 int height = onerealimg.getheight();
 bufferedimage onecharimg = new bufferedimage(width*3, height*3,bufferedimage.type_int_rgb);
 graphics g = onecharimg.getgraphics();
 g.setcolor(color.white);
 g.fillrect(0, 0, onecharimg.getwidth(), onecharimg.getheight());
 g.setcolor(color.black);
 g.setfont(new font("黑体", font.bold, 15));
 
 for(int i= 0 ; i < height ; i+=size){
 for(int j = 0 ; j < width; j+=size){
  int rgb = getavgrgb(j, i, onerealimg);
 int k = rgb/interval;
 g.drawstring(chars[k], 12*j/size, 12*i/size);
 }
 }
 g.dispose();
 charimgs[index] = onecharimg;
 index++;
 }
 
 }
 public static void readgif() {
 try {
 system.out.println(imgtocharacter.class.getclassloader().getresource("").getfile());
 file f = new file(imgtocharacter.class.getclassloader().getresource("").getfile()+"image/"+client.input);
 string name = f.getname();
 string suffix = name.substring(name.lastindexof('.')+1);
 
 iterator<imagereader> iter = imageio.getimagereadersbysuffix(suffix);
 imagereader reader = iter.next();
 imageinputstream imagein;
 imagein = imageio.createimageinputstream(f);
 reader.setinput(imagein);
 int count = reader.getnumimages(true);
 realimgs = new bufferedimage[count];
 charimgs = new bufferedimage[count];
 for(int i=0; i< count; i++) {
 realimgs[i] = reader.read(i);
 }
 
 } catch (ioexception e) {
 e.printstacktrace();
 }
 }
 public static bufferedimage[] getcharimgs() {
 return charimgs;
 }
 public static int getavgrgb(int i, int j, bufferedimage img) {
 int result = 0;
 for(int m=0; m<size; m++) {
 for(int n=0; n<size; n++) {
 if(i+m < img.getwidth() && j+n < img.getheight())
 result += img.getrgb(i+m, j+n);
 }
 }
 return math.abs(result) / (size*size);
 }
 
}

完整源码下载地址:代码舞

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。

标签:

相关文章

热门资讯

2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全
2020微信伤感网名听哭了 让对方看到心疼的伤感网名大全 2019-12-26
yue是什么意思 网络流行语yue了是什么梗
yue是什么意思 网络流行语yue了是什么梗 2020-10-11
背刺什么意思 网络词语背刺是什么梗
背刺什么意思 网络词语背刺是什么梗 2020-05-22
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总
苹果12mini价格表官网报价 iPhone12mini全版本价格汇总 2020-11-13
2021德云社封箱演出完整版 2021年德云社封箱演出在线看
2021德云社封箱演出完整版 2021年德云社封箱演出在线看 2021-03-15
返回顶部