Chris Pollett > Students >
Yaoyan

    ( Print View)

    [Bio]

    [Blog]

    [CS297Proposal]

    [Deliverable 1: GPS App]

    [Deliverable 2: Image Feature Selection]

    [Deliverable 3: Accelerometer and Video Recording Apps]

    [Deliverable 4: Model Survey]

    [Deliverable 5: CS297 Report]

    [Menze Paper Review Slides]

    [Pomerleau Paper Review Slides]

    [CS298Proposal]

    [CS298Report]

Code Android Apps to Capture Movement and Record Video

Description:

Two Apps are coded with Android Studio to capture the cellphone movement with accelerometer and record the real time traffic with camera. Accelerometer is perfect to capture the spatial accelerations in three dimensions and can be indicative of angular movement of a vehicle when it takes turns. A linear correlation will be established to convert the accelerometer readings into the degrees of angular movements of steering wheel later on. Sensor.TYPE_ACCELEROMETER is used and SensorEventListener is implemented. onSensorChanged() method gives the accelerations in three dimensions.

Video recording App, on the other hand, captures the traffic conditions and will store the videos to an external SD disc. Internal storage will be investigated and tested later on. Intent class is used to delegate the recording via Android existing ACTION_VIDEO_CAPTURE action. Videos are saved as URI objects for subsequent retrieval and play. StartActivityForResults() and onActivityResult() are the centerpiece of the code. onClickListener class is invoked for RECORD button on the main view.

How to run the code:

Two ways: emulator or android smartphone tethering. I mainly used emulator and double checked with tethering. Both work well albeit slower implementation on emulator.

For emulator, choose any virtual cellphone that supports Android API 28 and click RUN on Android Studio. Emulator provides the GNSS coordinates: click on ... button and GPS information is listed on the top of left-handed bar.

For cellphone tethering, a Huawei MateSE is used for testing. To debug and run the code on the cellphone, two steps are required: 1)activating developer mode on the cellphone. Follow Setting > About phone > Build Number > click seven times, and you are in developer mode. To allow for debugging and testing, follow Setting > System > Developer Mode > Debugging > select USB debugging; 2) choose the tethering mode in Android Studio by following RUN > Edit Configurations... > Delopyment Target Options > USB Device.

Accelerometer Source code:

package com.example.accelerometerdemo;

import android.content.Context;
import android.hardware.SensorEvent;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.hardware.SensorManager;
import android.hardware.Sensor;
import android.hardware.SensorEventListener;
import android.hardware.SensorEvent;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity implements SensorEventListener {
    private SensorManager sensorManager;
    private Sensor sensor, gsensor;
    private float[] gravity = {0.0f,0.0f,0.0f};
    private float[] linear_acceleration = {0.0f,0.0f,0.0f};
    private TextView xAccelerometer, yAccelerometer, zAccelerometer;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        xAccelerometer = (TextView) findViewById(R.id.xAccelerometer);
        yAccelerometer = (TextView) findViewById(R.id.yAccelerometer);
        zAccelerometer = (TextView) findViewById(R.id.zAccelerometer);

        sensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
        sensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
        if (sensor != null) {
            sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
        }
        else{
            System.out.println("There is no accelerometer! ");
            return;
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int id = item.getItemId();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    protected void onResume() {
        super.onResume();
        sensorManager.registerListener(this, sensor, SensorManager.SENSOR_DELAY_NORMAL);
    }

    protected void onPause() {
        super.onPause();
        sensorManager.unregisterListener(this);
    }

    public void onSensorChanged(SensorEvent event ){

            final float alpha = 0.8f;

            // Isolate the force of gravity with the low-pass filter.
                gravity[0] = alpha * gravity[0] + (1 - alpha) * event.values[0];
                gravity[1] = alpha * gravity[1] + (1 - alpha) * event.values[1];
                gravity[2] = alpha * gravity[2] + (1 - alpha) * event.values[2];

            // Remove the gravity contribution with the high-pass filter.
                linear_acceleration[0] = event.values[0] - gravity[0];
                linear_acceleration[1] = event.values[1] - gravity[1];
                linear_acceleration[2] = event.values[2] - gravity[2];

            xAccelerometer.setText(Float.toString(linear_acceleration[0]));
            yAccelerometer.setText(Float.toString(linear_acceleration[1]));
            zAccelerometer.setText(Float.toString(linear_acceleration[2]));
    }

    public void onAccuracyChanged(Sensor s, int x){
    }
}


Emulator Screenshot:

Text_description

Text_description

Video Recording Source code:

MainActivity.java

package com.example.videorecordingdemo;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Intent;
import android.hardware.Camera;
import android.widget.Toast;
import android.widget.VideoView;
import android.widget.Button;
import java.io.File;
import android.view.View.OnClickListener;

public class MainActivity extends AppCompatActivity  {

    // need Intent itself
    // call external Activity
    // handle returned video (DB)

    static final int REQUEST_VIDEO_CAPTURE = 1;
    private VideoView videoView;
    private Button playVideoButton;
    private Button recordVideoButton;
    private Uri videoFileUri;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });

        videoView = (VideoView) findViewById(R.id.videoView);
        playVideoButton = (Button) findViewById(R.id.playVideoButton);
        playVideoButton.setEnabled(false);
        recordVideoButton = (Button) findViewById(R.id.recordVideoButton);
        recordVideoButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //dispatchTakeVideoIntent();
                if (v == recordVideoButton){
                    Intent takeVideoIntent = new Intent(MediaStore.ACTION_VIDEO_CAPTURE);
                    startActivityForResult(takeVideoIntent, REQUEST_VIDEO_CAPTURE);
                }
                if (v == playVideoButton){
                    videoView.setVideoURI(videoFileUri);
                    videoView.start();
                }
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
        if (requestCode == REQUEST_VIDEO_CAPTURE && resultCode == RESULT_OK) {
            //videoView = (VideoView) findViewById(R.id.videoView);
            //Toast.makeText(getApplicationContext(),"Video saved", Toast.LENGTH_LONG).show();
            videoFileUri = intent.getData();
            playVideoButton.setEnabled(true);
        }
        else
            Toast.makeText(getApplicationContext(),"Error: video not saved", Toast.LENGTH_LONG).show();
    }
}

Emulator Screenshot:

Text_description

Initial UI

Text_description

Ready to Record

Text_description

Recording

Text_description

Replay the Video Captured

Text_description

Persistence

Reference: