jpeg 的有效 base64 字符串

2022-01-21 00:00:00 image base64 javascript

这是一个有效的 base64 格式的 jpeg 字符串吗?

Is this a valid base64 string of jpeg?

<代码>数据:图像/JPEG; BASE64,/9J/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgIDAwMDBAYEBAQEBAgGBgUGCQgKCgkICQkKDA8MCgsOCwkJDRENDg8QEBEQCgwSExIQEw8QEBD/2wBDAQMDAwQDBAgEBAgQCwkLEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBD

有问题的字符是base64"之后的/9j/",

That characters in question are the '/9j/' after the 'base64,'

我猜这可能与压缩有关,因为它是 JPEG.

I'm guessing it could be to do with compression as it's JPEG.

与 png 的 base64 字符串相比,模式不同,即字符模式与上述不同.

As when compared to a base64 string of a png, the pattern is different, i.e. the character pattern is different from above.

<代码>数据:图像/PNG; BASE64,iVBORw0KGgoAAAANSUhEUgAAAe8AAADICAYAAADBV2n3AAAIc0lEQVR4Xu3VgQkAAAgCwdx/6eZ4uCaQM3DnCBAgQIAAgZTAUmmFJUCAAAECBM54ewICBAgQIBATMN6xwsQlQIAAAQLG2w8QIECAAIGYgPGOFSYuAQIECBAw3n6AAAECBAjEBIx3rDBxCRAgQICA8fYDBAgQIEAgJmC8Y4WJS4AAAQIEjLcfIECAAAECMQHjHStMXAIECBAgYLz9AAECBAgQiAkY71hh4hIgQIAAAePtBwgQIECAQEzAeMcKE5cAAQIECBhvP0CAAAECBGICxjtWmLgECBAgQMB4 + wECBAgQIBATMN6xwsQlQIAAAQLG2w8QIECAAIGYgPGOFSYuAQIECBAw3n6AAAECBAjEBIx3rDBxCRAgQICA8fYDBAgQIEAgJmC8Y4WJS4AAAQIEjLcfIECAAAECMQHjHStMXAIECBAgYLz9AAECBAgQiAkY71hh4hIgQIAAAePtBwgQIECAQEzAeMcKE5cAAQIECBhvP0CAAAECBGICxjtWmLgECBAgQMB4 + wECBAgQIBATM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IgQIAAAePtBwgQIECAQEzAeMcKE5cAAQIECBhvP0CAAAECBGICxjtWmLgECBAgQMB4 + wECBAgQIBATMN6xwsQlQIAAAQLG2w8QIECAAIGYgPGOFSYuAQIECBAw3n6AAAECBAjEBIx3rDBxCRAgQIDAA8MHAMnDFd9nAAAAAElFTkSuQmCC

我的问题的根源是我将一组画布编码为 .jpeg 和 .png 的 base64 - .png 的输出很好,而 .jpeg 的输出始终为黑框.

The origin of my problem is that i'm base64 encoding an array of canvases into .jpeg's and .png's - the .png's output fine whereas the .jpeg's always output as black boxes.

我正在使用 html2canvas (javascript) 捕获图像,它们是 html 节点的图像",我已经解决了透明度问题,例如将 dom 节点的背景设置为白色.

I'm capturing the images using html2canvas (javascript), they are 'images' of html nodes, I have played around with transparency issues, like setting the background of the dom nodes to white.

我正在编码的所有 jpeg 图像都有这种/9j/"模式,我在循环中生成它们,所以它很可能是所有图像都具有的共同属性.

All of the jpeg images I am encoding have this '/9j/' pattern, i'm generating them in a loop so it may well be a common property all the images have.

我似乎也无法在网上找到合适的工具来验证 base64 字符串.

I can't seem to find a decent tool online to validate base64 strings either.

推荐答案

我们来看看:

$ echo -n '/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAMCAgICAgMCAgID' | base64 --decode | file -
/dev/stdin: JPEG image data, JFIF standard 1.01

因此它被识别为有效的 JPEG 标头(但这并不一定意味着整个 JPEG 数据都是有效的,但至少它看起来像 JPEG).

So it's recognized as a valid JPEG header (this doesn't necessarily mean that the entire JPEG data is valid, though, but at least it looks like a JPEG).

JPEG 数据和 PNG 数据的区别在于文件格式不同.此外,/ 是 Base64 中的有效字符.

The difference between the JPEG data and the PNG data is because the file formats are different. Also, / is a valid character in Base64.

相关文章