栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

如何在Java中通过套接字发送图像数据类型

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何在Java中通过套接字发送图像数据类型

您可以使用以下方法将其转换

Image
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 succesfullyprivate 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
使用ImageIO.write()方法写入结果。



转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/574716.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号