将画布的内容发送到 Java 服务器并将其保存为图像
好的,基本上我已经开发了一个简单的图像上传系统。用户选择本地图像(使用 HTML5 File / FileReader API),并能够在确认结果之前对其进行裁剪。
最终结果在画布中查看,以便将其发送到我使用 toDataURL 的服务器。后端服务器是一个 NodeJS 服务器,然后需要对 Java 服务器进行 REST 调用,该服务器将从数据创建图像文件并将其保存到磁盘。
toDataURL 的结果格式为:data:image/png;base64, ENCODED DATA。
Java 服务器上需要什么才能将字符串转换为正确的二进制表示形式?
Okay, basically I've developed a simple image upload system. The user selects a local image (using the HTML5 File / FileReader API) and has the ability to crop it before confirming the result.
The final result is viewed in a canvas so to send it to the server I'm using toDataURL. The backend server is a NodeJS server which then needs to make a REST call to a Java server which will create an image file from the data and save it to disk.
The results of toDataURL are in the form: data:image/png;base64, ENCODED DATA.
What would I need on the Java server to convert the string into it's proper binary representation?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您需要删除
data:image/png;base64,
部分和 Base 64 解码 其余部分。You need to remove the
data:image/png;base64,
part and base 64 decode the rest.对字符串进行 Base64 解码后,您将获得 PNG 文件形式的二进制图像。有关如何将 base64 字符串解码为字节的详细信息,请参阅这个问题。
Once you Base64-decode the string, you will have the binary image, in the form of a PNG file. See this SO question for details on how to decode a base64 string into bytes.
如果你的base64Image有空格字符,你必须用+替换空格,那么你必须从base64Image的开头删除data:image/png;base64。除非替换空格字符,否则无法获得正确的图像。那么你可以使用 Base64 解码
yourBase64String = yourBase64String.replace(' ', '+');
yourBase64String = yourBase64String.substring(22);
You have to replace space with + if your base64Image have space char, then you have to remove data:image/png;base64, from the beginning of the base64Image. Unless you replace space char, you can't get correct Image. then you can use Base64 decode
yourBase64String = yourBase64String.replace(' ', '+');
yourBase64String = yourBase64String.substring(22);