Android Open Source - Cam2IMU_calib Main Activity






From Project

Back to project page Cam2IMU_calib.

License

The source code is released under:

MIT License

If you think the Android project Cam2IMU_calib listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package cvg.vslam.c2i_calib;
//  w ww.  jav a2 s .c  o m
import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;

import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Date;

public class MainActivity extends Activity {

    private static final String TAG = "MainActivity";

    private Camera mCamera;
    private CameraPreview mPreview;
    private SensorBuffer mSensorBuffer;
    private SaveDataDialog mSaveDialog;

    private static File storageDir;
    private static String sampleIndex;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        mCamera = getCameraInstance();

        mPreview = (CameraPreview) findViewById(R.id.surfaceView);
        mPreview.init(mCamera);

        mSensorBuffer = new SensorBuffer(this);

        setupStorage();
        mSaveDialog = new SaveDataDialog(storageDir, this);

        mPreview.setOnClickListener(new View.OnClickListener() {
            int imgIndex = 0;
            @Override
            public void onClick(View v) {
                sampleIndex = String.format("%02d", imgIndex++);
                // TODO: do buffer shit ...
                mCamera.takePicture(null, null, mPicture);
            }
        });
    }

    private static File getOutputFile(String nameSuffix){
        File file;
        file = new File(storageDir.getPath() + File.separator + "IMG_"+ nameSuffix);
        if (file == null){
            Log.e(TAG, "Error creating files, check storage permissions");
        }
        return file;
    }

    private Camera.PictureCallback mPicture = new Camera.PictureCallback() {
        @Override
        public void onPictureTaken(byte[] data, Camera camera) {

            File pictureFile = getOutputFile(sampleIndex + ".jpg");
            File sensorFile  = getOutputFile("imu_" + sampleIndex + ".txt");

            try {
                FileOutputStream fos = new FileOutputStream(pictureFile);
                fos.write(data);
                fos.close();
                BufferedWriter writer = new BufferedWriter(new FileWriter(sensorFile));
                writer.write(mSensorBuffer.getBufferAt(0));
                writer.close();
            } catch (FileNotFoundException e) {
                Log.d(TAG, "File not found: " + e.getMessage());
            } catch (IOException e) {
                Log.d(TAG, "Error accessing file: " + e.getMessage());
            }
        }
    };

    @Override
    public void onPause(){
        super.onPause();
        if (mCamera != null){
            mCamera.release();
            mCamera = null;
        }

    }

    @Override
    public void onBackPressed(){
        mSaveDialog.show(getFragmentManager(), "Tag");
    }

    private void setupStorage(){
        String timeStamp = new SimpleDateFormat("MM-dd_HH-mm-ss").format(new Date());
        storageDir = new File(Environment.getExternalStorageDirectory(),
                "Cam2Imu" + File.separator + timeStamp);

        if (! storageDir.exists()){
            if (! storageDir.mkdirs()){
                Log.d(TAG, "Failed to create directory");
                return;
            }
        }
    }

    public static Camera getCameraInstance(){
        Camera c = null;
        try {
            c = Camera.open();
        }
        catch (Exception e){
        }
        return c;
    }
}




Java Source Code List

cvg.vslam.c2i_calib.CameraPreview.java
cvg.vslam.c2i_calib.MainActivity.java
cvg.vslam.c2i_calib.SaveDataDialog.java
cvg.vslam.c2i_calib.SensorBuffer.java