您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)java怎么實現(xiàn)抖音代碼舞源碼的內(nèi)容。小編覺得挺實用的,因此分享給大家做個參考,一起跟隨小編過來看看吧。
具體內(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圖片生成路徑,這里沒有整合AnimatedGifEncoder包 * //所以生成的是一些jpg圖片,需要自己借助其他工具整個成gif * //如果有條件可以繼續(xù)改進(jìn),直接生成一個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(); //創(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)建長度 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怎么實現(xiàn)抖音代碼舞源碼”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯,可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。