java实现抖音代码舞源码

网友投稿 440 2023-01-09


java实现抖音代码舞源码

本文实例为大家分享了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 RunnLMlIOable() {

@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 http://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

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(LMlIO);

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 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

for(int n=0; n

if(i+m < img.getWidth() && j+n < img.getHeight())

result += img.getRGB(i+m, j+n);

}

}

return Math.abs(result) / (size*size);

}

}

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

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(LMlIO);

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 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

for(int n=0; n

if(i+m < img.getWidth() && j+n < img.getHeight())

result += img.getRGB(i+m, j+n);

}

}

return Math.abs(result) / (size*size);

}

}

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

for(int n=0; n

if(i+m < img.getWidth() && j+n < img.getHeight())

result += img.getRGB(i+m, j+n);

}

}

return Math.abs(result) / (size*size);

}

}

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

if(i+m < img.getWidth() && j+n < img.getHeight())

result += img.getRGB(i+m, j+n);

}

}

return Math.abs(result) / (size*size);

}

}

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


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:子类实现接口对象实例化(java接口可以实例化对象?)
下一篇:自动化接口测试维护(自动化接口测试用例)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~