Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Android OpenCV capturing an image from the phone camera.

I am trying to run an android app that uses openCV. My question is what can i call to capture an image from the camera? Is there a method like "captureAnImageFromYourPhoneCamera()"? I will try to post my code:

package com.kmf.helloopencv;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;

import com.kmf.helloopencv.util.SystemUiHider;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

/**
 * An implementation of OpenCV to control a lego NXT via an android phone. Work in progress.
 * Based off an OpenCV tutorial...
 * http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#dev-with-ocv-on-android
 * 
 * And a youtube video with accompanying code...
 * https://sites.google.com/site/ghoelzl/nxtcontrolv2
 * 
 * @author Kmf
 * @version Alpha
 * 
 */
public class HelloOpenCvActivity extends Activity implements CvCameraViewListener2 {
    /**
     * Whether or not the system UI should be auto-hidden after
     * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
     */
    private static final boolean AUTO_HIDE = true;
//********** Class fields added *************
private static final String TAG = "MyActivity";
private CameraBridgeViewBase mOpenCvCameraView;
//Random generator = new Random(); //for onCameraFrame experiment
private BluetoothSocket nxtBTsocket = null;
private DataOutputStream nxtDos = null;

public static final String DEFAULT_NXT_NAME = "NXT"; 
public static final int MOTOR_A_C_STOP = 0;
public static final int MOTOR_A_FORWARD = 1;
public static final int MOTOR_A_BACKWARD = 2;
public static final int MOTOR_C_FORWARD = 3;
public static final int MOTOR_C_BACKWARD = 4;
public static final int TACHOCOUNT_RESET = 8;
public static final int TACHOCOUNT_READ = 9;
public static final int ACTION=10;
public static final int DISCONNECT = 99;  

/**
 * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
 * user interaction before hiding the system UI.
 */
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

/**
 * If set, will toggle the system UI visibility upon interaction. Otherwise,
 * will show the system UI visibility upon interaction.
 */
private static final boolean TOGGLE_ON_CLICK = true;

/**
 * The flags to pass to {@link SystemUiHider#getInstance}.
 */
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;

/**
 * The instance of the {@link SystemUiHider} for this activity.
 */
private SystemUiHider mSystemUiHider;

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.helloopcvlayout);
    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView);
    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
    mOpenCvCameraView.setCvCameraViewListener(this);

    //Inserting createNXTConnection method from NXTControlAndroid.java

    try {
        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> bondedDevices = btAdapter.getBondedDevices();
        BluetoothDevice nxtDevice = null;

        for (BluetoothDevice bluetoothDevice : bondedDevices)
        {
            if (bluetoothDevice.getName().equals(DEFAULT_NXT_NAME)) {
                nxtDevice = bluetoothDevice;
                break;
            }
        }

        nxtBTsocket = nxtDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
        nxtBTsocket.connect();
        nxtDos = new DataOutputStream(nxtBTsocket.getOutputStream());

    } catch (IOException e) {
        Toast toast = Toast.makeText(this, "Problem at creating a connection", Toast.LENGTH_SHORT);
        toast.show();
    }



}

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

public void onDestroy() {
    super.onDestroy();
    destroyNXTConnection();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

public void onCameraViewStarted(int width, int height) {
}

public void onCameraViewStopped() {
}



public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    //make a decision based on the color
    //sendNXTcommand(colorDetector(inputFrame.rgba()), 200); 

    /*
     * The tutorial documentation warns against using the CvCameraViewFrame outside of this callback:
     * Note

"Do not save or use CvCameraViewFrame object out of onCameraFrame callback. This object does not have its own state and its behavior out of callback is unpredictable!" * * This would suggest to put the color detection (etc) here. Before this method returns. * */

    //write to data output stream
    sendNXTcommand(MOTOR_A_FORWARD, 100);
    sendNXTcommand(MOTOR_C_FORWARD, 100);

    return inputFrame.rgba(); //use to pick from either color or grayscale.

}


private int colorDetector(Mat rgba) {

    return 0;
}

//***************** OpenCV library initialization *********************
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
        case LoaderCallbackInterface.SUCCESS:
        {
            Log.i(TAG, "OpenCV loaded successfully");
            mOpenCvCameraView.enableView();
        } break;
        default:
        {
            super.onManagerConnected(status);
        } break;
        }
    }
};

//************ NXT Communication methods **********************

private void destroyNXTConnection() {
    try {
        if (nxtBTsocket != null) {
            // send one close message 
            sendNXTcommand(MOTOR_A_C_STOP,0);
            sendNXTcommand(DISCONNECT,0);
            nxtBTsocket.close();
            nxtBTsocket = null;
        }
        nxtDos = null;            
    } catch (IOException e) {
        Toast toast = Toast.makeText(this, "Problem at closing the connection", Toast.LENGTH_SHORT);
        toast.show();
    }
}

public void sendNXTcommand(int command, int value) {
    if (nxtDos == null) {
        return;
    }

    try {
        nxtDos.writeInt(command);
        nxtDos.writeInt(value);
        nxtDos.flush();
    } catch (IOException ioe) { 
        Toast toast = Toast.makeText(this, "Problem at writing command", Toast.LENGTH_SHORT);
        toast.show();            
    }
}



@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this, mLoaderCallback);
}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

    // Trigger the initial hide() shortly after the activity has been
    // created, to briefly hint to the user that UI controls
    // are available.
    delayedHide(100);
}

/**
 * Touch listener to use for in-layout UI controls to delay hiding the
 * system UI. This is to prevent the jarring behavior of controls going away
 * while interacting with activity UI.
 */
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (AUTO_HIDE) {
            delayedHide(AUTO_HIDE_DELAY_MILLIS);
        }
        return false;
    }
};

Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
    @Override
    public void run() {
        //mSystemUiHider.hide();
    }
};

/**
 * Schedules a call to hide() in [delay] milliseconds, canceling any
 * previously scheduled calls.
 */
private void delayedHide(int delayMillis) {
    mHideHandler.removeCallbacks(mHideRunnable);
    mHideHandler.postDelayed(mHideRunnable, delayMillis);
}

}

click to hide/show revision 2
retagged

updated 2013-10-26 14:45:39 -0600

berak gravatar image

Android OpenCV capturing an image from the phone camera.

I am trying to run an android app that uses openCV. My question is what can i call to capture an image from the camera? Is there a method like "captureAnImageFromYourPhoneCamera()"? I will try to post my code:

package com.kmf.helloopencv;

import java.io.DataOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.Set;
import java.util.UUID;

import org.opencv.android.BaseLoaderCallback;
import org.opencv.android.CameraBridgeViewBase;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewFrame;
import org.opencv.android.CameraBridgeViewBase.CvCameraViewListener2;
import org.opencv.android.LoaderCallbackInterface;
import org.opencv.android.OpenCVLoader;
import org.opencv.core.Mat;

import com.kmf.helloopencv.util.SystemUiHider;

import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothSocket;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.MotionEvent;
import android.view.SurfaceView;
import android.view.View;
import android.view.WindowManager;
import android.widget.TextView;
import android.widget.Toast;

/**
 * An implementation of OpenCV to control a lego NXT via an android phone. Work in progress.
 * Based off an OpenCV tutorial...
 * http://docs.opencv.org/doc/tutorials/introduction/android_binary_package/dev_with_OCV_on_Android.html#dev-with-ocv-on-android
 * 
 * And a youtube video with accompanying code...
 * https://sites.google.com/site/ghoelzl/nxtcontrolv2
 * 
 * @author Kmf
 * @version Alpha
 * 
 */
public class HelloOpenCvActivity extends Activity implements CvCameraViewListener2 {
    /**
     * Whether or not the system UI should be auto-hidden after
     * {@link #AUTO_HIDE_DELAY_MILLIS} milliseconds.
     */
    private static final boolean AUTO_HIDE = true;
//********** Class fields added *************
private static final String TAG = "MyActivity";
private CameraBridgeViewBase mOpenCvCameraView;
//Random generator = new Random(); //for onCameraFrame experiment
private BluetoothSocket nxtBTsocket = null;
private DataOutputStream nxtDos = null;

public static final String DEFAULT_NXT_NAME = "NXT"; 
public static final int MOTOR_A_C_STOP = 0;
public static final int MOTOR_A_FORWARD = 1;
public static final int MOTOR_A_BACKWARD = 2;
public static final int MOTOR_C_FORWARD = 3;
public static final int MOTOR_C_BACKWARD = 4;
public static final int TACHOCOUNT_RESET = 8;
public static final int TACHOCOUNT_READ = 9;
public static final int ACTION=10;
public static final int DISCONNECT = 99;  

/**
 * If {@link #AUTO_HIDE} is set, the number of milliseconds to wait after
 * user interaction before hiding the system UI.
 */
private static final int AUTO_HIDE_DELAY_MILLIS = 3000;

/**
 * If set, will toggle the system UI visibility upon interaction. Otherwise,
 * will show the system UI visibility upon interaction.
 */
private static final boolean TOGGLE_ON_CLICK = true;

/**
 * The flags to pass to {@link SystemUiHider#getInstance}.
 */
private static final int HIDER_FLAGS = SystemUiHider.FLAG_HIDE_NAVIGATION;

/**
 * The instance of the {@link SystemUiHider} for this activity.
 */
private SystemUiHider mSystemUiHider;

@Override
public void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "called onCreate");
    super.onCreate(savedInstanceState);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.helloopcvlayout);
    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.HelloOpenCvView);
    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
    mOpenCvCameraView.setCvCameraViewListener(this);

    //Inserting createNXTConnection method from NXTControlAndroid.java

    try {
        BluetoothAdapter btAdapter = BluetoothAdapter.getDefaultAdapter();
        Set<BluetoothDevice> bondedDevices = btAdapter.getBondedDevices();
        BluetoothDevice nxtDevice = null;

        for (BluetoothDevice bluetoothDevice : bondedDevices)
        {
            if (bluetoothDevice.getName().equals(DEFAULT_NXT_NAME)) {
                nxtDevice = bluetoothDevice;
                break;
            }
        }

        nxtBTsocket = nxtDevice.createRfcommSocketToServiceRecord(UUID.fromString("00001101-0000-1000-8000-00805F9B34FB"));
        nxtBTsocket.connect();
        nxtDos = new DataOutputStream(nxtBTsocket.getOutputStream());

    } catch (IOException e) {
        Toast toast = Toast.makeText(this, "Problem at creating a connection", Toast.LENGTH_SHORT);
        toast.show();
    }



}

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

public void onDestroy() {
    super.onDestroy();
    destroyNXTConnection();
    if (mOpenCvCameraView != null)
        mOpenCvCameraView.disableView();
}

public void onCameraViewStarted(int width, int height) {
}

public void onCameraViewStopped() {
}



public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    //make a decision based on the color
    //sendNXTcommand(colorDetector(inputFrame.rgba()), 200); 

    /*
     * The tutorial documentation warns against using the CvCameraViewFrame outside of this callback:
     * Note

"Do not save or use CvCameraViewFrame object out of onCameraFrame callback. This object does not have its own state and its behavior out of callback is unpredictable!" * * This would suggest to put the color detection (etc) here. Before this method returns. * */

    //write to data output stream
    sendNXTcommand(MOTOR_A_FORWARD, 100);
    sendNXTcommand(MOTOR_C_FORWARD, 100);

    return inputFrame.rgba(); //use to pick from either color or grayscale.

}


private int colorDetector(Mat rgba) {

    return 0;
}

//***************** OpenCV library initialization *********************
private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
        case LoaderCallbackInterface.SUCCESS:
        {
            Log.i(TAG, "OpenCV loaded successfully");
            mOpenCvCameraView.enableView();
        } break;
        default:
        {
            super.onManagerConnected(status);
        } break;
        }
    }
};

//************ NXT Communication methods **********************

private void destroyNXTConnection() {
    try {
        if (nxtBTsocket != null) {
            // send one close message 
            sendNXTcommand(MOTOR_A_C_STOP,0);
            sendNXTcommand(DISCONNECT,0);
            nxtBTsocket.close();
            nxtBTsocket = null;
        }
        nxtDos = null;            
    } catch (IOException e) {
        Toast toast = Toast.makeText(this, "Problem at closing the connection", Toast.LENGTH_SHORT);
        toast.show();
    }
}

public void sendNXTcommand(int command, int value) {
    if (nxtDos == null) {
        return;
    }

    try {
        nxtDos.writeInt(command);
        nxtDos.writeInt(value);
        nxtDos.flush();
    } catch (IOException ioe) { 
        Toast toast = Toast.makeText(this, "Problem at writing command", Toast.LENGTH_SHORT);
        toast.show();            
    }
}



@Override
public void onResume()
{
    super.onResume();
    OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_2_4_5, this, mLoaderCallback);
}


@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);

    // Trigger the initial hide() shortly after the activity has been
    // created, to briefly hint to the user that UI controls
    // are available.
    delayedHide(100);
}

/**
 * Touch listener to use for in-layout UI controls to delay hiding the
 * system UI. This is to prevent the jarring behavior of controls going away
 * while interacting with activity UI.
 */
View.OnTouchListener mDelayHideTouchListener = new View.OnTouchListener() {
    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        if (AUTO_HIDE) {
            delayedHide(AUTO_HIDE_DELAY_MILLIS);
        }
        return false;
    }
};

Handler mHideHandler = new Handler();
Runnable mHideRunnable = new Runnable() {
    @Override
    public void run() {
        //mSystemUiHider.hide();
    }
};

/**
 * Schedules a call to hide() in [delay] milliseconds, canceling any
 * previously scheduled calls.
 */
private void delayedHide(int delayMillis) {
    mHideHandler.removeCallbacks(mHideRunnable);
    mHideHandler.postDelayed(mHideRunnable, delayMillis);
}

}