Implement Flex and Java Communication Based on Socket

Source: Internet
Author: User

 

Using the Socket and Java to accept and process information in Flex can greatly reduce the front-end processing of Flex. This is what I have done in the last week and I am stuck with these things, but these small problems have to be solved. It is still hard for beginners. Of course, this is a pediatrics for cool people. Let's take a look at the joke, for beginners or those who have such requirements, you can learn from them. The following is the subject:
What I implemented is to convert Camera or the video stream played in the front-end of Flex to a binary stream and send the binary stream to the background Java through Socket, at first, I thought it was quite simple to save images. I didn't want to save it for a long time. It was really depressing.
1. functions implemented by Flex

This problem is relatively simple. The classes used include:

Public class BitmapData: I think a lot of methods, and this class is used (maybe my thinking is limited ,!)

Public class ByteArray: converts image data into byte streams.

The above two types can be used to fully implement and convert video streams. However, Flex also provides a class that can implement the following functions:

Public dynamic class ImageSnapshot: You can see what the implementation is.

The descriptions and usage of the above classes can be found in the Reference documents such as corner 3.0 Language and Components Reference. We will not introduce them here. In my need, only a conversion is required, and the third class is not required. Some code is as follows:

 

1 var byteArray: ByteArray = new ByteArray ();
2 var bitmap: BitmapData = new BitmapData (vd. width, vd. height );
3 bitmap. draw (vd );
4 byteArray. writeInt (bitmap. width );
5 byteArray. writeInt (bitmap. height );
6 var byteArrayTemp: ByteArray = bitmap. getPixels (bitmap. rect );
7 byteArray. writeBytes (byteArrayTemp );

 

Among them, vd is the video stream to be captured. The code is clear, so we won't bother writing too much. This part ends;

2. Socket implementation in Flex
The Socket class allows the Code to establish a Socket connection and read and write original binary data. All we have done here is to set the host address and port number and listen on it. After the connection is successful, we can send data. This is actually quite simple, there are a lot of information on the Internet, and it is not difficult to find the specific details. Here I just talk about it a lot and skip this section;
3. Java implementation
In Java, you need to create a service (set the port number), receive data, and process the data (generate the corresponding format, here I only generate JPG, others are the same)
Here is a brief introduction:
The first introduction is of course the establishment of the Service: java.net. ServerSocket

1 import java.net. ServerSocket
2 private static ServerSocket server;
3 server = new serversockets (1024 );
Declare the service and set the port number. Note that the port number must be greater than or equal to 1024. If the port number is smaller than 1024, all the names are named master,
The second thing to do is to listen, that is, to receive the data sent from the front-end, or to understand the Flex front-end as a client. The Java side creates a service, which can be called a server, this makes it easier to understand. Use an accept method of the ServerSocket class, which is blocked when data is not received:

1 import java.net. Socket;
2 private Socket socket;
3 socket = server. accept ();

But remember, this method throws an exception.
The third thing is to receive and process the data. Here, we can understand that it is not in place, and it is not a small bend, but it is eventually rolled back:
This is the binary stream that I want to receive. The data is basically binary, so we need to use DataInputStream to get the input stream of the socket object, the corresponding DataOutputStream obtains the output stream of the socket object to respond to the client. The Code is as follows:
1 import java. awt. Image;
2 import java. awt. Toolkit;
3 import java. awt. image. BufferedImage;
4 import java. awt. image. MemoryImageSource;
5 import java. io. DataInputStream;
6 import java. io. DataOutputStream;
7 import java. io. File;
8 import java. io. FileOutputStream;
9 import java. io. DataInputStream;
10 import javax. imageio. ImageIO;

11try {
12 System. out. println ("transfer start :");
13 file = new File ("C: \ images \ image" + sum + ". jpg ");
14 sum ++;
15 DataInputStream dis = new DataInputStream (socket. getInputStream ());
16 DataOutputStream dos = new DataOutputStream (socket. getOutputStream ());
17
18 FileOutputStream out = new FileOutputStream (file );
19 int width = dis. readInt ();
20 int height = dis. readInt ();
21 System. out. println ("width:" + width + "--> height:" + height );
22 byte [] buffer1 = new byte [MAX_COUNT];
23 int size = width * 4 * height;
24 byte [] buffer = new byte [size];
25 int count = 0, num = 0;
26 while (true ){
27 count = dis. read (buffer1, 0, MAX_COUNT );
28 System. arraycopy (buffer1, 0, buffer, num, count );
29 num + = count;
30 System. out. println ("length:" + num + "-->" + count );
31 if (num = size ){
32 Image TheImage = readImage32 (buffer, height, width );
33 BufferedImage tag = new BufferedImage (width, height,
34 BufferedImage. TYPE_INT_RGB );
35 tag. getGraphics (). drawImage (TheImage, 0, 0, width, height, null );
36 System. out. println (file. getName ());
37 // required imageencoder required image = required codec. createJPEGEncoder (out );
38 // & nb

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.