Mercurial > hg > Members > nobuyasu > tightVNCProxy
comparison src/myVncProxy/MyRfbProto.java @ 25:cded9fd297ab
create png data
author | e085711 |
---|---|
date | Tue, 26 Apr 2011 06:21:37 +0900 |
parents | 87b29d6039a6 |
children | 0aa0e0bd742c |
comparison
equal
deleted
inserted
replaced
24:87b29d6039a6 | 25:cded9fd297ab |
---|---|
1 package myVncProxy; | 1 package myVncProxy; |
2 import java.awt.Graphics; | |
3 import java.awt.Image; | |
4 import java.awt.image.BufferedImage; | |
5 import java.io.BufferedOutputStream; | |
2 import java.io.BufferedReader; | 6 import java.io.BufferedReader; |
7 import java.io.ByteArrayInputStream; | |
8 import java.io.ByteArrayOutputStream; | |
3 import java.io.IOException; | 9 import java.io.IOException; |
4 import java.io.InputStreamReader; | 10 import java.io.InputStreamReader; |
5 import java.net.BindException; | 11 import java.net.BindException; |
6 import java.net.ServerSocket; | 12 import java.net.ServerSocket; |
7 import java.net.Socket; | 13 import java.net.Socket; |
14 import java.nio.ByteBuffer; | |
8 import java.util.LinkedList; | 15 import java.util.LinkedList; |
16 | |
17 import javax.imageio.ImageIO; | |
9 | 18 |
10 | 19 |
11 class MyRfbProto extends RfbProto { | 20 class MyRfbProto extends RfbProto { |
12 | 21 |
13 private int messageType; | 22 private int messageType; |
20 | 29 |
21 private ServerSocket servSock; | 30 private ServerSocket servSock; |
22 private int acceptPort; | 31 private int acceptPort; |
23 private byte initData[]; | 32 private byte initData[]; |
24 private LinkedList <Socket> cliList; | 33 private LinkedList <Socket> cliList; |
34 | |
35 byte[] pngBytes; | |
25 | 36 |
26 MyRfbProto(String h, int p, VncViewer v ) throws IOException { | 37 MyRfbProto(String h, int p, VncViewer v ) throws IOException { |
27 super(h, p, v); | 38 super(h, p, v); |
28 cliList = new LinkedList <Socket>(); | 39 cliList = new LinkedList <Socket>(); |
29 } | 40 } |
126 | 137 |
127 void sendInitData(Socket sock) throws IOException{ | 138 void sendInitData(Socket sock) throws IOException{ |
128 sock.getOutputStream().write(initData); | 139 sock.getOutputStream().write(initData); |
129 } | 140 } |
130 | 141 |
131 // void sendData(byte b[]) throws IOException{ | |
132 void sendData(byte b[]){ | 142 void sendData(byte b[]){ |
143 | |
144 | |
133 try{ | 145 try{ |
134 for(Socket cli : cliList){ | 146 for(Socket cli : cliList){ |
135 try{ | 147 try{ |
136 cli.getOutputStream().write(b, 0, b.length); | 148 cli.getOutputStream().write(b, 0, b.length); |
137 }catch(IOException e){ | 149 }catch(IOException e){ |
138 // if socket closed | 150 // if socket closed |
139 // cliList.remove(cli); | |
140 cliList.remove(cli); | 151 cliList.remove(cli); |
141 } | 152 } |
142 } | 153 } |
143 // System.out.println("cliSize="+cliSize()); | 154 // System.out.println("cliSize="+cliSize()); |
144 }catch(Exception e){ | 155 }catch(Exception e){ |
145 // System.out.println("cliSize 0"); | |
146 } | 156 } |
147 } | 157 } |
148 boolean ready() throws IOException { | 158 boolean ready() throws IOException { |
149 BufferedReader br = new BufferedReader(new InputStreamReader(is)); | 159 BufferedReader br = new BufferedReader(new InputStreamReader(is)); |
150 return br.ready(); | 160 return br.ready(); |
184 break; | 194 break; |
185 default: | 195 default: |
186 mark(1000000); | 196 mark(1000000); |
187 } | 197 } |
188 } | 198 } |
189 | 199 BufferedImage createBufferedImage(Image img){ |
200 BufferedImage bimg = new BufferedImage(img.getWidth(null), img.getHeight(null), BufferedImage.TYPE_INT_RGB ); | |
201 System.out.println("img.getWidth="+img.getWidth(null)); | |
202 | |
203 Graphics g = bimg.getGraphics(); | |
204 g.drawImage(img, 0, 0, null); | |
205 g.dispose(); | |
206 return bimg; | |
207 } | |
208 | |
209 void createPngBytes(BufferedImage bimg)throws IOException { | |
210 pngBytes = getImageBytes(bimg , "png"); | |
211 } | |
212 byte[] getBytes(BufferedImage img)throws IOException { | |
213 byte[] b = getImageBytes(img, "png"); | |
214 return b; | |
215 } | |
216 | |
217 byte[] getImageBytes(BufferedImage image, String imageFormat) throws IOException { | |
218 ByteArrayOutputStream bos = new ByteArrayOutputStream(); | |
219 BufferedOutputStream os = new BufferedOutputStream(bos); | |
220 image.flush(); | |
221 ImageIO.write(image, imageFormat, os); | |
222 os.flush(); | |
223 os.close(); | |
224 return bos.toByteArray(); | |
225 } | |
226 | |
227 void sendPngData(Socket sock)throws IOException{ | |
228 // ByteBuffer length = ByteBuffer.allocate(4); | |
229 // length.putInt(pngBytes.length); | |
230 // sock.getOutputStream().write(length.getInt()); | |
231 sock.getOutputStream().write(pngBytes); | |
232 } | |
233 | |
234 BufferedImage createBimg()throws IOException{ | |
235 BufferedImage bimg = ImageIO.read(new ByteArrayInputStream(pngBytes)); | |
236 return bimg; | |
237 } | |
238 void readPngData()throws IOException{ | |
239 pngBytes = new byte[is.available()]; | |
240 readFully(pngBytes); | |
241 } | |
242 | |
243 | |
244 | |
190 void printFramebufferUpdate(){ | 245 void printFramebufferUpdate(){ |
191 | 246 |
192 System.out.println("messageType=" + messageType); | 247 System.out.println("messageType=" + messageType); |
193 System.out.println("rectangles="+rectangles); | 248 System.out.println("rectangles="+rectangles); |
194 System.out.println("encoding=" + encoding); | 249 System.out.println("encoding=" + encoding); |
195 switch(encoding){ | 250 switch(encoding){ |
196 case RfbProto.EncodingRaw: | 251 case RfbProto.EncodingRaw: |
197 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 + 16); | 252 System.out.println("rectW * rectH * 4 + 16 =" + rectW * rectH * 4 + 16); |
198 break; | 253 break; |
199 default: | 254 default: |
200 | 255 } |
201 | 256 } |
202 } | |
203 } | |
204 | |
205 | |
206 } | 257 } |