Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Ok, same stuff, just with JavaCameraView:

activity:

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import org.opencv.android.BaseLoaderCallback;
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.CvType;
import org.opencv.core.Mat;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.WindowManager;
import android.widget.Toast;

public class test extends Activity implements CvCameraViewListener2 {
private static final String TAG = "test";

private JavaCamResView mOpenCvCameraView;
private List<Camera.Size> mResolutionList;

private MenuItem[] mResolutionMenuItems;
private MenuItem[] mFocusListItems;
private MenuItem[] mFlashListItems;

private SubMenu mResolutionMenu;
private SubMenu mFocusMenu;
private SubMenu mFlashMenu;



private Mat mGrayMat;

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;
        }
    }
};

public test() {
    Log.i(TAG, "Instantiated new " + this.getClass());
}

@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.java_cam_res_view);

    mOpenCvCameraView = (JavaCamResView) findViewById(R.id.test_view);
    mOpenCvCameraView.setCvCameraViewListener(this); 

}

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

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


}

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

public void onCameraViewStarted(int width, int height) {        

    mGrayMat = new Mat(height, width, CvType.CV_8UC1);

}

public void onCameraViewStopped() { 
    mGrayMat.release();
}

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    mGrayMat=inputFrame.gray();        
    return mGrayMat;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    List<String> mFocusList = new LinkedList<String>();
    int idx =0;

    mFocusMenu = menu.addSubMenu("Focus");

    mFocusList.add("Auto");
    mFocusList.add("Continuous Video");
    mFocusList.add("EDOF");
    mFocusList.add("Fixed");
    mFocusList.add("Infinity");
    mFocusList.add("Makro");
    mFocusList.add("Continuous Picture");

    mFocusListItems = new MenuItem[mFocusList.size()];

    ListIterator<String> FocusItr = mFocusList.listIterator();
    while(FocusItr.hasNext()){
        // add the element to the mDetectorMenu submenu
        String element = FocusItr.next();
        mFocusListItems[idx] = mFocusMenu.add(2,idx,Menu.NONE,element);
        idx++;
    }



    List<String> mFlashList = new LinkedList<String>();
    idx = 0;

    mFlashMenu = menu.addSubMenu("Flash");

    mFlashList.add("Auto");
    mFlashList.add("Off");
    mFlashList.add("On");
    mFlashList.add("Red-Eye");
    mFlashList.add("Torch");

    mFlashListItems = new MenuItem[mFlashList.size()];

    ListIterator<String> FlashItr = mFlashList.listIterator();
    while(FlashItr.hasNext()){
        // add the element to the mDetectorMenu submenu
        String element = FlashItr.next();
        mFlashListItems[idx] = mFlashMenu.add(3,idx,Menu.NONE,element);
        idx++;
    }



    mResolutionMenu = menu.addSubMenu("Resolution");
    mResolutionList = mOpenCvCameraView.getResolutionList();
    mResolutionMenuItems = new MenuItem[mResolutionList.size()];

    ListIterator<Camera.Size> resolutionItr = mResolutionList.listIterator();
    idx = 0;
    while(resolutionItr.hasNext()) {
        Camera.Size element = resolutionItr.next();
        mResolutionMenuItems[idx] = mResolutionMenu.add(1, idx, Menu.NONE,
                Integer.valueOf((int) element.width).toString() + "x" + Integer.valueOf((int) element.height).toString());
        idx++;
     }

    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    Log.e(TAG, "called onOptionsItemSelected; selected item: " + item);
   if (item.getGroupId() == 1)
    {
        int id = item.getItemId();
        Camera.Size resolution = mResolutionList.get(id);
        mOpenCvCameraView.setResolution(resolution);
        resolution = mOpenCvCameraView.getResolution();
        Log.e("test","test");
        String caption = Integer.valueOf((int) resolution.width).toString() + "x" + Integer.valueOf((int) resolution.height).toString();
        Toast.makeText(this, caption, Toast.LENGTH_SHORT).show();
    } 
    else if (item.getGroupId()==2){

       int focusType = item.getItemId();
       String caption = "Focus Mode: "+ (String)item.getTitle();
       Toast.makeText(this, caption, Toast.LENGTH_SHORT).show();

       mOpenCvCameraView.setFocusMode(focusType);
    }
    else if (item.getGroupId()==3){

       int flashType = item.getItemId();
       String caption = "Flash Mode: "+ (String)item.getTitle();
       Toast.makeText(this, caption, Toast.LENGTH_SHORT).show();

       mOpenCvCameraView.setFlashMode(flashType);
    }

    return true;
}   
}

JavaCamResView:

import java.util.List;

import org.opencv.android.JavaCameraView;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;

import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;

public class JavaCamResView extends JavaCameraView {

public JavaCamResView(Context context, AttributeSet attrs) {
    super(context, attrs);
} 

public List<Camera.Size> getResolutionList() {      
    return  mCamera.getParameters().getSupportedPreviewSizes();      
}

public void setResolution(Camera.Size resolution) {
    disconnectCamera();
    connectCamera((int)resolution.width, (int)resolution.height);       
}

public void setFocusMode (int type){

    Camera.Parameters params = mCamera.getParameters();     

    switch (type){
    case 0:         
         params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        break;
    case 1:         
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
        break;
    case 2:         
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_EDOF);
        break;
    case 3:
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);
        break;
    case 4:
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
        break;
    case 5:
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
        break;      
    case 6:
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        break;
    }

    mCamera.setParameters(params);
}   

public void setFlashMode (int type){

    Camera.Parameters params = mCamera.getParameters();   

    switch (type){
    case 0:
        params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);         
        break;
    case 1:
        params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);          
        break;
    case 2:
        params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);           
        break;
    case 3:
        params.setFlashMode(Camera.Parameters.FLASH_MODE_RED_EYE);          
        break;
    case 4:
        params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);            
        break;
    }

    mCamera.setParameters(params);
}   

public Camera.Size getResolution() {

    Camera.Parameters params = mCamera.getParameters(); 

    Camera.Size s = params.getPreviewSize();
    return s;
}
}

And the layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:opencv="http://schemas.android.com/apk/res-auto" >

<org.opencv.samples.test.JavaCamResView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/test_view"
    />
</LinearLayout>
click to hide/show revision 2
Edited View and Activity to show caption when a Mode is not supported

Ok, same stuff, just with JavaCameraView:

activity:

import java.util.LinkedList;
import java.util.List;
import java.util.ListIterator;

import org.opencv.android.BaseLoaderCallback;
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.CvType;
import org.opencv.core.Mat;

import android.app.Activity;
import android.hardware.Camera;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.SubMenu;
import android.view.WindowManager;
import android.widget.Toast;

public class test extends Activity implements CvCameraViewListener2 {
private static final String TAG = "test";

private JavaCamResView mOpenCvCameraView;
private List<Camera.Size> mResolutionList;

private MenuItem[] mResolutionMenuItems;
private MenuItem[] mFocusListItems;
private MenuItem[] mFlashListItems;

private SubMenu mResolutionMenu;
private SubMenu mFocusMenu;
private SubMenu mFlashMenu;



private Mat mGrayMat;

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;
        }
    }
};

public test() {
    Log.i(TAG, "Instantiated new " + this.getClass());
}

@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.java_cam_res_view);

    mOpenCvCameraView = (JavaCamResView) findViewById(R.id.test_view);
    mOpenCvCameraView.setCvCameraViewListener(this); 

}

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

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


}

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

public void onCameraViewStarted(int width, int height) {        

    mGrayMat = new Mat(height, width, CvType.CV_8UC1);

}

public void onCameraViewStopped() { 
    mGrayMat.release();
}

public Mat onCameraFrame(CvCameraViewFrame inputFrame) {

    mGrayMat=inputFrame.gray();        
    return mGrayMat;
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    List<String> mFocusList = new LinkedList<String>();
    int idx =0;

    mFocusMenu = menu.addSubMenu("Focus");

    mFocusList.add("Auto");
    mFocusList.add("Continuous Video");
    mFocusList.add("EDOF");
    mFocusList.add("Fixed");
    mFocusList.add("Infinity");
    mFocusList.add("Makro");
    mFocusList.add("Continuous Picture");

    mFocusListItems = new MenuItem[mFocusList.size()];

    ListIterator<String> FocusItr = mFocusList.listIterator();
    while(FocusItr.hasNext()){
        // add the element to the mDetectorMenu submenu
        String element = FocusItr.next();
        mFocusListItems[idx] = mFocusMenu.add(2,idx,Menu.NONE,element);
        idx++;
    }



    List<String> mFlashList = new LinkedList<String>();
    idx = 0;

    mFlashMenu = menu.addSubMenu("Flash");

    mFlashList.add("Auto");
    mFlashList.add("Off");
    mFlashList.add("On");
    mFlashList.add("Red-Eye");
    mFlashList.add("Torch");

    mFlashListItems = new MenuItem[mFlashList.size()];

    ListIterator<String> FlashItr = mFlashList.listIterator();
    while(FlashItr.hasNext()){
        // add the element to the mDetectorMenu submenu
        String element = FlashItr.next();
        mFlashListItems[idx] = mFlashMenu.add(3,idx,Menu.NONE,element);
        idx++;
    }



    mResolutionMenu = menu.addSubMenu("Resolution");
    mResolutionList = mOpenCvCameraView.getResolutionList();
    mResolutionMenuItems = new MenuItem[mResolutionList.size()];

    ListIterator<Camera.Size> resolutionItr = mResolutionList.listIterator();
    idx = 0;
    while(resolutionItr.hasNext()) {
        Camera.Size element = resolutionItr.next();
        mResolutionMenuItems[idx] = mResolutionMenu.add(1, idx, Menu.NONE,
                Integer.valueOf((int) element.width).toString() + "x" + Integer.valueOf((int) element.height).toString());
        idx++;
     }

    return true;
}

public boolean onOptionsItemSelected(MenuItem item) {
    Log.e(TAG, "called onOptionsItemSelected; selected item: " + item);
   if (item.getGroupId() == 1)
    {
        int id = item.getItemId();
        Camera.Size resolution = mResolutionList.get(id);
        mOpenCvCameraView.setResolution(resolution);
        resolution = mOpenCvCameraView.getResolution();
        Log.e("test","test");
        String caption = Integer.valueOf((int) resolution.width).toString() + "x" + Integer.valueOf((int) resolution.height).toString();
        Toast.makeText(this, caption, Toast.LENGTH_SHORT).show();
    } 
    else if (item.getGroupId()==2){

       int focusType = item.getItemId();
       String //String caption = "Focus Mode: "+ (String)item.getTitle();
       Toast.makeText(this, //Toast.makeText(this, caption, Toast.LENGTH_SHORT).show();

       mOpenCvCameraView.setFocusMode(focusType);
mOpenCvCameraView.setFocusMode(this, focusType);
    }
    else if (item.getGroupId()==3){

       int flashType = item.getItemId();
       String //String caption = "Flash Mode: "+ (String)item.getTitle();
       Toast.makeText(this, //Toast.makeText(this, caption, Toast.LENGTH_SHORT).show();

       mOpenCvCameraView.setFlashMode(flashType);
mOpenCvCameraView.setFlashMode(this, flashType);
    }

    return true;
}   
}

JavaCamResView:

import java.util.List;

import org.opencv.android.JavaCameraView;
import org.opencv.core.Size;
import org.opencv.highgui.Highgui;

import android.content.Context;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.view.MenuItem;
import android.widget.Toast;

public class JavaCamResView extends JavaCameraView {

public JavaCamResView(Context context, AttributeSet attrs) {
    super(context, attrs);
} 

public List<Camera.Size> getResolutionList() {      
    return  mCamera.getParameters().getSupportedPreviewSizes();      
}

public void setResolution(Camera.Size resolution) {
    disconnectCamera();
    connectCamera((int)resolution.width, (int)resolution.height);       
}

public void setFocusMode (int (Context item, int type){

    Camera.Parameters params = mCamera.getParameters();  

    List<String> FocusModes = params.getSupportedFocusModes();

    switch (type){
    case 0:         
0:
        if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO))
            params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO);
        else 
            Toast.makeText(item, "Auto Mode not supported", Toast.LENGTH_SHORT).show();
        break;
    case 1:         
        if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO))
            params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);
        else
            Toast.makeText(item, "Continuous Mode not supported", Toast.LENGTH_SHORT).show();
        break;
    case 2:         
        if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_EDOF))
            params.setFocusMode(Camera.Parameters.FOCUS_MODE_EDOF);
        else
            Toast.makeText(item, "EDOF Mode not supported", Toast.LENGTH_SHORT).show();
        break;
    case 3:
        if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_FIXED))
            params.setFocusMode(Camera.Parameters.FOCUS_MODE_FIXED);
        else
            Toast.makeText(item, "Fixed Mode not supported", Toast.LENGTH_SHORT).show();
        break;
    case 4:
        if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_INFINITY))
            params.setFocusMode(Camera.Parameters.FOCUS_MODE_INFINITY);
        else
            Toast.makeText(item, "Infinity Mode not supported", Toast.LENGTH_SHORT).show();
        break;
    case 5:
        if (FocusModes.contains(Camera.Parameters.FOCUS_MODE_MACRO))
            params.setFocusMode(Camera.Parameters.FOCUS_MODE_MACRO);
        else
            Toast.makeText(item, "Macro Mode not supported", Toast.LENGTH_SHORT).show();
        break;      
    case 6:
        params.setFocusMode(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE);
        break;
    }

    mCamera.setParameters(params);
}   

public void setFlashMode (int (Context item, int type){

    Camera.Parameters params = mCamera.getParameters();   
mCamera.getParameters();
    List<String> FlashModes = params.getSupportedFlashModes();

    switch (type){
    case 0:
        params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);         
if (FlashModes.contains(Camera.Parameters.FLASH_MODE_AUTO))
            params.setFlashMode(Camera.Parameters.FLASH_MODE_AUTO);
        else
            Toast.makeText(item, "Auto Mode not supported", Toast.LENGTH_SHORT).show();
        break;
    case 1:
        params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF); if (FlashModes.contains(Camera.Parameters.FLASH_MODE_OFF))
            params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        else
            Toast.makeText(item, "Off Mode not supported", Toast.LENGTH_SHORT).show();          
        break;
    case 2:
        params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);     if (FlashModes.contains(Camera.Parameters.FLASH_MODE_ON))
            params.setFlashMode(Camera.Parameters.FLASH_MODE_ON);
        else
            Toast.makeText(item, "On Mode not supported", Toast.LENGTH_SHORT).show();       
        break;
    case 3:
        params.setFlashMode(Camera.Parameters.FLASH_MODE_RED_EYE); if (FlashModes.contains(Camera.Parameters.FLASH_MODE_RED_EYE))
            params.setFlashMode(Camera.Parameters.FLASH_MODE_RED_EYE);
        else
            Toast.makeText(item, "Red Eye Mode not supported", Toast.LENGTH_SHORT).show();          
        break;
    case 4:
        params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);     if (FlashModes.contains(Camera.Parameters.FLASH_MODE_TORCH))
            params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        else
            Toast.makeText(item, "Torch Mode not supported", Toast.LENGTH_SHORT).show();        
        break;
    }

    mCamera.setParameters(params);
}   

public Camera.Size getResolution() {

    Camera.Parameters params = mCamera.getParameters(); 

    Camera.Size s = params.getPreviewSize();
    return s;
}
}

And the layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:opencv="http://schemas.android.com/apk/res-auto" >

<org.opencv.samples.test.JavaCamResView
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:id="@+id/test_view"
    />
</LinearLayout>