黑马程序员技术交流社区

标题: 【广州校区】+【原创】网络编程之屏幕广播教师端 [打印本页]

作者: yqw_gz_java    时间: 2018-9-27 12:24
标题: 【广州校区】+【原创】网络编程之屏幕广播教师端
工具类
[AppleScript] 纯文本查看 复制代码
package cn.itcast.screenbroadcast;

import java.awt.Rectangle;
import java.awt.Robot;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;
import java.util.zip.ZipOutputStream;

import javax.imageio.ImageIO;

public class Util {
        //
        private static Robot robot ;
       
        //
        static{
                try {
                        robot = new Robot();
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
       
        /**
         * ץͼ
         */
        public static byte[] captureScreen(){
                try {
                        BufferedImage image = robot.createScreenCapture(new Rectangle(0, 0, 1366, 768));
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        ImageIO.write(image, "jpg", baos);
                        return baos.toByteArray();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return null ;
        }
       

        public static byte[] long2Bytes(long l){
                byte[] bytes = new byte[8] ;
                bytes[0] = (byte)l ;
                bytes[1] = (byte)(l >> 8) ;
                bytes[2] = (byte)(l >> 16) ;
                bytes[3] = (byte)(l >> 24) ;
                bytes[4] = (byte)(l >> 32) ;
                bytes[5] = (byte)(l >> 40) ;
                bytes[6] = (byte)(l >> 48) ;
                bytes[7] = (byte)(l >> 56) ;
                return bytes ;
        }
       
        public static long byte2Long(byte[] bytes){
                return ((long)(bytes[0] & 0xFF))
                                | ((long)(bytes[1] & 0xFF) << 8)
                                | ((long)(bytes[2] & 0xFF) << 16)
                                | ((long)(bytes[3] & 0xFF) << 24)
                                | ((long)(bytes[4] & 0xFF) << 32)
                                | ((long)(bytes[5] & 0xFF) << 40)
                                | ((long)(bytes[6] & 0xFF) << 48)
                                | ((long)(bytes[7] & 0xFF) << 56);
        }
       

        public static byte[] int2Bytes(int l){
                byte[] bytes = new byte[8] ;
                bytes[0] = (byte)l ;
                bytes[1] = (byte)(l >> 8) ;
                bytes[2] = (byte)(l >> 16) ;
                bytes[3] = (byte)(l >> 24) ;
                return bytes ;
        }
       
        public static int bytes2Int(byte[] bytes){
                return ((bytes[0] & 0xFF))
                                | ((bytes[1] & 0xFF) << 8)
                                | ((bytes[2] & 0xFF) << 16)
                                | ((bytes[3] & 0xFF) << 24);
        }
       
        public static int byte2Int(byte[] bytes,int offset){
                return ((bytes[0 + offset] & 0xFF))
                                | ((bytes[1 + offset] & 0xFF) << 8)
                                | ((bytes[2 + offset] & 0xFF) << 16)
                                | ((bytes[3 + offset] & 0xFF) << 24);
        }


        public static byte[] zipData(byte[] rawData) {
                try {
                        //
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        ZipOutputStream zos = new ZipOutputStream(baos);
                        zos.putNextEntry(new ZipEntry("one"));
                        zos.write(rawData);
                        zos.close();
                        return baos.toByteArray();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return null;
        }
       

        public static byte[] unzipData(byte[] zipData) {
                try {
                        //
                        ByteArrayInputStream bais = new ByteArrayInputStream(zipData);
                        ZipInputStream zis = new ZipInputStream(bais);
                       
                        ByteArrayOutputStream baos = new ByteArrayOutputStream();
                        zis.getNextEntry();
                        byte[] buf = new byte[1024];
                        int len = 0 ;
                        while((len = zis.read(buf)) != -1){
                                baos.write(buf, 0, len);
                        }
                        zis.close();
                        bais.close();
                        baos.close();
                        return baos.toByteArray();
                } catch (Exception e) {
                        e.printStackTrace();
                }
                return null;
        }
}


幁单元实体类
[AppleScript] 纯文本查看 复制代码
package cn.itcast.screenbroadcast;


public class FrameUnit {
        private long frameId;

        private int unitCont;

        private int unitNo;

        private int dateLen;

        private byte[] data;

        public long getFrameId() {
                return frameId;
        }

        public void setFrameId(long frameId) {
                this.frameId = frameId;
        }

        public int getUnitCont() {
                return unitCont;
        }

        public void setUnitCont(int unitCont) {
                this.unitCont = unitCont;
        }

        public int getUnitNo() {
                return unitNo;
        }

        public void setUnitNo(int unitNo) {
                this.unitNo = unitNo;
        }

        public int getDateLen() {
                return dateLen;
        }

        public void setDateLen(int dateLen) {
                this.dateLen = dateLen;
        }

        public byte[] getData() {
                return data;
        }

        public void setData(byte[] data) {
                this.data = data;
        }

}

发送工具类
[AppleScript] 纯文本查看 复制代码
package cn.itcast.screenbroadcast;

import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetSocketAddress;
import java.util.List;


public class ScreenBroadCastSender {
        private InetSocketAddress bcAddr ;
        private DatagramSocket socket ;
       
        public ScreenBroadCastSender(){
                try {
                        socket = new DatagramSocket(8888);
                        bcAddr = new InetSocketAddress("192.168.12.255", 9999);
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
       

        public void send(List<FrameUnit> unitList){
                try {
                        for(FrameUnit unit : unitList){
                                byte[] packetData = popFrameUnit(unit);
                                DatagramPacket pack = new DatagramPacket(packetData, packetData.length);
                                pack.setSocketAddress(bcAddr);
                                socket.send(pack);
                        }
                } catch (Exception e) {
                        e.printStackTrace();
                }
        }
       

        private byte[] popFrameUnit(FrameUnit unit){
                byte[] bytes = new byte[unit.getDateLen() + 14] ;
                byte[] frameIdBytes = Util.long2Bytes(unit.getFrameId());
                System.arraycopy(frameIdBytes, 0, bytes, 0, 8);
                bytes[8] = (byte)unit.getUnitCont();
                bytes[9] = (byte)unit.getUnitNo();
                byte[] dateLenBytes = Util.int2Bytes(unit.getDateLen());
                System.arraycopy(dateLenBytes, 0, bytes, 10, 4);
                System.arraycopy(unit.getData(), 0, bytes, 14, unit.getDateLen());
               
                return bytes ;
        }
}






欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2