• <bdo id='X1wot'></bdo><ul id='X1wot'></ul>
    <legend id='X1wot'><style id='X1wot'><dir id='X1wot'><q id='X1wot'></q></dir></style></legend>

    <tfoot id='X1wot'></tfoot>

    <small id='X1wot'></small><noframes id='X1wot'>

    <i id='X1wot'><tr id='X1wot'><dt id='X1wot'><q id='X1wot'><span id='X1wot'><b id='X1wot'><form id='X1wot'><ins id='X1wot'></ins><ul id='X1wot'></ul><sub id='X1wot'></sub></form><legend id='X1wot'></legend><bdo id='X1wot'><pre id='X1wot'><center id='X1wot'></center></pre></bdo></b><th id='X1wot'></th></span></q></dt></tr></i><div id='X1wot'><tfoot id='X1wot'></tfoot><dl id='X1wot'><fieldset id='X1wot'></fieldset></dl></div>

        如何从 Java websocket 服务器发送图像以在 HTML5 画布中使用?

        时间:2023-10-14
          <tbody id='m9Y45'></tbody>

      1. <legend id='m9Y45'><style id='m9Y45'><dir id='m9Y45'><q id='m9Y45'></q></dir></style></legend>

        <small id='m9Y45'></small><noframes id='m9Y45'>

        • <tfoot id='m9Y45'></tfoot>
          <i id='m9Y45'><tr id='m9Y45'><dt id='m9Y45'><q id='m9Y45'><span id='m9Y45'><b id='m9Y45'><form id='m9Y45'><ins id='m9Y45'></ins><ul id='m9Y45'></ul><sub id='m9Y45'></sub></form><legend id='m9Y45'></legend><bdo id='m9Y45'><pre id='m9Y45'><center id='m9Y45'></center></pre></bdo></b><th id='m9Y45'></th></span></q></dt></tr></i><div id='m9Y45'><tfoot id='m9Y45'></tfoot><dl id='m9Y45'><fieldset id='m9Y45'></fieldset></dl></div>
              • <bdo id='m9Y45'></bdo><ul id='m9Y45'></ul>

                  本文介绍了如何从 Java websocket 服务器发送图像以在 HTML5 画布中使用?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个用 Java 实现的 WebSocket 服务器.当客户端连接时,我想通过此连接发送图像供客户端在画布元素中使用.我想出了以下服务器代码:

                  public void onOpen(Connection connection) {尝试 {BufferedImage image = ImageIO.read(new File("image.jpg"));ByteArrayOutputStream baos = new ByteArrayOutputStream();ImageIO.write(image, "jpg", baos);byte[] byteArray = baos.toByteArray();connection.sendMessage(byteArray, 0, byteArray.length);} 捕捉(异常 e){System.out.println("错误:"+e.getMessage());}}

                  客户端 Javascript 如下所示:

                  onmessage : function(m) {如果(m.data){if (m.data instanceof Blob) {var blob = m.data;var bytes = new Uint8Array(blob);var image = context.createImageData(canvas.width, canvas.height);for (var i=0; i

                  连接正常并发送数据(blob.size 具有正确的值),但图像未绘制到画布上.Firefox 给我错误消息TypeError:值无法转换为任何一个:HTMLImageElement、HTMLCanvasElement、HTMLVideoElement.".

                  我知道使用 WebSockets 并不是向客户端发送图像的最佳方式.发送图片后,WebSocket 仅用于发送短信.

                  为了将图像发送到画布上,我需要进行哪些更改?

                  使用的资源:

                  如何在java中将图像转换为字节数组?

                  在 WebSocket 中接收 Blob 并在中呈现为图像帆布

                  解决方案

                  发送前尝试将图片转base64,例如:

                  函数drawImage(imgString){var canvas = document.getElementById("canvas");var ctx = canvas.getContext("2d");变种图像 = 新图像();image.src = imgString;image.onload = 函数() {ctx.drawImage(图像, 0, 0);};}

                  这里一个关于如何在Java中将图像转换为base64的链接p>

                  I have a WebSocket server implemented in Java. When a client connects I want to send an image over this connection for the client to use in a canvas element. I have come up with the following server code:

                  public void onOpen(Connection connection) {
                      try {
                          BufferedImage image = ImageIO.read(new File("image.jpg"));
                          ByteArrayOutputStream baos = new ByteArrayOutputStream();
                          ImageIO.write(image, "jpg", baos);
                          byte[] byteArray = baos.toByteArray();
                          connection.sendMessage(byteArray, 0, byteArray.length);
                      } catch (Exception e ){
                          System.out.println("Error: "+e.getMessage());
                      }
                  }
                  

                  The client-side Javascript looks like this:

                  onmessage : function(m) {
                      if (m.data) {
                          if (m.data instanceof Blob) {
                              var blob = m.data;
                  
                              var bytes = new Uint8Array(blob);
                              var image = context.createImageData(canvas.width, canvas.height);
                              for (var i=0; i<bytes.length; i++) {
                                  image.data[i] = bytes[i];
                              }
                          }
                      }
                  }
                  

                  The connection works and the data is sent (blob.size has the correct value), but the image is not drawn onto the canvas. Firefox gives me the error message "TypeError: Value could not be converted to any of: HTMLImageElement, HTMLCanvasElement, HTMLVideoElement.".

                  I am aware of the fact that this using WebSockets is not the best way to send an image to the client. After sending the image the WebSocket is only used to send text messages.

                  What do I need to change for the image to be sent an applied to the canvas?

                  Resources used:

                  how to convert image to byte array in java?

                  Receive Blob in WebSocket and render as image in Canvas

                  解决方案

                  Try converting the image to base64 before sending, for example:

                  function drawImage(imgString){
                      var canvas = document.getElementById("canvas");
                      var ctx = canvas.getContext("2d");
                  
                      var image = new Image();
                      image.src = imgString;
                      image.onload = function() {
                          ctx.drawImage(image, 0, 0);
                      };
                  }
                  

                  Here's a link on how to convert the image to base64 in Java

                  这篇关于如何从 Java websocket 服务器发送图像以在 HTML5 画布中使用?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  • <bdo id='hA6Lk'></bdo><ul id='hA6Lk'></ul>
                    1. <small id='hA6Lk'></small><noframes id='hA6Lk'>

                    2. <tfoot id='hA6Lk'></tfoot>

                        <i id='hA6Lk'><tr id='hA6Lk'><dt id='hA6Lk'><q id='hA6Lk'><span id='hA6Lk'><b id='hA6Lk'><form id='hA6Lk'><ins id='hA6Lk'></ins><ul id='hA6Lk'></ul><sub id='hA6Lk'></sub></form><legend id='hA6Lk'></legend><bdo id='hA6Lk'><pre id='hA6Lk'><center id='hA6Lk'></center></pre></bdo></b><th id='hA6Lk'></th></span></q></dt></tr></i><div id='hA6Lk'><tfoot id='hA6Lk'></tfoot><dl id='hA6Lk'><fieldset id='hA6Lk'></fieldset></dl></div>

                        <legend id='hA6Lk'><style id='hA6Lk'><dir id='hA6Lk'><q id='hA6Lk'></q></dir></style></legend>
                          <tbody id='hA6Lk'></tbody>