這篇文章給大家分享的是有關(guān)java怎么實(shí)現(xiàn)抖音代碼舞源碼的內(nèi)容。小編覺(jué)得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過(guò)來(lái)看看吧。

企業(yè)建站必須是能夠以充分展現(xiàn)企業(yè)形象為主要目的,是企業(yè)文化與產(chǎn)品對(duì)外擴(kuò)展宣傳的重要窗口,一個(gè)合格的網(wǎng)站不僅僅能為公司帶來(lái)巨大的互聯(lián)網(wǎng)上的收集和信息發(fā)布平臺(tái),創(chuàng)新互聯(lián)公司面向各種領(lǐng)域:圍欄護(hù)欄等網(wǎng)站設(shè)計(jì)、成都營(yíng)銷網(wǎng)站建設(shè)解決方案、網(wǎng)站設(shè)計(jì)等建站排名服務(wù)。
具體內(nèi)容如下
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以內(nèi)的
* //gif圖片生成路徑,這里沒(méi)有整合AnimatedGifEncoder包
* //所以生成的是一些jpg圖片,需要自己借助其他工具整個(gè)成gif
* //如果有條件可以繼續(xù)改進(jìn),直接生成一個(gè)gif文件
* @ClassName: Client
* @Description: TODO(這里用一句話描述這個(gè)類的作用)
* @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();
//創(chuàng)建并添加菜單欄
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);
//創(chuàng)建搬運(yùn)工具
byte datas[] = new byte[1024*8];
//創(chuàng)建長(zhǎng)度
int len = 0;
//循環(huán)讀取數(shù)據(jù)
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);
}
}感謝各位的閱讀!關(guān)于“java怎么實(shí)現(xiàn)抖音代碼舞源碼”這篇文章就分享到這里了,希望以上內(nèi)容可以對(duì)大家有一定的幫助,讓大家可以學(xué)到更多知識(shí),如果覺(jué)得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
分享名稱:java怎么實(shí)現(xiàn)抖音代碼舞源碼
網(wǎng)站URL:http://chinadenli.net/article42/jgpoec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供企業(yè)建站、品牌網(wǎng)站建設(shè)、外貿(mào)網(wǎng)站建設(shè)、定制開發(fā)、品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)