這個是從抖音上學來的,一開始刷抖音,遇到很多字符串跳舞的視頻,所以來實踐一下
原理和靜態圖片的轉換相似,這個須要每一幀的去轉換。一開始的思路是把gif的每一幀轉爲圖片,而後對圖片進行轉換,最後合成 gif 。
研究了 img4java,
![原圖](
https://blog-lossingdawn.oss-... java
![轉換後2](
https://blog-lossingdawn.oss-...git
@Test public static void gifTest() { String srcFile = "F:/123/123.gif"; String targetFile = "F:/123/123_04.gif"; String base = "01"; // 替換的字符串 // String base = "@#&$%*o!;.";// 字符串由複雜到簡單 int threshold = 3;// 閾值 GifUtil.toTextGif(srcFile, targetFile, base, threshold); }
https://blog.csdn.net/DamonRush/article/details/51746995、
https://blog.csdn.net/weiwangchao_/article/details/46520571,
還有一些其餘的工具(用了私有api,不推薦)
http://zhaorui1125.iteye.com/blog/2116816,
最後發現他們截出來的每一張圖要麼發紅,要麼模糊,只好放棄了。最後發現不須要把每一幀都保存下來,臨時存一下就好,具體代碼以下:github
JDK 1.8segmentfault
import java.awt.Color; import java.awt.Font; import java.awt.Graphics; import java.awt.image.BufferedImage; import java.io.File; import java.io.FileInputStream; import java.io.FileOutputStream; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import com.dawn.img2text.external.AnimatedGifEncoder; import com.dawn.img2text.external.GifDecoder; /** * @ClassName: GifUtil * @Description: TODO * @author jiang * @date 2018年8月14日 下午9:47:29 * */ public class GifUtil { static Logger logger = LoggerFactory.getLogger(GifUtil.class); public static boolean toTextGif(final String srcFile, final String targetFile, final String base, int threshold) { long startTime = System.currentTimeMillis(); try { GifDecoder gd = new GifDecoder(); // 要處理的圖片 int status = gd.read(new FileInputStream(new File(srcFile))); if (status != GifDecoder.STATUS_OK) { return false; } // AnimatedGifEncoder ge = new AnimatedGifEncoder(); // 這裏是關鍵,設置要替換成透明的顏色 ge.setTransparent(Color.WHITE); // ge.start(new FileOutputStream(new File(targetFile))); ge.setRepeat(0); for (int i = 0; i < gd.getFrameCount(); i++) { // 取得gif的每一幀 BufferedImage frame = gd.getFrame(i); // 你能夠對每一幀作點什麼,好比縮放什麼的,這裏就什麼都不作了 int[] rgb = new int[3]; int width = frame.getWidth(); int height = frame.getHeight(); int minx = frame.getMinX(); int miny = frame.getMinY(); int delay = gd.getDelay(i); BufferedImage tag = new BufferedImage(width, height, BufferedImage.TYPE_4BYTE_ABGR); Graphics g = tag.getGraphics(); g.setFont(new Font("微軟雅黑", Font.PLAIN, 2));// 設置字體 g.setColor(Color.BLACK);// 設置顏色 for (int x = minx; x < width; x += 1) { for (int y = miny; y < height; y += 1) { int pixel = frame.getRGB(x, y); // 下面三行代碼將一個數字轉換爲RGB數字 rgb[0] = (pixel & 0xff0000) >> 16;// red rgb[1] = (pixel & 0xff00) >> 8;// green rgb[2] = (pixel & 0xff);// blue final float gray = 0.299F * rgb[0] + 0.578F * rgb[1] + 0.114F * rgb[2]; // index [0,base.length()),index越小顏色越深 final int index = Math.round(gray * (base.length() + 1) / 255); if (index <= base.length() % threshold) { g.drawString(String.valueOf(base.charAt(index % base.length())), x, y);// 文字的編寫及位置 } /*- if (rgb[0] + rgb[1] + rgb[2] <= 300) { g.drawString(String.valueOf(base.charAt(index % base.length())), x, y);// 文字的編寫及位置 }*/ } } ge.setDelay(delay); ge.addFrame(tag); } // 輸出圖片 ge.finish(); logger.debug("{} toTextGif cost time: {}s", srcFile, System.currentTimeMillis() - startTime); } catch (Exception e) { logger.error("err", e); return false; } return true; } }
原理和靜態圖轉字符圖片是一致,再也不累述。api
https://github.com/Ruffianjiang/java4fun/tree/master/img2text工具