通过套接字发送BufferedImage,非常慢

时间:2014-12-08 22:56:34

标签: java sockets bufferedimage server

我尝试将BufferedImage发送到我的服务器,然后服务器将图像发送到接收客户端。一切正常,只是极其缓慢。当我在localhost上运行服务器时,一切都很完美,图像几乎是瞬间传输的。但是一旦我在外部平台上传和运行我的服务器,发送1张图像只需要30秒。我甚至试图篡改分辨率,但它似乎并没有影响传输时间。我得到10mbps,我的服务器在100mbps连接上运行所以它绝对不需要20-30秒来发送一个765x503图像。我也尝试将图像分成小块并将每个块分别放在不同的线程和不同的插槽中,然后将它们放回原处,但这与发送整个图像一样慢。这让我相信我没有做有效的事情。非常感谢!!

发送客户:

public void run() {
    try {

        Socket s = new Socket("serverip", port);

        ObjectOutputStream objectOutput = new ObjectOutputStream(
                s.getOutputStream());

        for (int i = 0; i < 2; i++)
            objectOutput.writeObject("set name dan");

        while (true) {
            if (client.methods != null
                    && client.methods.getClientImage() != null) {

                // Tools.log("Sending Image Data Array.....");

                // ArrayList<String> data = Conversion
                // .convertToRGBArray(client.methods.getClientImage());
                // Tools.log("Array Size: " + data.size());

                int w = 765;
                int h = 503;
                double d = .5;
                BufferedImage img = resize(client.methods.getClientImage(),
                        (int) (w * d), (int) (h * d));
                objectOutput.writeObject(WriteableBufferimg
                        .WriteBufferimgFactory(resize(img, w, h)));

                // Tools.log("Done sending...");

            }
        }

    } catch (UnknownHostException e) {
        e.printStackTrace();
    } catch (IOException e) {

        e.printStackTrace();
    }
}

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;
}

服务器:

while (true) {

            Object obj = objectInput.readObject();

            if (obj != null) {
                // Tools.log("Object recieved...");
                if (obj instanceof String) {
                    String str = (String) obj;
                    if (str.contains("set name")) {
                        String name = str.replace("set name ", "");
                        Tools.log("Changing " + this.getName() + " to: "
                                + name);
                        this.setName(name);
                    }
                } else {
                    // ArrayList<String> data = (ArrayList<String>) obj;
                    WriteableBufferimg data = (WriteableBufferimg) obj;
                    if (data != null) {
                        BufferedImage img = data.getImage();

                        String toFind = this.getName();
                        if (this.getName().contains("_app")) {
                            toFind = this.getName().replace("_app", "");
                        } else {
                            toFind = this.getName() + "_app";
                        }
                        for (Node n : Server.nodes) {
                            if (n != null && n.getName().equals(toFind)) {
                                //Tools.log("Sending Data to: " + n.getName());
                                if (n.getObjectOutputStream() != null)
                                    n.getObjectOutputStream().writeObject(
                                            data);
                                else
                                    Tools.log(n.getName()
                                            + "'s out is null");
                                //Tools.log("Complete.");
                            }
                        }

                    }
                }
            }

        }

接收客户:

ObjectInputStream in = new ObjectInputStream(s.getInputStream());

        while (true) {
            Object obj = in.readObject();
            if (obj != null) {
                WriteableBufferimg data = (WriteableBufferimg) obj;
                BufferedImage img = data.getImage();
                if (img != null) {
                    Main.panel.getGraphics().drawImage(img, 0, 0, null);
                } else {
                    Tools.log("img is null");
                }
            }

        }

WriteableBufferedimg类:

package data;

import java.awt.Frame;
import java.awt.MediaTracker;
import java.awt.Point;
import java.awt.color.ColorSpace;
import java.awt.image.BufferedImage;
import java.awt.image.ColorModel;
import java.awt.image.DataBuffer;
import java.awt.image.DataBufferInt;
import java.awt.image.PixelGrabber;
import java.awt.image.Raster;
import java.awt.image.SinglePixelPackedSampleModel;
import java.awt.image.WritableRaster;
import java.io.Serializable;

public class WriteableBufferimg implements Serializable {

int WIDTH, HIGHT, TransferType, Transparncy, Numcomp;
ColorSpace Colorspace;
int[] Data, BITS;
boolean Alpha, Alphaperm;
private static final long serialVersionUID = 1L;

public static WriteableBufferimg WriteBufferimgFactory(BufferedImage img) {
    Frame frame = new Frame("temp");
    MediaTracker mt = new MediaTracker(frame);
    mt.addImage(img, 1);
    int W = img.getWidth(frame);
    int H = img.getHeight(frame);
    int[] pixels = new int[W * H];
    PixelGrabber PG = new PixelGrabber(img, 0, 0, W, H, pixels, 0, W);

    try {
        PG.grabPixels();
    } catch (InterruptedException e) {
        System.out.println(e.getMessage());
    }
    ColorModel model = PG.getColorModel();
    int[] bits = model.getComponentSize();
    boolean alpha = model.hasAlpha();
    boolean alphaperm = model.isAlphaPremultiplied();
    int transferType = model.getTransferType();
    int transparncy = model.getTransparency();
    ColorSpace colorspace = model.getColorSpace();
    return new WriteableBufferimg(pixels, W, H, bits, alpha, alphaperm,
            transferType, transparncy, colorspace);

}

public WriteableBufferimg(int[] data, int w, int h, int[] bits,
        boolean alpha, boolean alphaperm, int transferType,
        int transparncy, ColorSpace colorspace) {
    WIDTH = w;
    HIGHT = h;
    Data = data;
    BITS = bits;
    Alpha = alpha;
    Alphaperm = alphaperm;
    TransferType = transferType;
    Transparncy = transparncy;
    Colorspace = colorspace;

}

public BufferedImage getImage() {
    DataBufferInt DB = new DataBufferInt(Data, (WIDTH * HIGHT), 0);
    int[] BM = new int[] { 0xff0000, 0xff00, 0xff };
    SinglePixelPackedSampleModel SM = new SinglePixelPackedSampleModel(
            DataBuffer.TYPE_INT, WIDTH, HIGHT, BM);

    // creating the raster
    Point P = new Point(0, 0);
    WritableRaster R = Raster.createWritableRaster(SM, DB, P);
    BufferedImage bi = new BufferedImage(WIDTH, HIGHT,
            BufferedImage.TYPE_INT_RGB);
    // in this case, P can be replace with null

    // convert the raster to a BufferedImage
    BufferedImage BI = new BufferedImage(bi.getColorModel(), R, false, null);
    return BI;
}

public void Close() {
    try {
        finalize();
        } catch (Throwable e) {
            e.printStackTrace();
        }
    }
}

0 个答案:

没有答案