Loading

图片转字符图片(二)

序言

这个是从抖音上学来的,一开始刷抖音,遇到不少字符串跳舞的视频,因此来实践一下

主要分为三个部分

  1. 静态图片转静态图片
  2. gif 转 gif
  3. 视频转视频

gif 转 gif

原理和静态图片的转换类似,这个需要每一帧的去转换。一开始的思路是把gif的每一帧转为图片,然后对图片进行转换,最后合成 gif 。
研究了 im4java,参考: https://blog.csdn.net/DamonRush/article/details/51746995
https://blog.csdn.net/weiwangchao_/article/details/46520571
还有一些其他的工具(用了私有api,不推荐)
http://zhaorui1125.iteye.com/blog/2116816
最后发现他们截出来的每一张图要么发红,要么模糊,只好放弃了。最后发现不需要把每一帧都保存下来,临时存一下就好,具体代码如下:

环境:
JDK 1.8

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

原理和静态图转字符图片是一致,不再累述。

源码地址:
https://github.com/Ruffianjiang/java4fun/tree/master/img2text

posted @ 2018-08-21 00:23  遗失的拂晓  阅读(219)  评论(0编辑  收藏  举报