欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

java实现抖音代码舞源码

程序员文章站 2023-12-15 08:40:28
本文实例为大家分享了java实现抖音代码舞源码,供大家参考,具体内容如下 client.java package com.dance; import ja...

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

client.java

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

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

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);
 }

}

完整源码下载地址:

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

上一篇:

下一篇: