Assorted logic added to ImageTransferThread. Rewrites
This commit is contained in:
@@ -55,6 +55,7 @@ public class CamActivity extends Activity{
|
||||
Intent intent = getIntent();
|
||||
serverIp = intent.getStringExtra("address");
|
||||
imThread = new ImageTransferThread(serverIp);
|
||||
imThread.start();
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@@ -342,7 +342,7 @@ public class MainActivity extends Activity implements WifiOnDialogListener, Conn
|
||||
@Override
|
||||
protected Boolean doInBackground(Void... params){
|
||||
boolean result, done = false;
|
||||
byte[] buffer = (new String("NxtAR server here!")).getBytes();
|
||||
byte[] buffer = ProjectConstants.SERVICE_DISCOVERY_MESSAGE.getBytes();
|
||||
|
||||
// Create a buffer and tell Android we want to receive multicast datagrams.
|
||||
packet = new DatagramPacket(buffer, buffer.length);
|
||||
@@ -359,7 +359,7 @@ public class MainActivity extends Activity implements WifiOnDialogListener, Conn
|
||||
Logger.log_d(TAG, CLASS_NAME + ".run() :: Found a server at " + packet.getAddress().getHostAddress());
|
||||
String received = new String(packet.getData());
|
||||
Logger.log_d(TAG, CLASS_NAME + ".doInBackground() :: Packet payload is\n" + received);
|
||||
if(received.compareTo("NxtAR server here!") == 0)
|
||||
if(received.compareTo(ProjectConstants.SERVICE_DISCOVERY_MESSAGE) == 0)
|
||||
done = true;
|
||||
}
|
||||
result = true;
|
||||
|
@@ -46,6 +46,8 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
|
||||
private Activity parentActivity;
|
||||
private SurfaceHolder holder;
|
||||
private Camera camera;
|
||||
private int previewWidth;
|
||||
private int previewHeight;
|
||||
|
||||
@SuppressWarnings("deprecation")
|
||||
public CameraPreview(Context context, Camera camera){
|
||||
@@ -107,6 +109,8 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
|
||||
Logger.log_d(TAG, CLASS_NAME + ".surfaceChanged() :: Preview size set at (" + optimal.width + ", " + optimal.height + ")");
|
||||
camParams.setPreviewSize(optimal.width, optimal.height);
|
||||
camera.setParameters(camParams);
|
||||
previewWidth = optimal.width;
|
||||
previewHeight = optimal.height;
|
||||
|
||||
android.hardware.Camera.CameraInfo info = new android.hardware.Camera.CameraInfo();
|
||||
android.hardware.Camera.getCameraInfo(0, info);
|
||||
@@ -138,9 +142,8 @@ public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback
|
||||
|
||||
@Override
|
||||
public void onPreviewFrame(byte[] data, Camera camera){
|
||||
Size previewSize = camera.getParameters().getPreviewSize();
|
||||
if(imgMonitor.hasChanged())
|
||||
imgMonitor.setImageParameters(previewSize.width, previewSize.height);
|
||||
imgMonitor.setImageParameters(previewWidth, previewHeight);
|
||||
Logger.log_d(TAG, CLASS_NAME + ".onPreviewFrame() :: Preview received");
|
||||
Logger.log_d(TAG, CLASS_NAME + ".onPreviewFrame() :: Frame has" + (imgMonitor.hasChanged() ? "" : " not") + " been consumed.");
|
||||
imgMonitor.setImageData(data);
|
||||
|
@@ -63,10 +63,11 @@ public class ImageTransferThread extends Thread{
|
||||
Object auxiliary;
|
||||
ImageTransferProtocolMessage simpleMessage;
|
||||
ImageDataMessage imageMessage;
|
||||
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
|
||||
connectToServer();
|
||||
|
||||
if(socket.isConnected()){
|
||||
if(!socket.isConnected()){
|
||||
Logger.log_e(TAG, CLASS_NAME + ".run() :: Not connected to a server. Finishing thread.");
|
||||
}else{
|
||||
while(!done){
|
||||
@@ -101,7 +102,7 @@ public class ImageTransferThread extends Thread{
|
||||
sendUnrecognizedMessage();
|
||||
|
||||
}else{
|
||||
// Else if the passed the validity check then proceed to the next protocol state.
|
||||
// Else if the message passed the validity check then proceed to the next protocol state.
|
||||
simpleMessage = (ImageTransferProtocolMessage)auxiliary;
|
||||
if(simpleMessage.message == ImageTransferProtocol.ACK_SEND_NEXT)
|
||||
threadState = thread_state_t.CAN_SEND;
|
||||
@@ -113,8 +114,6 @@ public class ImageTransferThread extends Thread{
|
||||
break;
|
||||
|
||||
case CAN_SEND:
|
||||
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
|
||||
|
||||
// Get the image and it's parameters from the monitor.
|
||||
Rect imageSize = camMonitor.getImageParameters();
|
||||
image = camMonitor.getImageData();
|
||||
@@ -135,11 +134,21 @@ public class ImageTransferThread extends Thread{
|
||||
}catch(IOException io){
|
||||
Logger.log_e(TAG, CLASS_NAME + ".run() :: Error sending image to the server: " + io.getMessage());
|
||||
}
|
||||
|
||||
// Clean up stuff.
|
||||
yuvImage = null;
|
||||
image = null;
|
||||
outputStream.reset();
|
||||
imageMessage = null;
|
||||
imageSize = null;
|
||||
|
||||
threadState = thread_state_t.WAIT_FOR_ACK;
|
||||
break;
|
||||
|
||||
case END_STREAM:
|
||||
// Simply disconnect from the server.
|
||||
disconnect();
|
||||
done = true;
|
||||
break;
|
||||
}
|
||||
|
||||
|
@@ -2,7 +2,7 @@ package ve.ucv.ciens.ccg.nxtcam.network.protocols;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public final class ImageDataMessage extends ProtocolMessage implements Serializable{
|
||||
public final class ImageDataMessage implements Serializable{
|
||||
private static final long serialVersionUID = 9989L;
|
||||
public static final int magicNumber = 0x10;
|
||||
|
||||
|
@@ -2,7 +2,7 @@ package ve.ucv.ciens.ccg.nxtcam.network.protocols;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
public final class ImageTransferProtocolMessage extends ProtocolMessage implements Serializable{
|
||||
public final class ImageTransferProtocolMessage implements Serializable{
|
||||
private static final long serialVersionUID = 8898L;
|
||||
public static final int magicNumber = 0x20;
|
||||
|
||||
|
@@ -1,8 +0,0 @@
|
||||
package ve.ucv.ciens.ccg.nxtcam.network.protocols;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@SuppressWarnings("serial")
|
||||
public abstract class ProtocolMessage implements Serializable {
|
||||
public static int magicNumber = 0x00;
|
||||
}
|
@@ -27,6 +27,7 @@ public abstract class ProjectConstants {
|
||||
public static final UUID SERIAL_PORT_SERVICE_CLASS_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");
|
||||
public static final String OUI_LEGO = "00:16:53";
|
||||
public static final String MULTICAST_ADDRESS = "230.0.0.1";
|
||||
public static final String SERVICE_DISCOVERY_MESSAGE = "NxtAR server here!";
|
||||
|
||||
// Activity results.
|
||||
public static final int RESULT_CAMERA_FAILURE = Activity.RESULT_FIRST_USER + 1;
|
||||
|
Reference in New Issue
Block a user