如何在java中通过套接字发送图像数据类型 [英] How to send Image data type via socket in java

查看:132
本文介绍了如何在java中通过套接字发送图像数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我真的很困惑如何通过套接字发送图像数据类型。请帮帮我。我已经了解了如何将Image数据类型转换为char [],但结果是0。

I really confused how to send an Image data type via socket. Please help me. I've searced how to convert Image data type to char[] but the result was 0.

推荐答案

您可以转换<$使用此方法c $ c>图像到 BufferedImage source ):

public BufferedImage toBufferedImage(final Image image, final int type) {
    //Test if image does not need conversion
    if (image instanceof BufferedImage)
        return (BufferedImage) image;
    //Check if image can be converted easily
    if (image instanceof VolatileImage)
        return ((VolatileImage) image).getSnapshot();
    //loadImage method ensures that the image has loaded fully (it could be from the web or something). If you are sure that when this method is called - the image is loaded, you can remove this line and whole method.
    loadImage(image);
    //Create new BufferedImage with the same width and height and given data type (see constants in BufferedImage API)
    final BufferedImage buffImg = new BufferedImage(image.getWidth(null), image.getHeight(null), type);
    //Get graphics out of our new BufferedImage. Graphics2D is used to draw something on the image
    final Graphics2D g2 = buffImg.createGraphics();
    //Use Graphics2D to draw our Image contents on top of BufferedImage
    g2.drawImage(image, null, null);
    //We no longer need our graphics object as we drawn everything we wanted
    g2.dispose();
    //Return BufferedImage
    return buffImg;
}

//Method that ensures that the image was loaded succesfully
private void loadImage(final Image image) {
    //Inner class implementing the ImageObserver interface. It will be used to track the image loading progress
    class StatusObserver implements ImageObserver {
        boolean imageLoaded = false;
            //Each time an image updates - it will call this method
        public boolean imageUpdate(final Image img, final int infoflags, 
              final int x, final int y, final int width, final int height) {
                    //When flags contains ALLBITS flag - that means that the image was fully loaded.
            if (infoflags == ALLBITS) {
                synchronized (this) {
                                    //Therefore we set status to true
                    imageLoaded = true;
                                    //And notify anyone who was waiting for our job to be done
                    notify();
                }
                return true;
            }
            return false;
        }
    }
    //Then we create the observer itself
    final StatusObserver imageStatus = new StatusObserver();
    //We aquire it's monitor with this synchronized block. This will allow us to "wait" for it to complete loading (see notify() in StatusObserver)
    synchronized (imageStatus) {
            //Basically if image is loaded - it will have it's width and height set
        if (image.getWidth(imageStatus) == -1 || image.getHeight(imageStatus) == -1) {
                    //While status observer is not loaded
            while (!imageStatus.imageLoaded) {
                try {
                                    //We wait for status observer to notify us
                    imageStatus.wait();
                } catch (InterruptedException ex) {}
            }
        }
    }
}

然后,您可以使用 BufferedImage 6 / docs / api / javax / imageio / ImageIO.html#write%28java.awt.image.RenderedImage,%20java.lang.String,%20java.io.OutputStream%29rel =nofollow> ImageIO.write( )方法。

Then, you can write the resulting BufferedImage with ImageIO.write() method.

这篇关于如何在java中通过套接字发送图像数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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