简体   繁体   中英

Livestreaming app for the SmartEyeglass - Localhost

I have downloaded the Livestreaming App for the SmartEyeglass and I want to enter certain things to it but I don't know how.For the App to work you need to install it to your phone and open the localhost web page in your computer.In the localhost web page, there is a camera view which displays what the wearer of the Smarteyeglass is seeing and a textbox in which you can write what you want the wearer to see. In this webpage I want to add another textbox in which I will be able to take feedback from the wearer.But I have no idea in which part of the code I can add that. Can you help me? How do I change the layout in the localhost webpage?

public final class LivestreamingControl extends ControlExtension {

/**
 * Uses SmartEyeglass API version
 */
private static final int SMARTEYEGLASS_API_VERSION = 3;
public final int width;
public final int height;
// The application context.
private final Context context;

private static final String[] BUTTONS1 = {"Βράχος", "Φυτό", "Ρομπότ"};
private static final String[] BUTTONS2 = {"Κίνηση", "Αναπνοή", "Και τα δύο"};
private static final String[] BUTTONS3 = {"Ζωικό", "Φυτικό", "Και τα δύο"};
private static final String[] BUTTONS4 = {"Ζωικό", "Φυτικό", "Και τα δύο"};
private static final String[] BUTTONS5 = {"Ουσίες","Πλη/φορίες"};
private static final String[] BUTTONS6 = {"Σωστό", "Λάθος"};
private static final String[] BUTTONS7 = {"Σωστό", "Λάθος"};
private static final String[] BUTTONS8 = {"Σωστό", "Λάθος"};
private static final String[] BUTTONS9 = {"Σωστό", "Λάθος"};
private static final String[] BUTTONS10 = {"Ιοί", "Μύκητες", "Και τα δύο"};

private boolean q1 =false;
private boolean q2 =false;
private boolean q3 =false;
private boolean q4 =false;
private boolean q5 =false;
private boolean q6 =false;
private boolean q7 =false;
private boolean q8 =false;
private boolean q9 =false;
private boolean q10 =false;
private boolean aq =false;
private int qn = 0;


private boolean dialogClosed;
private int question =0;
private int count=0;

/**
 * Instance of the Control Utility class.
 */
private final SmartEyeglassControlUtils utils;
private boolean cameraStarted = false;
private int recordingMode = SmartEyeglassControl.Intents.CAMERA_MODE_STILL;
private int pointX;
private int pointY;

private SharedPreferences sharedPref;

private SocketClient socket;

private String serverIP = "";

/**
 * Creates an instance of this control class.
 *
 * @param context            The context.
 * @param hostAppPackageName Package name of host application.
 */
public LivestreamingControl(final Context context, final String hostAppPackageName) {
    super(context, hostAppPackageName);
    this.context = context;
    sharedPref = PreferenceManager.getDefaultSharedPreferences(context);
    serverIP = sharedPref.getString("ipAddress","");

    // Checking if server IP address was not set yet on mobile
    if (serverIP.equals("")) {
        updateDisplay();
    }
    else {
        // Start socket client connection to server
        socket = new SocketClient(serverIP);
        socket.connect();
    }

    // Initialize listener for camera events
    SmartEyeglassEventListener listener = new SmartEyeglassEventListener() {
        // When camera operation has succeeded
        // handle result according to current recording mode
        @Override
        public void onCameraReceived(final CameraEvent event) {
            // Retrieve image data
            String imageEncoded = Base64.encodeToString( event.getData(), Base64.DEFAULT);
            // Send image data as message over socket client
            socket.sendMessage("new image", imageEncoded);

            Log.d(Constants.LOG_TAG, "Stream Event coming: " + event.toString());
            updateDisplay();
        }

        @Override
        public void onDialogClosed(int code) {
            dialogClosed = true;
            //Η ΣΩΣΤΕΣ ΑΠΑΝΤΗΣΕΙΣ ΚΑΘΕ ΕΡΩΤΗΣΗΣ
            if(question== 1)
            {
                if (code == 0) {
                    utils.showDialogMessage("ΛΑΘΟΣ:Tο Φυτό", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    cameraStarted = true;
                } else if (code == 1) {
                    utils.showDialogMessage("ΣΩΣΤΟ", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    count++;
                    cameraStarted = true;
                }
                else if (code == 2){
                    utils.showDialogMessage("ΛΑΘΟΣ:Tο Φυτό", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    cameraStarted = true;
                }
            }
            else if(question ==2)
            {
                if (code == 0) {
                    utils.showDialogMessage("ΛΑΘΟΣ:Και τα δύο", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    cameraStarted = true;
                } else if (code == 1) {
                    utils.showDialogMessage("ΛΑΘΟΣ:Και τα δύο", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    cameraStarted = true;
                }
                else if (code == 2){
                    utils.showDialogMessage("ΣΩΣΤΟ", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    count++;
                    socket.message = "";
                    cameraStarted = true;
                }

            }
            else if(question ==3)
            {
                if (code == 0) {
                    utils.showDialogMessage("ΛΑΘΟΣ:Και τα δύο έχουν      πυρήνα", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    cameraStarted = true;
                } else if (code == 1) {
                    utils.showDialogMessage("ΛΑΘΟΣ:Και τα δύο έχουν      πυρήνα", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    cameraStarted = true;
                }
                else if (code == 2){
                    utils.showDialogMessage("ΣΩΣΤΟ", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    count++;
                    cameraStarted = true;
                }
            }
            else if(question ==4)
            {
                if (code == 0) {
                    utils.showDialogMessage("ΛΑΘΟΣ:Τα φυτικά         κύτταρα", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    cameraStarted = true;
                } else if (code == 1) {
                    utils.showDialogMessage("ΣΩΣΤΟ", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    count++;
                    cameraStarted = true;
                }
                else if (code == 2){
                    utils.showDialogMessage("ΛΑΘΟΣ:Τα φυτικά         κύτταρα", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    cameraStarted = true;
                }
            }
            else if(question ==5)
            {
                if (code == 0) {
                    utils.showDialogMessage("ΣΩΣΤΟ", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    count++;
                    cameraStarted = true;
                } else if (code == 1) {
                    utils.showDialogMessage("ΛΑΘΟΣ:Αποθηκεύονται   χρήσιμες ουσίες", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    cameraStarted = true;
                }
            }
            else if(question ==6)
            {
                if (code == 0) {
                    utils.showDialogMessage("ΛΑΘΟΣ:Ο πυρήνας αποτε-  λεί το κέντρο ελέγχου", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    cameraStarted = true;
                } else if (code == 1) {
                    utils.showDialogMessage("ΣΩΣΤΟ", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    count++;
                    cameraStarted = true;
                }
            }
            else if(question ==7)
            {
                if (code == 0) {
                    utils.showDialogMessage("ΛΑΘΟΣ:Δεν έχουν                 σκελετό", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    cameraStarted = true;
                } else if (code == 1) {
                    utils.showDialogMessage("ΣΩΣΤΟ", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    count++;
                    cameraStarted = true;
                }
            }
            else if(question ==8)
            {
                if (code == 0) {
                    utils.showDialogMessage("ΛΑΘΟΣ:Στους χλωροπλά-   στες", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    cameraStarted = true;
                } else if (code == 1) {
                    utils.showDialogMessage("ΣΩΣΤΟ", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    count++;
                    cameraStarted = true;
                }
            }
            else if(question ==9)
            {
                if (code == 0) {
                    utils.showDialogMessage("ΣΩΣΤΟ", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    count++;
                    cameraStarted = true;
                } else if (code == 1) {
                    utils.showDialogMessage("ΛΑΘΟΣ", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    cameraStarted = true;
                }
            }
            else if(question ==10)
            {
                if (code == 0) {
                    utils.showDialogMessage("ΛΑΘΟΣ:Και τα δύο", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    cameraStarted = true;
                } else if (code == 1) {
                    utils.showDialogMessage("ΛΑΘΟΣ:Και τα δύο", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    cameraStarted = true;
                }
                else if (code == 2){
                    utils.showDialogMessage("ΣΩΣΤΟ", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    count++;
                    cameraStarted = true;
                }
            }
            else if(qn ==1)
            {
                    socket.message = "";
                    String message0 = "";
                    aq=true;
                    qn=0;
                    utils.showDialogMessage("ΣΩΣΤΟ ή ΛΑΘΟΣ?", message0, BUTTONS6);
            }
            else if(aq)
            {
                if (code == 0) {
                    utils.showDialogMessage("ΛΑΘΟΣ", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    aq=false;
                    cameraStarted = true;
                } else if (code == 1) {
                    utils.showDialogMessage("ΣΩΣΤΟ", SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
                    socket.message = "";
                    aq=false;
                    count++;
                    cameraStarted = true;
                }
            }
        }

        // Called when camera operation has failed
        // We just log the error
        @Override
        public void onCameraErrorReceived(final int error) {
            Log.d(Constants.LOG_TAG, "onCameraErrorReceived: " + error);
        }

        // When camera is set to record image to a file,
        // log the operation and clean up
        @Override
        public void onCameraReceivedFile(final String filePath) {
            Log.d(Constants.LOG_TAG, "onCameraReceivedFile: " + filePath);
            updateDisplay();
        }
    };
    utils = new SmartEyeglassControlUtils(hostAppPackageName, listener);
    utils.setRequiredApiVersion(SMARTEYEGLASS_API_VERSION);
    utils.activate(context);

    width = context.getResources().getDimensionPixelSize(R.dimen.smarteyeglass_control_width);
    height = context.getResources().getDimensionPixelSize(R.dimen.smarteyeglass_control_height);

    // Switch WLAN mode ON to achieve higher speed on live stream
    utils.setPowerMode(SmartEyeglassControl.Intents.POWER_MODE_HIGH);
}

// Respond to tap on touch pad by triggering camera capture.
@Override
public void onTouch(final ControlTouchEvent event) {
    if (event.getAction() == Control.TapActions.SINGLE_TAP) {
        if (!cameraStarted) {
            initializeCamera();
        } else {
            cleanupCamera();
        }
        updateDisplay();
    }
}

// Call the startCamera, and start the camera.
private void initializeCamera() {
    try {
        Time now = new Time();
        now.setToNow();
        // Start camera without filepath for other recording modes
        Log.d(Constants.LOG_TAG, "startCamera ");
        utils.startCamera();
    } catch (ControlCameraException e) {
        Log.d(Constants.LOG_TAG, "Failed to register listener", e);
    }
    Log.d(Constants.LOG_TAG, "onResume: Registered listener");
    cameraStarted = true;
}

// Call the stopCamera, and stop the camera.
private void cleanupCamera() {
    utils.stopCamera();
    cameraStarted = false;
}

// When app becomes visible, set up camera mode choices
// and instruct user to begin camera operation
@Override
public void onResume() {
    socket = new SocketClient(serverIP);
    socket.connect();
    // Note: Setting the screen to be always on will drain the accessory
    // battery. It is done here solely for demonstration purposes.
    setScreenState(Control.Intents.SCREEN_STATE_ON);
    pointX = context.getResources().getInteger(R.integer.POINT_X);
    pointY = context.getResources().getInteger(R.integer.POINT_Y);

    // Read the settings for the extension.
    SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    int recMode = Integer.parseInt(prefs.getString(context.getString(R.string.preference_key_recordmode), "2"));
    int preferenceId = R.string.preference_key_resolution_still;

    recordingMode = SmartEyeglassControl.Intents.CAMERA_MODE_JPG_STREAM_LOW_RATE;
    preferenceId = R.string.preference_key_resolution_movie;

    // Get and show quality parameters
    int jpegQuality = Integer.parseInt(prefs.getString(context.getString(R.string.preference_key_jpeg_quality), "1"));
    int resolution = Integer.parseInt(prefs.getString(context.getString(preferenceId), "6"));

    // Set the camera mode to match the setup
    utils.setCameraMode(jpegQuality, resolution, recordingMode);

    cameraStarted = false;
    updateDisplay();
}

// Clean up any open files and reset mode when app is paused.
@Override
public void onPause() {
    socket.disconnect();
    // Stop camera.
    if (cameraStarted) {
        Log.d(Constants.LOG_TAG, "onPause() : stopCamera");
        cleanupCamera();
    }
}

// Clean up data structures on termination.
@Override
public void onDestroy() {
    utils.deactivate();
    socket.disconnect();
}

private void updateDisplay()
{
    Bitmap displayBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
    displayBitmap.setDensity(DisplayMetrics.DENSITY_DEFAULT);
    Canvas canvas = new Canvas(displayBitmap);
    Paint paint = new Paint();
    paint.setStyle(Paint.Style.FILL);
    paint.setTextSize(16);

    paint.setColor(Color.WHITE);

    // Update layout according to current state of the app
    if (serverIP.equals("")) {
        canvas.drawText("First open the Stream Test app and set the ip address", pointX, pointY, paint);
    }
    else if (cameraStarted) {

    //ΟΙ ΕΡΩΤΗΣΕΙΣ!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

            if (socket.message.equals("1") && !q1) {
                cameraStarted = false;
                q1=true;
                question=1;
                String title0 = "Ποιό από αυτά";
                String message0 = "είναι έμβιο όν;";
                utils.showDialogMessage(title0, message0, BUTTONS1);
            }
            else if(socket.message.equals("2")&& !q2) {
                cameraStarted = false;
                q2=true;
                question=2;
                String title0 = "Λειτουργία της";
                String message0 = "ζωής είναι η:";
                utils.showDialogMessage(title0, message0, BUTTONS2);
            }
            else if(socket.message.equals("3") && !q3) {
                cameraStarted = false;
                q3=true;
                question=3;
                String title0 = "Ποιά κύτταρα";
                String message0 = "έχουν πυρήνα;";
                utils.showDialogMessage(title0, message0, BUTTONS3);
            }
            else if(socket.message.equals("4")&& !q4) {
                cameraStarted = false;
                q4=true;
                question=4;
                String title0 = "Ποιά κύτταρα έχουν";
                String message0 = "κυτταρικό τοίχωμα;";
                utils.showDialogMessage(title0, message0, BUTTONS4);
            }
            else if(socket.message.equals("5")&& !q5) {
                cameraStarted = false;
                q5=true;
                question=5;
                String title0 = "Στο χυμοτόποιο";
                String message0 = "αποθηκεύονται:";
                utils.showDialogMessage(title0, message0, BUTTONS5);
            }
            else if(socket.message.equals("6")&& !q6) {
                cameraStarted = false;
                q6=true;
                question=6;
                String title0 = "Ο πυρήνας αποτελεί";
                String message0 = "εργοστάσιο ενέργειας.";
                utils.showDialogMessage(title0, message0, BUTTONS6);
            }
            else if(socket.message.equals("7")&& !q7) {
                cameraStarted = false;
                q7=true;
                question=7;
                String title0 = "Όλα τα φυτά";
                String message0 = "έχουν σκελετό.";
                utils.showDialogMessage(title0, message0, BUTTONS7);
            }
            else if(socket.message.equals("8")&& !q8) {
                cameraStarted = false;
                q8=true;
                question=8;
                String title0 = "Στα μιτοχόνδρια";
                String message0 = "γίνεται η φωτοσύνθεση.";
                utils.showDialogMessage(title0, message0, BUTTONS8);
            }
            else if(socket.message.equals("9")&& !q9) {
                cameraStarted = false;
                q9=true;
                question=9;
                String title0 = "Η χλωροφύλλη";
                String message0 = "είναι πράσινη.";
                utils.showDialogMessage(title0, message0, BUTTONS9);
            }
            else if(socket.message.equals("10")&& !q10) {
                cameraStarted = false;
                q10=true;
                question=10;
                String title0 = "Μονοκύτταροι είναι";
                String message0 = "ποιοι οργανισμοί;";
                utils.showDialogMessage(title0, message0, BUTTONS10);
            }
            else if(socket.message.endsWith("?")) {
                String title0 = "";
                cameraStarted = false;
                 qn =1;
                utils.showDialogMessage(socket.message, SmartEyeglassControl.Intents.DIALOG_MODE_TIMEOUT);
            }
            else if(socket.message.equals("s")) {
                canvas.drawText("SOS: δώσε προσοχή", pointX, (pointY * 3), paint);
            }
            else if(socket.message.equals("x")) {
                canvas.drawText("Ξυπνήστε!!!", pointX, (pointY * 3), paint);
            }
            else {
                canvas.drawText("JPEG Streaming...", pointX, pointY, paint);
                canvas.drawText("Tap to stop.", pointX, (pointY * 2), paint);
                canvas.drawText(socket.message, pointX, (pointY * 3), paint);
                canvas.drawText("Έχεις απαντήσει σωστά "+count+"/10 ερωτήσεις", pointX, (pointY * 4), paint);
            }

    } else {
        canvas.drawText("Tap to start JPEG Stream.", pointX, pointY, paint);
    }
    Log.d(Constants.LOG_TAG, "updateDisplay() : updating SmartEyeglass display");
    showBitmap(displayBitmap);
}

SocketClient Class

public class SocketClient {

    public Socket mSocket;
    private String SERVER_URL;
    private Activity activity;
    private Context ctxt;
    public static String message = "";

    public SocketClient(String ipAddress) {
        SERVER_URL = "http://"+ipAddress+":80";
    }

    public void connect() {


        //Try to connect to the socket
        try {
            Log.d("mySony", "connecting to socket : "+SERVER_URL);
            mSocket = IO.socket(SERVER_URL);
            mSocket.on(Socket.EVENT_CONNECT_ERROR, onConnectError);
            mSocket.on(Socket.EVENT_CONNECT_TIMEOUT, onConnectTimeout);
            mSocket.on("new chat message",  new Emitter.Listener() {
                @Override
                public void call(final Object... args) {
                    Log.d("mySony", args.toString());
                    message = args[0].toString();
                }
            });

            mSocket.connect();

        } catch (URISyntaxException e) {
            throw new RuntimeException(e);
        }
    }

    public void sendMessage(String socket, String message) {
        mSocket.emit(socket, message);
    }

    public void disconnect() {
        mSocket.disconnect();
        mSocket.off(Socket.EVENT_CONNECT_ERROR, onConnectError);
        mSocket.off(Socket.EVENT_CONNECT_TIMEOUT, onConnectError);
        mSocket.off("new chat message", onNewMessage);
    }

    private Emitter.Listener onConnectError = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            Log.d("mySony", "Connection error!! ");
            for (int x=0; x<args.length; x++) {
                Log.d("mySony", x+" = "+args[x].toString());
            }
        }
    };

    private Emitter.Listener onConnectTimeout = new Emitter.Listener() {
        @Override
        public void call(Object... args) {
            Log.d("mySony", "Connection timed out!!");
        }
    };

    private Emitter.Listener onNewMessage = new Emitter.Listener() {
        @Override
        public void call(final Object... args) {
            JSONObject data = (JSONObject) args[0];
            Log.d("mySony", "data = " + data.toString());
        }
    };
}

要更新localhost服务器,您将需要编辑PC上运行的PC-nodeJS-Server文件夹中的HTML和Javascript文件。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM