1.1.10.2. fejezet, kép átvitele socket-el

A java kliens fogadja a képeket a 8555-ös socket porton

	public static void main(String[] args) {
		Object server = null;
		server = getImageServer();
		// server = lookupRemoteStatelessImageServer();
		if (server instanceof ImageServer) {
			System.out.print("megvan:" + server.toString());
		} else {
			System.exit(1);
		}
		byte[] a = new byte[4];
		a[3] = 15;
		int c = ByteBuffer.wrap(a).getInt();
 
		ServerSocket serverSocket = null;
		Socket clientSocket = null;
		try {
			serverSocket = new ServerSocket(8555);
			clientSocket = serverSocket.accept();
			InputStream in = clientSocket.getInputStream();
			int idx = 0;
			byte[] b = new byte[4];
			int i = in.read(b);
			int result = ByteBuffer.wrap(b).getInt();
			while (result > 0) {
				System.out.println("fileSize:" + result);
				byte[] img = new byte[result];
				int readed = in.read(img);
				while (readed < result) {
					int cnt = result - readed;
					readed += in.read(img, readed, cnt);
				}
				ByteArrayInputStream buff = new ByteArrayInputStream(img);
				BufferedImage image = ImageIO.read(buff);
				if (image != null) {
					// ImageIO.write(image, "jpg", new File(
					// "c:/temp/video/new-img" + idx + ".jpg"));
					((ImageServer) server).acceptImage(idx, img);
					idx++;
				}
				Arrays.fill(b, (byte) 0);
				i = in.read(b);
				result = ByteBuffer.wrap(b).getInt();
 
			}
		} catch (IOException e) {
 
		} finally {
			try {
				clientSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
			try {
				serverSocket.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

A webkamera kép rögzítő python script a VideoCapture projekttel készült, és a képet továbbítja a Java socketnek. ( A videocapture projektnek új változata elérhető itt).

from VideoCapture import Device
import StringIO
import io
import socket, struct
import time
 
screensize = width, height = 640, 480
imgsize = width, height = 640,480
 
# get cam device
cam = Device(devnum=0)
 
cnt = 0
while cnt<2:
	camshot = cam.getImage()
	time.sleep(2)
	cnt+=1
 
s = socket.socket(
    socket.AF_INET, socket.SOCK_STREAM)
s.connect(('localhost', 8555))
cnt=0
while cnt<50:
	camshot = cam.getImage()
	fp = StringIO.StringIO()	
	camshot.save(fp, format="JPEG", quality=75)
	fp.flush()
	isize = fp.tell()
	print 'filesize: {0:d}'.format(isize)
	fp.seek(0)
	strc = struct.pack('>i',isize)
	s.send(strc)
	s.send(fp.read())
	cnt+=1
s.close()

Itt érdemes megjegyezni, hogyan használjuk a python struct típusát fájl méret átküldéséhez

struct.pack('>i',isize)

A példa big-endian formába küldi át az adatot, a Java így kapja meg a helyes értéket

byte[] b = new byte[4];
int i = in.read(b);
int result = ByteBuffer.wrap(b).getInt();

A távoli eljárás:

@Named
@Stateless
@Remote(ImageServer.class)
public class ImageServerImpl implements ImageServer, Serializable {
 
	private static final long serialVersionUID = 3835243080950242682L;
 
	@Override
	public void acceptImage(int idx, byte[] img) {
		ByteArrayInputStream buff = new ByteArrayInputStream(img);
		try {
			BufferedImage image = ImageIO.read(buff);
			if (image != null) {
 
				ImageIO.write(image, "jpg", new File("c:/temp/video/new-img"
						+ idx + ".jpg"));
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
}