将画布的内容发送到 Java 服务器并将其保存为图像

发布于 2024-12-25 16:41:29 字数 309 浏览 3 评论 0原文

好的,基本上我已经开发了一个简单的图像上传系统。用户选择本地图像(使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

云归处 2025-01-01 16:41:29

您需要删除 data:image/png;base64, 部分和 Base 64 解码 其余部分。

You need to remove the data:image/png;base64, part and base 64 decode the rest.

梦中楼上月下 2025-01-01 16:41:29
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import javax.imageio.ImageIO;
import javax.xml.bind.DatatypeConverter;

public class test {
    public static void main (String[] args){
     try{
            // remove data:image/png;base64, and then take rest sting
            String img64 = "64 base image data here";
        byte[] decodedBytes = DatatypeConverter.parseBase64Binary(img64 );
        BufferedImage bfi = ImageIO.read(new ByteArrayInputStream(decodedBytes));    
        File outputfile = new File("saved.png");
        ImageIO.write(bfi , "png", outputfile);
        bfi.flush();
     }catch(Exception e)
         {  
          //Implement exception code    
     }

    }
}
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import javax.imageio.ImageIO;
import javax.xml.bind.DatatypeConverter;

public class test {
    public static void main (String[] args){
     try{
            // remove data:image/png;base64, and then take rest sting
            String img64 = "64 base image data here";
        byte[] decodedBytes = DatatypeConverter.parseBase64Binary(img64 );
        BufferedImage bfi = ImageIO.read(new ByteArrayInputStream(decodedBytes));    
        File outputfile = new File("saved.png");
        ImageIO.write(bfi , "png", outputfile);
        bfi.flush();
     }catch(Exception e)
         {  
          //Implement exception code    
     }

    }
}
随风而去 2025-01-01 16:41:29

对字符串进行 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.

蒲公英的约定 2025-01-01 16:41:29

如果你的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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文