调用实例

require("ZZBase64")
local files
local file = io.open("E:\\2342.jpg","rb") if file then
print "发现文件"
files = file:read("*a")
file:close()
else
print "没有找到文件"
end --图片转成base64文本
print(ZZBase64.encode(files))

帮助类

ZZBase64 = {}
local string = string ZZBase64.__code = {
'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f',
'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
'w', 'x', 'y', 'z', '', '', '', '', '', '', '', '', '', '', '+', '/',
};
ZZBase64.__decode = {}
for k,v in pairs(ZZBase64.__code) do
ZZBase64.__decode[string.byte(v,)] = k -
end function ZZBase64.encode(text)
local len = string.len(text)
local left = len %
len = len - left
local res = {}
local index =
for i = , len, do
local a = string.byte(text, i )
local b = string.byte(text, i + )
local c = string.byte(text, i + )
-- num = a<<16 + b<<8 + c
local num = a * + b * + c
for j = , do
--tmp = num >> ((4 -j) * 6)
local tmp = math.floor(num / ( ^ ((-j) * )))
--curPos = tmp&0x3f
local curPos = tmp % +
res[index] = ZZBase64.__code[curPos]
index = index +
end
end if left == then
ZZBase64.__left1(res, index, text, len)
elseif left == then
ZZBase64.__left2(res, index, text, len)
end
return table.concat(res)
end function ZZBase64.__left2(res, index, text, len)
local num1 = string.byte(text, len + )
num1 = num1 * --lshift 10
local num2 = string.byte(text, len + )
num2 = num2 * --lshift 2
local num = num1 + num2 local tmp1 = math.floor(num / ) --rShift 12
local curPos = tmp1 % +
res[index] = ZZBase64.__code[curPos] local tmp2 = math.floor(num / )
curPos = tmp2 % +
res[index + ] = ZZBase64.__code[curPos] curPos = num % +
res[index + ] = ZZBase64.__code[curPos] res[index + ] = "="
end function ZZBase64.__left1(res, index,text, len)
local num = string.byte(text, len + )
num = num * tmp = math.floor(num / )
local curPos = tmp % +
res[index ] = ZZBase64.__code[curPos] curPos = num % +
res[index + ] = ZZBase64.__code[curPos] res[index + ] = "="
res[index + ] = "="
end function ZZBase64.decode(text)
local len = string.len(text)
local left =
if string.sub(text, len - ) == "==" then
left =
len = len -
elseif string.sub(text, len) == "=" then
left =
len = len -
end local res = {}
local index =
local decode = ZZBase64.__decode
for i =, len, do
local a = decode[string.byte(text,i )]
local b = decode[string.byte(text,i + )]
local c = decode[string.byte(text,i + )]
local d = decode[string.byte(text,i + )] --num = a<<18 + b<<12 + c<<6 + d
local num = a * + b * + c * + d local e = string.char(num % )
num = math.floor(num / )
local f = string.char(num % )
num = math.floor(num / )
res[index ] = string.char(num % )
res[index + ] = f
res[index + ] = e
index = index +
end if left == then
ZZBase64.__decodeLeft1(res, index, text, len)
elseif left == then
ZZBase64.__decodeLeft2(res, index, text, len)
end
return table.concat(res)
end function ZZBase64.__decodeLeft1(res, index, text, len)
local decode = ZZBase64.__decode
local a = decode[string.byte(text, len + )]
local b = decode[string.byte(text, len + )]
local c = decode[string.byte(text, len + )]
local num = a * + b * + c local num1 = math.floor(num / ) %
local num2 = math.floor(num / ) %
res[index] = string.char(num1)
res[index + ] = string.char(num2)
end function ZZBase64.__decodeLeft2(res, index, text, len)
local decode = ZZBase64.__decode
local a = decode[string.byte(text, len + )]
local b = decode[string.byte(text, len + )]
local num = a * + b
num = math.floor(num / )
res[index] = string.char(num)
end

lua 把图片转换成base64的更多相关文章

  1. java 图片转换成base64字符串

    import java.io.ByteArrayOutputStream; import java.io.FileInputStream;import java.io.FileOutputStream ...

  2. js绝对地址图片转换成base64的方法

    //将图片转换成base64 function getBase64Image(url, callback){ var canvas = document.createElement('canvas') ...

  3. Java对网络图片/本地图片转换成Base64编码和解码

    一.将本地图片转换成Base64编码字符串 /** * 将本地图片转换成Base64编码字符串 * * @param imgFile 图片目录路径 * @return */ public static ...

  4. delphi将图片转换成Base64编码函数

    {************************************************************************** 名称: BaseImage 参数: fn: TF ...

  5. Base64字符保存图片,图片转换成Base64字符编码

    //文件转换成Base64编码 public static String getFileBase64Str(String filePath) throws IOException { String f ...

  6. 利用PHP将图片转换成base64编码的实现方法

    先来说一下为什么我们要对图片base64编码 base64是当前网络上最为常见的传输8Bit字节代码的编码方式其中之一.base64主要不是加密,它主要的用途是把某些二进制数转成普通字符用于网络传输. ...

  7. 图片转换成Base64编码集成到html文件

    首先为什么要这么做?  原因很简单这样可以减少与服务器的请求,当然对于一些浏览器并不支持,如IE8.通常用在手机版网站中,具体转化方法如下: 1.在线打开Base64的编码器将图片编码成Base64 ...

  8. JS将图片转换成Base64码

    直接上代码 html页面代码 <!DOCTYPE html> <html lang="en"> <head> <meta charset= ...

  9. data:image/png;base64 上传图像将图片转换成base64格式

    大家可能注意到了,网页上有些图片的src或css背景图片的url后面跟了一大串字符,比如: data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAkAAAAJ ...

随机推荐

  1. MFC 选择文件夹

    WCHAR szPath[_MAX_PATH] = {}; BROWSEINFO bi; //指定父窗口,在对话框显示期间,父窗口将被禁用 bi.hwndOwner = this->GetSaf ...

  2. Day2-T1

    原题目 Describe:贪心,左边和右边中选字典序小的 code: #include<bits/stdc++.h> using namespace std; int n,step,hea ...

  3. python利用百度云接口实现车牌识别

    一个小需求---实现车牌识别. 目前有两个想法 调云在线的接口或者使用SDK做开发(配置环境和编译第三方库很麻烦,当然使用python可以避免这些问题) 自己实现车牌识别算法(复杂) ! 一开始准备使 ...

  4. PAT Advanced 1098 Insertion or Heap Sort (25) [heap sort(堆排序)]

    题目 According to Wikipedia: Insertion sort iterates, consuming one input element each repetition, and ...

  5. ✨vue引入组件 axios和icont矢量图标

    axios 在vue项目开发中,我们使用axios进行ajax请求,很多人一开始使用axios的方式,会当成vue-resoure的使用方式来用,即在主入口文件引入import VueResource ...

  6. jQuery选择器全解析

    1. 基本选择器 1.1 id选择器:$(#id) <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" ...

  7. 51nod 1080:两个数的平方和

    1080 两个数的平方和 基准时间限制:1 秒 空间限制:131072 KB 分值: 5 难度:1级算法题  收藏  关注 给出一个整数N,将N表示为2个整数i j的平方和(i <= j),如果 ...

  8. 查询内核符号链接的信息的API

    NtOpenSymbolicLinkObject和NtQuerySymbolicLinkObject获取指定符号链接的信息 版权声明:本文为博主原创文章,未经博主允许不得转载.

  9. CTF -攻防世界-web新手区

    直接f12出来 先用get后加/?a=1 然后火狐装hackbar(老版本)f12 post b=2 搞定 Hackbar:https://github.com/Mr-xn/hackbar2.1.3 ...

  10. mybatis中#{}和${}的区别及order by的sql注入问题

    mybatis的#{}和${}的区别以及order by注入问题 原文  http://www.cnblogs.com/chyu/p/4389701.html   前言略,直奔主题.. #{}相当于j ...