A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

字节转换工具类
[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;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;

/**
 * StudentUI
 */
public class StudentUI extends JFrame{
	
	private JLabel lblIcon ;
	
	public StudentUI(){
		init();
	}

	private void init() {
		this.setTitle("yqw广播");
		this.setBounds(0, 0, 1366, 768);
		this.setLayout(null);
		

		lblIcon = new JLabel();
		lblIcon.setBounds(0, 0, 1366, 768);
		

		ImageIcon icon = new ImageIcon("D:/Koala.jpg");
		lblIcon.setIcon(icon);
		this.add(lblIcon);
		this.setVisible(true);
	}
	

	public void updateIcon(byte[] dataBytes){
		ImageIcon icon = new ImageIcon(dataBytes);
		lblIcon.setIcon(icon);
	}
}

数据接收类
[AppleScript] 纯文本查看 复制代码
package cn.itcast.screenbroadcast;

import java.io.ByteArrayOutputStream;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.util.HashMap;
import java.util.Map;

public class Receiver extends Thread {
	private DatagramSocket sock ; 
	private DatagramPacket pack ;
	private byte[] buf = new byte[Teacher.FRAME_UNIT_MAX + 14];
	
	//
	private Map<Integer, FrameUnit> unitMap = new HashMap<Integer,FrameUnit>();
	
	//
	private StudentUI ui;
	
	public Receiver(StudentUI ui){
		try {
			this.ui = ui ;
			sock = new DatagramSocket(8888);
			pack = new DatagramPacket(buf, buf.length);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public void run() {
		try {
			long currentFrameId = 0 ;
			while(true){
				sock.receive(pack);
				int len = pack.getLength();
				//ת����֡��Ԫ
				FrameUnit funit = convertData2FrameUnit(buf,0,len);
				long newFrameId = funit.getFrameId();
				//ͬһframe
				if(newFrameId == currentFrameId){
					unitMap.put(funit.getUnitNo(), funit);
				}
				//��frame
				else if(newFrameId > currentFrameId){
					currentFrameId = newFrameId ;
					unitMap.clear();
					unitMap.put(funit.getUnitNo(), funit);
				}
				processFrame();
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * ��������unit����
	 */
	private void processFrame() {
		try {
			//�õ�Frame���ܵ�unit����
			int unitCont = unitMap.values().iterator().next().getUnitCont();
			if(unitMap.size() == unitCont ){
				ByteArrayOutputStream baos = new ByteArrayOutputStream();
				for(int i = 0 ; i < unitCont ; i ++){
					FrameUnit fu = unitMap.get(i);
					fu.getData();
					baos.write(fu.getData());
				}
				unitMap.clear();
				
				//
				ui.updateIcon(Util.unzipData(baos.toByteArray()));
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * ת��ÿ��packΪFrameunit
	 */
	private FrameUnit convertData2FrameUnit(byte[] buf2, int i, int len) {
		byte[] data = new byte[len];
		System.arraycopy(buf2, 0, data, 0, len);
		
		FrameUnit unit = new FrameUnit();
		unit.setFrameId(Util.byte2Long(data));
		unit.setUnitCont(data[8]);
		unit.setUnitNo(data[9]);
		unit.setDateLen(Util.byte2Int(data,10));
		
		byte[] frameUnitBytes = new byte[unit.getDateLen()];
		System.arraycopy(buf2, 14, frameUnitBytes, 0, unit.getDateLen());
		
		unit.setData(frameUnitBytes);
		return unit;
	}
}

app 程序入口
[AppleScript] 纯文本查看 复制代码
package cn.itcast.screenbroadcast;

/**
 * ���ܷ�
 * 
 */
public class Student {
	public static void main(String[] args) {
		StudentUI ui = new StudentUI();
		new Receiver(ui).start();
	}
}


0 个回复

您需要登录后才可以回帖 登录 | 加入黑马