惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

IT之家
IT之家
NISL@THU
NISL@THU
The Hacker News
The Hacker News
C
CXSECURITY Database RSS Feed - CXSecurity.com
T
Tenable Blog
Forbes - Security
Forbes - Security
V2EX - 技术
V2EX - 技术
Webroot Blog
Webroot Blog
Schneier on Security
Schneier on Security
T
The Exploit Database - CXSecurity.com
T
Tor Project blog
C
Cisco Blogs
TaoSecurity Blog
TaoSecurity Blog
The Last Watchdog
The Last Watchdog
PCI Perspectives
PCI Perspectives
O
OpenAI News
C
Cyber Attacks, Cyber Crime and Cyber Security
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Google Online Security Blog
Google Online Security Blog
宝玉的分享
宝玉的分享
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More
量子位
D
Docker
AI
AI
Blog — PlanetScale
Blog — PlanetScale
S
Security @ Cisco Blogs
S
Schneier on Security
The GitHub Blog
The GitHub Blog
W
WeLiveSecurity
云风的 BLOG
云风的 BLOG
M
MIT News - Artificial intelligence
P
Privacy International News Feed
Cyber Security Advisories - MS-ISAC
Cyber Security Advisories - MS-ISAC
H
Hackread – Cybersecurity News, Data Breaches, AI and More
B
Blog
C
Check Point Blog
A
About on SuperTechFans
D
Darknet – Hacking Tools, Hacker News & Cyber Security
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
Application and Cybersecurity Blog
Application and Cybersecurity Blog
Engineering at Meta
Engineering at Meta
I
InfoQ
T
Threat Research - Cisco Blogs
Project Zero
Project Zero
Cloudbric
Cloudbric
MongoDB | Blog
MongoDB | Blog
Cisco Talos Blog
Cisco Talos Blog
L
Lohrmann on Cybersecurity
S
Securelist

轶哥博客

blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog blog
blog
轶哥 · 2019-02-23 · via 轶哥博客

部分业务中会碰到Base64图片文本。

例如:

  • 微信JS-SDK - “拍照或从手机相册中选图接口”,wx.chooseImage与wx.request结合获取Base64数据。“获取本地图片接口”,wx.getLocalImgData返回Base64数据。
  • 微信小程序中,图片访问地址受到白名单限制,有可能采用base64数据直接在接口返回。
  • 某种读卡器获取到的图片数据以Base64格式返回。

如果返回的图片数据中不包含格式前缀(甚至某个微信接口在ios中错误返回image/jgp类型),那就需要推测图片类型。

JS推测代码:

function guessImageTypeFromBase64 (str) {
    switch(str.charAt(0)) {
        case '/':
            return 'image/jpeg';
        case 'i':
            return 'image/png';
        case 'R':
            return 'image/gif';
        case 'U':
            return 'image/webp';
        case 'Q':
            return 'image/bmp';
        default:
            return null;
    }
}

function getCompleteImageBase64 (str) {
    return 'data:' + (guessImageTypeFromBase64(str) || 'image/jpeg') + ';base64,' + str
}

附:

JAVA推测思路

fileType = HttpURLConnection.guessContentTypeFromStream(typeStream); // 从前几个字节流中判断文件类型,得到MIME类型结果,再由MIME类型,拼接Base64图片前缀

提示: 使用Base64传输图片数据在很多情况下不是最佳方案。如果你的需求仅是方便的存储和获取图片,可以选择将图片存储至各大云的对象存储或者各种云存储,通常他们提供免费的图片处理接口,在获取图片的同时转换图片格式或者调整图片大小和质量。