最近了解到关于抖音上图片转化为txt文本的视频,自认为IT搬用工我也可以尝试下,从而有以下实验成果

代码工程搬自开源项目,个人加以修改并简单注释了。代码如下:

package cn.xsshome.imagetool.util;
/**
 *
 */
public class NeuQuant {
    protected static final int netsize = 256; /* number of colours used */

/* four primes near 500 - assume no image has a length so large */
    /* that it is divisible by all four primes */
    protected static final int prime1 = 499;
    protected static final int prime2 = 491;
    protected static final int prime3 = 487;
    protected static final int prime4 = 503;
    protected static final int minpicturebytes = (3 * prime4);
    /* minimum size for input image */

/*
     * Program Skeleton ---------------- [select samplefac in range 1..30] [read
     * image from input file] pic = (unsigned char*) malloc(3*width*height);
     * initnet(pic,3*width*height,samplefac); learn(); unbiasnet(); [write
     * output image header, using writecolourmap(f)] inxbuild(); write output
     * image using inxsearch(b,g,r)
     */

/*
     * Network Definitions -------------------
     */
    protected static final int maxnetpos = (netsize - 1);
    protected static final int netbiasshift = 4; /* bias for colour values */

protected static final int ncycles = 100; /* no. of learning cycles */

/* defs for freq and bias */
    protected static final int intbiasshift = 16; /* bias for fractions */

protected static final int intbias = (((int) 1) << intbiasshift);
    protected static final int gammashift = 10; /* gamma = 1024 */

protected static final int gamma = (((int) 1) << gammashift);
    protected static final int betashift = 10;
    protected static final int beta = (intbias >> betashift); /* beta = 1/1024 */

protected static final int betagamma = (intbias << (gammashift - betashift));

/* defs for decreasing radius factor */
    protected static final int initrad = (netsize >> 3); /*
                                                         * for 256 cols, radius
                                                         * starts
                                                         */

protected static final int radiusbiasshift = 6; /* at 32.0 biased by 6 bits */

protected static final int radiusbias = (((int) 1) << radiusbiasshift);
    protected static final int initradius = (initrad * radiusbias); /*
                                                                     * and
                                                                     * decreases
                                                                     * by a
                                                                     */

protected static final int radiusdec = 30; /* factor of 1/30 each cycle */

/* defs for decreasing alpha factor */
    protected static final int alphabiasshift = 10; /* alpha starts at 1.0 */

protected static final int initalpha = (((int) 1) << alphabiasshift);
    protected int alphadec; /* biased by 10 bits */

/* radbias and alpharadbias used for radpower calculation */
    protected static final int radbiasshift = 8;
    protected static final int radbias = (((int) 1) << radbiasshift);
    protected static final int alpharadbshift = (alphabiasshift + radbiasshift);
    protected static final int alpharadbias = (((int) 1) << alpharadbshift);

/*
     * Types and Global Variables --------------------------
     */
    protected byte[] thepicture; /* the input image itself */

protected int lengthcount; /* lengthcount = H*W*3 */

protected int samplefac; /* sampling factor 1..30 */

// typedef int pixel[4]; /* BGRc */
    protected int[][] network; /* the network itself - [netsize][4] */

protected int[] netindex = new int[256];
    /* for network lookup - really 256 */
    protected int[] bias = new int[netsize];
    /* bias and freq arrays for learning */
    protected int[] freq = new int[netsize];
    protected int[] radpower = new int[initrad];

/* radpower for precomputation */

/*
     * Initialise network in range (0,0,0) to (255,255,255) and set parameters
     * -----------------------------------------------------------------------
     */
    public NeuQuant(byte[] thepic, int len, int sample) {

int i;
        int[] p;

thepicture = thepic;
        lengthcount = len;
        samplefac = sample;

network = new int[netsize][];
        for (i = 0; i < netsize; i++) {
            network[i] = new int[4];
            p = network[i];
            p[0] = p[1] = p[2] = (i << (netbiasshift + 8)) / netsize;
            freq[i] = intbias / netsize; /* 1/netsize */
            bias[i] = 0;
        }
    }

public byte[] colorMap() {
        byte[] map = new byte[3 * netsize];
        int[] index = new int[netsize];
        for (int i = 0; i < netsize; i++) {
            index[network[i][3]] = i;
        }
        int k = 0;
        for (int i = 0; i < netsize; i++) {
            int j = index[i];
            map[k++] = (byte) (network[j][0]);
            map[k++] = (byte) (network[j][1]);
            map[k++] = (byte) (network[j][2]);
        }
        return map;
    }

/*
     * Insertion sort of network and building of netindex[0..255] (to do after
     * unbias)
     * ------------------------------------------------------------------
     * -------------
     */
    public void inxbuild() {

int i, j, smallpos, smallval;
        int[] p;
        int[] q;
        int previouscol, startpos;

previouscol = 0;
        startpos = 0;
        for (i = 0; i < netsize; i++) {
            p = network[i];
            smallpos = i;
            smallval = p[1]; /* index on g */
            /* find smallest in i..netsize-1 */
            for (j = i + 1; j < netsize; j++) {
                q = network[j];
                if (q[1] < smallval) { /* index on g */
                    smallpos = j;
                    smallval = q[1]; /* index on g */
                }
            }
            q = network[smallpos];
            /* swap p (i) and q (smallpos) entries */
            if (i != smallpos) {
                j = q[0];
                q[0] = p[0];
                p[0] = j;
                j = q[1];
                q[1] = p[1];
                p[1] = j;
                j = q[2];
                q[2] = p[2];
                p[2] = j;
                j = q[3];
                q[3] = p[3];
                p[3] = j;
            }
            /* smallval entry is now in position i */
            if (smallval != previouscol) {
                netindex[previouscol] = (startpos + i) >> 1;
                for (j = previouscol + 1; j < smallval; j++) {
                    netindex[j] = i;
                }
                previouscol = smallval;
                startpos = i;
            }
        }
        netindex[previouscol] = (startpos + maxnetpos) >> 1;
        for (j = previouscol + 1; j < 256; j++) {
            netindex[j] = maxnetpos; /* really 256 */
        }
    }

/*
     * Main Learning Loop ------------------
     */
    public void learn() {

int i, j, b, g, r;
        int radius, rad, alpha, step, delta, samplepixels;
        byte[] p;
        int pix, lim;

if (lengthcount < minpicturebytes) {
            samplefac = 1;
        }
        alphadec = 30 + ((samplefac - 1) / 3);
        p = thepicture;
        pix = 0;
        lim = lengthcount;
        samplepixels = lengthcount / (3 * samplefac);
        delta = samplepixels / ncycles;
        alpha = initalpha;
        radius = initradius;

rad = radius >> radiusbiasshift;
        if (rad <= 1) {
            rad = 0;
        }
        for (i = 0; i < rad; i++) {
            radpower[i] = alpha
                    * (((rad * rad - i * i) * radbias) / (rad * rad));
        }

// fprintf(stderr,"beginning 1D learning: initial radius=%d\n", rad);

if (lengthcount < minpicturebytes) {
            step = 3;
        } else if ((lengthcount % prime1) != 0) {
            step = 3 * prime1;
        } else {
            if ((lengthcount % prime2) != 0) {
                step = 3 * prime2;
            } else {
                if ((lengthcount % prime3) != 0) {
                    step = 3 * prime3;
                } else {
                    step = 3 * prime4;
                }
            }
        }

i = 0;
        while (i < samplepixels) {
            b = (p[pix + 0] & 0xff) << netbiasshift;
            g = (p[pix + 1] & 0xff) << netbiasshift;
            r = (p[pix + 2] & 0xff) << netbiasshift;
            j = contest(b, g, r);

altersingle(alpha, j, b, g, r);
            if (rad != 0) {
                alterneigh(rad, j, b, g, r); /* alter neighbours */
            }

pix += step;
            if (pix >= lim) {
                pix -= lengthcount;
            }

i++;
            if (delta == 0) {
                delta = 1;
            }
            if (i % delta == 0) {
                alpha -= alpha / alphadec;
                radius -= radius / radiusdec;
                rad = radius >> radiusbiasshift;
                if (rad <= 1) {
                    rad = 0;
                }
                for (j = 0; j < rad; j++) {
                    radpower[j] = alpha
                            * (((rad * rad - j * j) * radbias) / (rad * rad));
                }
            }
        }
        // fprintf(stderr,"finished 1D learning: final alpha=%f !\n",((float)alpha)/initalpha);
    }

/*
     * Search for BGR values 0..255 (after net is unbiased) and return colour
     * index
     * --------------------------------------------------------------------
     * --------
     */
    public int map(int b, int g, int r) {

int i, j, dist, a, bestd;
        int[] p;
        int best;

bestd = 1000; /* biggest possible dist is 256*3 */
        best = -1;
        i = netindex[g]; /* index on g */
        j = i - 1; /* start at netindex[g] and work outwards */

while ((i < netsize) || (j >= 0)) {
            if (i < netsize) {
                p = network[i];
                dist = p[1] - g; /* inx key */
                if (dist >= bestd) {
                    i = netsize; /* stop iter */
                } else {
                    i++;
                    if (dist < 0) {
                        dist = -dist;
                    }
                    a = p[0] - b;
                    if (a < 0) {
                        a = -a;
                    }
                    dist += a;
                    if (dist < bestd) {
                        a = p[2] - r;
                        if (a < 0) {
                            a = -a;
                        }
                        dist += a;
                        if (dist < bestd) {
                            bestd = dist;
                            best = p[3];
                        }
                    }
                }
            }
            if (j >= 0) {
                p = network[j];
                dist = g - p[1]; /* inx key - reverse dif */
                if (dist >= bestd) {
                    j = -1; /* stop iter */
                } else {
                    j--;
                    if (dist < 0) {
                        dist = -dist;
                    }
                    a = p[0] - b;
                    if (a < 0) {
                        a = -a;
                    }
                    dist += a;
                    if (dist < bestd) {
                        a = p[2] - r;
                        if (a < 0) {
                            a = -a;
                        }
                        dist += a;
                        if (dist < bestd) {
                            bestd = dist;
                            best = p[3];
                        }
                    }
                }
            }
        }
        return (best);
    }

public byte[] process() {
        learn();
        unbiasnet();
        inxbuild();
        return colorMap();
    }

/*
     * Unbias network to give byte values 0..255 and record position i to
     * prepare for sort
     * ----------------------------------------------------------
     * -------------------------
     */
    public void unbiasnet() {

int i, j;

for (i = 0; i < netsize; i++) {
            network[i][0] >>= netbiasshift;
            network[i][1] >>= netbiasshift;
            network[i][2] >>= netbiasshift;
            network[i][3] = i; /* record colour no */
        }
    }

/*
     * Move adjacent neurons by precomputed alpha*(1-((i-j)^2/[r]^2)) in
     * radpower[|i-j|]
     * ----------------------------------------------------------
     * -----------------------
     */
    protected void alterneigh(int rad, int i, int b, int g, int r) {

int j, k, lo, hi, a, m;
        int[] p;

lo = i - rad;
        if (lo < -1) {
            lo = -1;
        }
        hi = i + rad;
        if (hi > netsize) {
            hi = netsize;
        }

j = i + 1;
        k = i - 1;
        m = 1;
        while ((j < hi) || (k > lo)) {
            a = radpower[m++];
            if (j < hi) {
                p = network[j++];
                try {
                    p[0] -= (a * (p[0] - b)) / alpharadbias;
                    p[1] -= (a * (p[1] - g)) / alpharadbias;
                    p[2] -= (a * (p[2] - r)) / alpharadbias;
                } catch (Exception e) {
                } // prevents 1.3 miscompilation
            }
            if (k > lo) {
                p = network[k--];
                try {
                    p[0] -= (a * (p[0] - b)) / alpharadbias;
                    p[1] -= (a * (p[1] - g)) / alpharadbias;
                    p[2] -= (a * (p[2] - r)) / alpharadbias;
                } catch (Exception e) {
                }
            }
        }
    }

/*
     * Move neuron i towards biased (b,g,r) by factor alpha
     * ----------------------------------------------------
     */
    protected void altersingle(int alpha, int i, int b, int g, int r) {

/* alter hit neuron */
        int[] n = network[i];
        n[0] -= (alpha * (n[0] - b)) / initalpha;
        n[1] -= (alpha * (n[1] - g)) / initalpha;
        n[2] -= (alpha * (n[2] - r)) / initalpha;
    }

/*
     * Search for biased BGR values ----------------------------
     */
    protected int contest(int b, int g, int r) {

/* finds closest neuron (min dist) and updates freq */
        /* finds best neuron (min dist-bias) and returns position */
        /*
         * for frequently chosen neurons, freq[i] is high and bias[i] is
         * negative
         */
        /* bias[i] = gamma*((1/netsize)-freq[i]) */

int i, dist, a, biasdist, betafreq;
        int bestpos, bestbiaspos, bestd, bestbiasd;
        int[] n;

bestd = ~(((int) 1) << 31);
        bestbiasd = bestd;
        bestpos = -1;
        bestbiaspos = bestpos;

for (i = 0; i < netsize; i++) {
            n = network[i];
            dist = n[0] - b;
            if (dist < 0) {
                dist = -dist;
            }
            a = n[1] - g;
            if (a < 0) {
                a = -a;
            }
            dist += a;
            a = n[2] - r;
            if (a < 0) {
                a = -a;
            }
            dist += a;
            if (dist < bestd) {
                bestd = dist;
                bestpos = i;
            }
            biasdist = dist - ((bias[i]) >> (intbiasshift - netbiasshift));
            if (biasdist < bestbiasd) {
                bestbiasd = biasdist;
                bestbiaspos = i;
            }
            betafreq = (freq[i] >> betashift);
            freq[i] -= betafreq;
            bias[i] += (betafreq << gammashift);
        }
        freq[bestpos] += beta;
        bias[bestpos] -= betagamma;
        return (bestbiaspos);
    }
}

package cn.xsshome.imagetool;

import java.awt.image.BufferedImage;
import java.io.File;

import javax.imageio.ImageIO;

import cn.xsshome.imagetool.convert.ImageToChar;
/**
 * 调用示例
 * @author 小帅丶
 *
 */
public class Sample {
    public static void main(String[] args) throws Exception {
        ImageToChar.load("G:/sws2.jpg", "G:/sws/woman3.txt");//静态图片转字符保存为txt文件
        ImageToChar.load("G:/sws5.png", "G:/sws/woman6.txt");//静态图片转字符保存为txt文件
        ImageToChar.loadGif("C:/Users/Administrator/Desktop/页面录屏显示.gif", "F:/gif/");//动图转为动态的字符图片
        BufferedImage bi = null;
        bi = ImageIO.read(new File("G:/body.jpg"));
        String bytePic = ImageToChar.txtToImageByBase64(bi);//静态图转字符 返回转换后图片的base64
        System.out.println(bytePic);
    }
}

项目工程本人已上传,在我的资源自己下载吧

关于抖音图片转文字的代码工程分享相关推荐

  1. 抖音文字时钟壁纸html源码,这次要把抖音网红文字时钟设置为壁纸了~

    原标题:这次要把抖音网红文字时钟设置为壁纸了~ 本文作者 作者:二娃_ https://juejin.im/post/5d52aea86fb9a06ae61aad5b 还记得上篇吗?我们先实现了抖音网 ...

  2. 【自定义View】抖音网红文字时钟-上篇

    源码地址 抖音网红文字时钟-TextClockView 起源 周末在家刷抖音的时候看到了这款网红时钟,都是Android平台的,想来何不自己实现一把.看抖音里大家发的视频,这款时钟基本分两类,一类是展 ...

  3. python制作照片_Python-制作抖音图片

    ---------------------------------------------------------------------------------------------------- ...

  4. 抖音文字时钟壁纸html,网红文字时钟怎么弄 抖音网红文字时钟主题壁纸设置教程...

    类型:桌面主题大小:54KB语言:中文 评分:10.0 标签: 立即下载 最近很多小伙伴在抖音上看到了网红文字时钟主题壁纸,很有意思,自己也想要制作这种的,网红文字时钟怎么弄,西西小编为大家带来抖音网 ...

  5. python爬取抖音用户评论_python实现模拟器爬取抖音评论数据的示例代码

    目标: 由于之前和朋友聊到抖音评论的爬虫,demo做出来之后一直没整理,最近时间充裕后,在这里做个笔记. 提示:大体思路 通过fiddle + app模拟器进行抖音抓包,使用python进行数据整理 ...

  6. Python-制作抖音图片

    request.urlretrieve(img_url, 'background.jpg') 当总结写的假的好么? 然后更改了一下,现在是可以 功能1:批量下载图片 功能2:转化为抖音图片(无论提交外 ...

  7. 抖音小店无货源玩法分享(一)开店前准备,核心思路奉上

    对于普通人来说,抖音绝对是一个不错的机会,但是这个机会又不完全是给普通人的,大部分能在抖音获利的,都是有一技之长,要么卖课,要么卖货,加上团队运作,最终变现. 我这里说的普通人是什么?不会直播,没有什 ...

  8. 使用mui开发的抖音、快手互赞互粉的分享类app

    使用mui开发的抖音.快手互赞互粉的分享类app 应用宝下载地址 mui介绍 最接近原生APP体验的高性能前端框架 mui官方地址 app介绍 应用支持抖音.快手.B站.微视等多个短视频平台的视频分享 ...

  9. 抖音轮播图html代码,抖音上超火的“图片轮播”、“卡点”视频都是怎么做的?...

    原标题:抖音上超火的"图片轮播"."卡点"视频都是怎么做的? 话不多说,直接进入正题. 首先,图片轮播类的视频该怎么做? 抖音视频剪辑 江苏麒麟互娱 步骤一:选 ...

最新文章

  1. W7程序计算机面板介绍,win7系统隐藏任意程序运行界面的详细步骤
  2. map的生命周期 java,【JAVA面试的艺术】JAVA基础知识阶段三
  3. 一些在Android中的小设置~~~持续添加
  4. 小米CC开机动画公布:扑面而来的青春活力
  5. 二、SpringBoot配置文件讲解
  6. Shell(6): 多线程操作及线程数
  7. linux通过xrander添加分辨率,使用X11,XDamage,XRender和其他技巧保留屏幕内容的QPixmap副本...
  8. windows server 2003 系统提权
  9. 如何对开发团队的人员进行绩效管理?
  10. 计算机系云,计算机与信息技术学院
  11. 小木虫好中的ei期刊图像处理
  12. 输入三角形的3条边长(均为正整数),如果不能构成一个三角形,则输出“not a triangle”;如果能够构成一个直角三角形,则输出“yes”;如果不能构成直角三角形,则输出“no”。
  13. Redis非关系型数据库(三)持久化
  14. 如何在微信中做好微信h5棋牌下载类推广防封防屏蔽?
  15. JAVA学习笔记JEECG BOOT介绍
  16. 5y计算机应用选择题答案,2016年电大网考计算机应用基础统考试题模拟真题及答案 含小抄复习资料推荐.docx...
  17. rabbit-1简介
  18. 加速电脑浏览器下载和加载网页速度
  19. Android 如何通过menu id来得到menu item 控件
  20. 为什么互导纳要有个负号?

热门文章

  1. 怎么开发html5页面,一步一步教你如何开发h5页面
  2. python和tkinter实现摄像头实时无闪烁显示
  3. 一文掌握组织项目等级划分维度,标准和实例
  4. 超赞圆形动画进度条,爱了爱了(使用HTML、CSS和bootstrap框架)
  5. 改变 STREAMING 部分--基于MATLAB的LBM代码
  6. macos 彻底删除goland编译器
  7. Powerpoint中用vba进行半角到全角的转换
  8. 全能中间件 REST API 使用手册
  9. Java高级开发工程师面试题
  10. Java工程师面试题整理[社招篇]