在客户端和服务器之间将图像作为字节数组发送 [英] Sending an image as a byte array between client and server

查看:99
本文介绍了在客户端和服务器之间将图像作为字节数组发送的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以成功地从我的客户端向我的服务器发送并调整一个经过调整的125 x 125图像。唯一的问题是,这太小了。我希望能够发送更大的图像,但字节数组无法处理它,我得到一个Java堆异常。目前我用它来解释我的形象。是否有更有效的方式?

I can successfully send and draw a resized, 125 x 125 image from my client to my server. only problem is, thats way too small. I want to be able to send a larger image but the byte array can't handle it and I get a java heap exception. currently I'm using this to interpret my image. Is there a more efficient way?

在客户端

screenShot = new Robot().createScreenCapture(new Rectangle(Toolkit.getDefaultToolkit().getScreenSize()));
                screenShot = resize(screenShot, 125, 125);

                ByteArrayOutputStream byteArrayO = new ByteArrayOutputStream();
                ImageIO.write(screenShot,"PNG",byteArrayO);
                byte [] byteArray = byteArrayO.toByteArray();
                out.writeLong(byteArray.length);
                out.write(byteArray);

调整上面调用的方法。

    public static BufferedImage resize(BufferedImage img, int newW, int newH) {  
    int w = img.getWidth();  
    int h = img.getHeight();  
    BufferedImage dimg = new BufferedImage(newW, newH, img.getType()); 
    Graphics2D g = dimg.createGraphics();  
    g.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
    RenderingHints.VALUE_INTERPOLATION_BILINEAR);  
    g.drawImage(img, 0, 0, newW, newH, 0, 0, w, h, null);  
    g.dispose();  
    return dimg;  
   } 

解释图像的服务器

in = new DataInputStream(Client.getInputStream());
            long nbrToRead = in.readLong();
            byte[] byteArray = new byte[(int) nbrToRead];
            int nbrRd = 0;
            int nbrLeftToRead = (int) nbrToRead;

            while (nbrLeftToRead > 0) {
                int rd = in.read(byteArray, nbrRd, nbrLeftToRead);
                if (rd < 0)
                    break;
                nbrRd += rd; // accumulate bytes read
                nbrLeftToRead -= rd;
            }
            ByteArrayInputStream byteArrayI = new ByteArrayInputStream(
                    byteArray);
            image = ImageIO.read(byteArrayI);

            if (image != null) {
                paint(f.getGraphics(), image);
            } else {
                System.out.println("null image.");
            }

因为你可以看出代码很大并且很可能效率低下。我可以将图像的1/10发送10次用于和高度,而不是绘制这些部分,但我想知道是否有更简单的方法来做到这一点。

as you can tell the code is massive and most likely inefficient. I could send 1/10 of the image 10 times for with and height, drawing on those parts instead but I wanted to know if there was an easier way to do this.

推荐答案

您应该考虑通过网络将数据传输为流。您可以使用第三方库,如 RMIIO 。如果您可以使用Web服务进行数据传输,那么您可以查看消息传输优化机制(MTOM),它允许您以更有效的方式作为流传输数据。有关详细信息,请查看这里

You should probably think of transferring data as stream over the network. You can make use of third-party libraries like RMIIO . In case you can make data transfer using web service then you can look at Message Transmission Optimization Mechanism (MTOM) which lets you transfer data as stream in more efficient manner. For more details please have a look here

这篇关于在客户端和服务器之间将图像作为字节数组发送的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆