如何将图片转换为base64呢
管理员

如题

最后由 O了吗 编辑

百度一下,有好多在线转换

我按照网络上找到的编码规则,自己写的,可能有些错误。因为起名困难,所以有些变量的名字有点奇葩,但还能读的。

let path = ""; // 图片路径
path = fileToBase(path);
log(path);

function fileToBase(p) { // 文件编码
    let data = files.readBytes(p);
    data = byteToBit(data);
    // 补位
    let lack = 6 - data.length % 6;
    for (let i = 0; i < lack; i++) data += "0";
    data = bitToBase(data);
    // 加入文件信息
    data = "data:image/jpeg;base64," + data;
    for (let i = 0; i < lack / 2; i++) data += "=";
    return data;
}

function byteToBit(b) { // 字节数组转二进制字符串
    let r = "";
    b.forEach((i) => {
        // 判断符号位
        if (i < 0) {
            i = (-i).toString(2).split("");
            i.reverse();
            i = complement(i);
            i[7] = "1";
        } else {
            i = i.toString(2).split("");
            i.reverse();
            for (let j = i.length; j < 8; j++) i[j] = "0";
        }
        i.reverse();
        r += i.join("");
    });
    return r;
}

function bitToBase(b) { // 编码器
    // 分组(6bit 一组)
    let bit = [];
    for (let i = 0; i < b.length; i += 6)
        bit.push(b.substr(i, 6));
    // 建立编码对照表
    let code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
        "abcdefghijklmnopqrstuvwxyz" +
        "0123456789+/";
    code = code.split("");
    let base = "";
    bit.forEach((i) => {
        i = i.split("");
        let n = 0;
        for (let j = 0; j < 6; j++) {
            if (i[j] == "1") n += Math.pow(2, 5 - j);
        }
        base += code[n];
    });
    return base;
}

function complement(n) { // 求补码
    // 取反
    for (let i = 0; i < 7; i++) {
        if (n[i] == undefined) n[i] = "0";
        if (n[i] == "0") n[i] = "1";
        else n[i] = "0";
    }
    // 加一
    for (let i = 0; i < 7; i++) {
        if (n[i] == "0") {
            n[i] = "1";
            break;
        } else n[i] = "0";
    }
    return n;
}
管理员

@zhan-0 谢谢大佬,非常感谢🌷🌷

1、简化进制转换
2、优化部分逻辑
2、添加文件类型判断(jpg,gif,png)

let path = ""; // 图片路径
path = fileToBase(path);
console.log(path);

function fileToBase(p) { // 文件编码
    let data, lack;
    data = byteToBit(files.readBytes(p));
    // 补位
    lack = 6 - data.length % 6;
    for (let i = 0; i < lack; i++) {
        data += "0";
    }
    data = bitToBase(data);
    // 加入文件信息
    data = "data:" + fileType(p) + ";base64," + data;
    for (let i = 0; i < lack / 2; i++) {
        data += "=";
    }
    return data;
}

function byteToBit(b) { // 字节数组转二进制字符串
    let bit = "";
    b.forEach((i) => {
        // 判断符号位
        let mark = i < 0 ? true : false;
        i = Math.abs(i).toString(2).split("");
        i.reverse();
        if (mark) {
            i = complement(i);
            i[7] = "1";
        } else {
            for (let j = i.length; j < 8; j++) {
                i[j] = "0";
            }
        }
        i.reverse();
        bit += i.join("");
    });
    return bit;
}

function bitToBase(b) { // 编码器
    let bit = [],
        base = "",
        code;
    // 分组(6位一组)
    for (let i = 0; i < b.length; i += 6) {
        bit.push(b.substr(i, 6));
    }
    // 建立编码对照表
    code = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
        "abcdefghijklmnopqrstuvwxyz" +
        "0123456789+/";
    code = code.split("");
    bit.forEach((i) => {
        base += code[parseInt(i, 2)];
    });
    return base;
}

function complement(n) { // 计算补码(补码的补码为原码)
    // 取反
    for (let i = 0; i < 7; i++) {
        if (n[i] == undefined) n[i] = "0";
        if (n[i] == "0") n[i] = "1";
        else n[i] = "0";
    }
    // 加一
    for (let i = 0; i < 7; i++) {
        if (n[i] == "0") {
            n[i] = "1";
            break;
        } else n[i] = "0";
    }
    return n;
}

function fileType(f) { // 文件类型判断
    // 文件后缀列表
    let img = ["jpeg", "png", "gif"],
        txt = ["css", "html", "js"];
    f = files.getExtension(f);
    if (f == "jpg") f = img[0]; // jpg 是 jpeg 的简写
    for (let i = 0; i < img.length; i++) {
        if (f == img[i]) {
            return "image/" + img[i];
        }
    }
    for (let i = 0; i < txt.length; i++) {
        if (f == txt[i]) {
            return "text/" + txt[i];
        }
    }
    // 错误提示
    f = "不支持此类型文件!";
    toast(f);
    console.error(f);
}
管理员

@zhan-0 谢谢大佬,谢谢,你知道怎么调用百度识图api吗?

@o了吗如何将图片转换为base64呢 中说:

@zhan-0 谢谢大佬,谢谢,你知道怎么调用百度识图api吗?

我通过抓包,抓到了一个 API,但 Auto.js 提供的 Http 模块里面的 http.postMultipart() 不能在上传文件时附加头信息。

POST /n/image?fr=form&target=pcSearchImage&needText=true HTTP/1.1
Host: image.baidu.com
Connection: keep-alive
Content-Length: 1268
Accept: */*
Origin: https://image.baidu.com
X-Requested-With: XMLHttpRequest
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.73 Safari/537.36
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryy4lc41n2nBzTbQ86
Referer: https://image.baidu.com/n/pc_search
Accept-Encoding: gzip, deflate
Accept-Language: zh-CN,en-US;q=0.9
Cookie: sttbHint=sttbHintShow; shituhistory=%7B%220%22%3A%22http%3A%2F%2Fd.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F3bf33a87e950352aad36f80c5f43fbf2b2118b5b.jpg%22%2C%221%22%3A%22http%3A%2F%2Ff.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fb151f8198618367aef2086d922738bd4b31ce50d.jpg%22%2C%222%22%3A%22http%3A%2F%2Fg.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2F241f95cad1c8a7862f97b70a6b09c93d70cf5071.jpg%22%2C%223%22%3A%22http%3A%2F%2Fd.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Ffaedab64034f78f0d49968f775310a55b3191c27.jpg%22%2C%224%22%3A%22http%3A%2F%2Fc.hiphotos.baidu.com%2Fimage%2Fpic%2Fitem%2Fc9fcc3cec3fdfc03689110bcd83f8794a5c226cd.jpg%22%7D; BAIDUID=8FA575190E10E28116ACF1026CECFEF9:FG=1; BIDUPSID=8FA575190E10E28116ACF1026CECFEF9; PSTM=1519211756; BDRCVFR[fBLL8ZbbiMm]=WH-qyqYMsFCmv9EUhIBIykdUz4WUvY; H_WISE_SIDS=122758_100806_121885_122156_100100_122640_120140_121999_118886_118870_118851_118833_118786_120549_107312_117336_121862_117435_122789_122735_122138_122672_120852_122542_120035_116408_122624_122498_110085_122303; PSINO=3; __cfduid=db8860ba6e8c652f7f09cca217c5de1291521374571; BDRCVFR[kAjskz-82g3]=mk3SLVN4HKm; FEEDSID=30D852BBF580132BA666A3564D0AC780; BDRCVFR[5GQZCjFg8mf]=mk3SLVN4HKm; userFrom=null; firstShowTip=1; tip_show_limit=5
管理员

@zhan-0 厉害了:+1: :+1: ,谢谢大佬

再次改进,编码速度提升 10~20%

let path = ""; // 图片路径
path = fileToBase(path);
log(path);

function fileToBase(p) { // 文件编码
    let data, lack;
    data = byteToBit(files.readBytes(p));
    // 补位
    lack = 6 - data.length % 6;
    for (let i = 0; i < lack; i++) {
        data += "0";
    }
    data = bitToBase(data);
    // 加入文件信息
    data = "data:" + fileType(p) + ";base64," + data;
    for (let i = 0; i < lack / 2; i++) {
        data += "=";
    }
    return data;
}

function byteToBit(b) { // 字节数组转二进制字符串
    let bit = "";
    b.forEach((i) => {
        i = (i < 0 ? i + 256 : i).toString(2);
        if (i.length < 8) {
            // 字节补位
            i = i.split("");
            i.reverse();
            for (let j = i.length; j < 8; j++) {
                i[j] = "0";
            }
            i.reverse();
            i = i.join("");
        }
        bit += i;
    });
    return bit;
}

function bitToBase(b) { // 编码器
    let code = [],
        base = "",
        key;
    // 分组,6 位一组
    for (let i = 0; i < b.length; i += 6) {
        code.push(b.substr(i, 6));
    }
    // 建立编码对照表
    key = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/".split("");
    code.forEach((i) => {
        base += key[parseInt(i, 2)];
    });
    return base;
}

function fileType(f) { // 文件类型判断
    // 文件后缀列表
    let img = ["jpeg", "png", "gif"],
        txt = ["css", "html", "js"];
    f = files.getExtension(f);
    if (f == "jpg") f = "jpeg"; // jpg 是 jpeg 的简写
    for (let i = 0; i < img.length; i++) {
        if (f == img[i]) {
            return "image/" + img[i];
        }
    }
    for (let i = 0; i < txt.length; i++) {
        if (f == txt[i]) {
            return "text/" + txt[i];
        }
    }
    // 错误提示
    f = "不支持此类型文件!";
    toast(f);
    console.error(f);
    exit();
}
最后由 Zhan 0 编辑

用android原生的的速度更快。

  • 10
    帖子
  • 1952
    浏览

与 Auto.js 的连接断开,我们正在尝试重连,请耐心等待