# HG changeset patch # User e085711 # Date 1303766497 -32400 # Node ID cded9fd297ab4eb77d62a84537e88989c5ed8b95 # Parent 87b29d6039a65150186baf1a064414e2fb8b080e create png data diff -r 87b29d6039a6 -r cded9fd297ab src/myVncProxy/MyRfbProto.java --- a/src/myVncProxy/MyRfbProto.java Sun Apr 24 23:03:00 2011 +0900 +++ b/src/myVncProxy/MyRfbProto.java Tue Apr 26 06:21:37 2011 +0900 @@ -1,12 +1,21 @@ package myVncProxy; +import java.awt.Graphics; +import java.awt.Image; +import java.awt.image.BufferedImage; +import java.io.BufferedOutputStream; import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStreamReader; import java.net.BindException; import java.net.ServerSocket; import java.net.Socket; +import java.nio.ByteBuffer; import java.util.LinkedList; +import javax.imageio.ImageIO; + class MyRfbProto extends RfbProto { @@ -23,6 +32,8 @@ private byte initData[]; private LinkedList cliList; + byte[] pngBytes; + MyRfbProto(String h, int p, VncViewer v ) throws IOException { super(h, p, v); cliList = new LinkedList (); @@ -128,21 +139,20 @@ sock.getOutputStream().write(initData); } -// void sendData(byte b[]) throws IOException{ void sendData(byte b[]){ + + try{ for(Socket cli : cliList){ try{ cli.getOutputStream().write(b, 0, b.length); }catch(IOException e){ // if socket closed - // cliList.remove(cli); cliList.remove(cli); } } // System.out.println("cliSize="+cliSize()); }catch(Exception e){ -// System.out.println("cliSize 0"); } } boolean ready() throws IOException { @@ -186,7 +196,52 @@ mark(1000000); } } + BufferedImage createBufferedImage(Image img){ + BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB ); + System.out.println("img.getWidth="+img.getWidth(null)); + + Graphics g = bimg.getGraphics(); + g.drawImage(img, 0, 0, null); + g.dispose(); + return bimg; + } + + void createPngBytes(BufferedImage bimg)throws IOException { + pngBytes = getImageBytes(bimg , "png"); + } + byte[] getBytes(BufferedImage img)throws IOException { + byte[] b = getImageBytes(img, "png"); + return b; + } + byte[] getImageBytes(BufferedImage image, String imageFormat) throws IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + BufferedOutputStream os = new BufferedOutputStream(bos); + image.flush(); + ImageIO.write(image, imageFormat, os); + os.flush(); + os.close(); + return bos.toByteArray(); + } + + void sendPngData(Socket sock)throws IOException{ +// ByteBuffer length = ByteBuffer.allocate(4); +// length.putInt(pngBytes.length); +// sock.getOutputStream().write(length.getInt()); + sock.getOutputStream().write(pngBytes); + } + + BufferedImage createBimg()throws IOException{ + BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes)); + return bimg; + } + void readPngData()throws IOException{ + pngBytes = new byte[is.available()]; + readFully(pngBytes); + } + + + void printFramebufferUpdate(){ System.out.println("messageType=" + messageType); @@ -197,10 +252,6 @@ System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 + 16); break; default: - - } } - - } diff -r 87b29d6039a6 -r cded9fd297ab src/myVncProxy/ProxyVncCanvas.java --- a/src/myVncProxy/ProxyVncCanvas.java Sun Apr 24 23:03:00 2011 +0900 +++ b/src/myVncProxy/ProxyVncCanvas.java Tue Apr 26 06:21:37 2011 +0900 @@ -9,6 +9,8 @@ import java.net.Socket; +import javax.imageio.ImageIO; + // //VncCanvas is a subclass of Canvas which draws a VNC desktop on it. // @@ -29,7 +31,8 @@ Image memImage; Graphics memGraphics; - Image rawPixelsImage; +// Image rawPixelsImage; + BufferedImage rawPixelsImage; MemoryImageSource pixelsSource; byte[] pixels8; int[] pixels24; @@ -97,7 +100,7 @@ for (int i = 0; i < 256; i++) colors[i] = new Color(cm8.getRGB(i)); - // setPixelFormat(); +// setPixelFormat(); inputEnabled = false; // Keyboard listener is enabled even in view-only mode, to catch @@ -215,6 +218,7 @@ 0); bytesPixel = 4; } + updateFramebufferSize(); } @@ -278,7 +282,7 @@ } pixelsSource.setAnimated(true); - rawPixelsImage = Toolkit.getDefaultToolkit().createImage(pixelsSource); + rawPixelsImage = (BufferedImage) Toolkit.getDefaultToolkit().createImage(pixelsSource); } @@ -360,9 +364,6 @@ */ - Thread accept = new Thread(new acceptThread(rfb)); - accept.start(); - while (true) { @@ -502,7 +503,8 @@ setPixelFormat(); fullUpdateNeeded = true; } -*/ +*/ + // Request framebuffer update if needed. int w = rfb.framebufferWidth; int h = rfb.framebufferHeight; @@ -528,6 +530,10 @@ bufSize = (int)rfb.getNumBytesRead() - bufSize; // System.out.println("bufSize="+bufSize); rfb.bufResetSend(bufSize); + +// byte[] rawBytes = getBytes(rawPixelsImage);/ +// System.out.println("rawBytes.length="+rawBytes.length); + } } @@ -558,17 +564,20 @@ if (rfb.rec != null) { rfb.rec.write(buf); } - /* - * offset = dy * rfb.framebufferWidth + x; for (i = 0; i < w; - * i++) { pixels24[offset + i] = (buf[i * 4 + 2] & 0xFF) << 16 | - * (buf[i * 4 + 1] & 0xFF) << 8 | (buf[i * 4] & 0xFF); } - */ + + offset = dy * rfb.framebufferWidth + x; + for (i = 0; i < w; i++) { + pixels24[offset + i] = (buf[i * 4 + 2] & 0xFF) << 16 | + (buf[i * 4 + 1] & 0xFF) << 8 | + (buf[i * 4] & 0xFF); + } + } } - /* - * handleUpdatedPixels(x, y, w, h); if (paint) scheduleRepaint(x, y, w, - * h); - */ +/* + handleUpdatedPixels(x, y, w, h); + if (paint) scheduleRepaint(x, y, w, h); +*/ } // @@ -1494,10 +1503,12 @@ void handleUpdatedPixels(int x, int y, int w, int h) { // Draw updated pixels of the off-screen image. + pixelsSource.newPixels(x, y, w, h); memGraphics.setClip(x, y, w, h); memGraphics.drawImage(rawPixelsImage, 0, 0, null); memGraphics.setClip(0, 0, rfb.framebufferWidth, rfb.framebufferHeight); + } // @@ -1875,4 +1886,30 @@ cursorWidth, cursorHeight); } } + + BufferedImage createBufferedImage(Image img){ + BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB ); + + Graphics g = bimg.getGraphics(); + g.drawImage(img, 0, 0, null); + g.dispose(); + return bimg; + } + + byte[] getBytes(BufferedImage img)throws IOException { + byte[] b = getImageBytes(img, "raw"); + return b; + } + + byte[] getImageBytes(BufferedImage image, String imageFormat) throws IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + BufferedOutputStream os = new BufferedOutputStream(bos); + image.flush(); + ImageIO.write(image, imageFormat, os); + os.flush(); + os.close(); + return bos.toByteArray(); + } + + } diff -r 87b29d6039a6 -r cded9fd297ab src/myVncProxy/VncCanvas.java --- a/src/myVncProxy/VncCanvas.java Sun Apr 24 23:03:00 2011 +0900 +++ b/src/myVncProxy/VncCanvas.java Tue Apr 26 06:21:37 2011 +0900 @@ -32,6 +32,8 @@ import java.net.Socket; +import javax.imageio.ImageIO; + // // VncCanvas is a subclass of Canvas which draws a VNC desktop on it. // @@ -53,6 +55,8 @@ Graphics memGraphics; Image rawPixelsImage; + BufferedImage bimg; + MemoryImageSource pixelsSource; byte[] pixels8; int[] pixels24; @@ -305,7 +309,8 @@ } pixelsSource.setAnimated(true); rawPixelsImage = Toolkit.getDefaultToolkit().createImage(pixelsSource); - +// rawPixelsImage = (BufferedImage) Toolkit.getDefaultToolkit().createImage(pixelsSource); + // Update the size of desktop containers. if (viewer.inSeparateFrame) { if (viewer.desktopScrollPane != null) @@ -398,16 +403,12 @@ while (true) { - if(!rfb.ready())continue; System.out.println("\ncount=" + count); count++; - System.out.println("ready rfb.available()="+rfb.available()); rfb.regiFramebufferUpdate(); - rfb.printFramebufferUpdate(); +// rfb.printFramebufferUpdate(); rfb.checkAndMark(); - - System.out.println("rfb.available()="+rfb.available()); // rfb.printNumBytesRead(); long bufSize = rfb.getNumBytesRead(); @@ -563,13 +564,21 @@ bufSize = rfb.getNumBytesRead() - bufSize; System.out.println("bufSize="+bufSize); rfb.bufResetSend((int)bufSize); - if(rfb.available() < bufSize){ - System.out.println("rfb.available() < bufSize"); - + + bimg = createBufferedImage(rawPixelsImage); + rfb.createPngBytes(bimg); + + boolean result = false; + try{ + result = ImageIO.write(bimg, "png", new File("sample.png")); + }catch(Exception e){ + e.printStackTrace(); + result = false; } - + } } + // // Handle a raw rectangle. The second form with paint==false is used @@ -1917,4 +1926,29 @@ cursorWidth, cursorHeight); } } + + BufferedImage createBufferedImage(Image img){ + BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB ); + System.out.println("img.getWidth="+img.getWidth(null)); + + Graphics g = bimg.getGraphics(); + g.drawImage(img, 0, 0, null); + g.dispose(); + return bimg; + } + + byte[] getBytes(BufferedImage img)throws IOException { + byte[] b = getImageBytes(img, "png"); + return b; + } + + byte[] getImageBytes(BufferedImage image, String imageFormat) throws IOException { + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + BufferedOutputStream os = new BufferedOutputStream(bos); + image.flush(); + ImageIO.write(image, imageFormat, os); + os.flush(); + os.close(); + return bos.toByteArray(); + } } diff -r 87b29d6039a6 -r cded9fd297ab src/myVncProxy/VncProxyService.java --- a/src/myVncProxy/VncProxyService.java Sun Apr 24 23:03:00 2011 +0900 +++ b/src/myVncProxy/VncProxyService.java Tue Apr 26 06:21:37 2011 +0900 @@ -69,9 +69,23 @@ cursorUpdatesDef = null; eightBitColorsDef = null; + try{ + connectAndAuthenticate(); + }catch (NoRouteToHostException e) { + fatalError("Network error: no route to server: " + host, e); + } catch (UnknownHostException e) { + fatalError("Network error: server name unknown: " + host, e); + } catch (ConnectException e) { + fatalError("Network error: could not connect to server: " + host + + ":" + port, e); + }catch(Exception e){} + rfbThread = new Thread(this); rfbThread.start(); + Thread accept = new Thread(new acceptThread(rfb, vc)); + accept.start(); + } // @@ -81,11 +95,12 @@ public void run() { try { - connectAndAuthenticate(); +// connectAndAuthenticate(); doProtocolInitialisation(); vc = new ProxyVncCanvas(this, 0, 0); - + vc.updateFramebufferSize(); + processNormalProtocol();// main loop } catch (NoRouteToHostException e) { diff -r 87b29d6039a6 -r cded9fd297ab src/myVncProxy/acceptThread.java --- a/src/myVncProxy/acceptThread.java Sun Apr 24 23:03:00 2011 +0900 +++ b/src/myVncProxy/acceptThread.java Tue Apr 26 06:21:37 2011 +0900 @@ -1,26 +1,35 @@ package myVncProxy; -import java.net.BindException; import java.net.Socket; import java.io.IOException; public class acceptThread implements Runnable { MyRfbProto rfb; - - acceptThread(MyRfbProto _rfb) { + ProxyVncCanvas pvc; + VncCanvas vc; + byte[] imageBytes; + + acceptThread(MyRfbProto _rfb ) { rfb = _rfb; } - - + acceptThread(MyRfbProto _rfb, ProxyVncCanvas _vc ) { + rfb = _rfb; + pvc = _vc; + } + acceptThread(MyRfbProto _rfb, VncCanvas _vc ) { + rfb = _rfb; + vc = _vc; + } public void run() { rfb.selectPort(); - while (true) { try { Socket newCli = rfb.accept(); rfb.sendInitData(newCli); + rfb.sendPngData(newCli); rfb.addSock(newCli); } catch (IOException e) { - + e.printStackTrace(); + System.out.println(e); } } }