Ask Your Question
0

OpenCV + Android - implement flashlight

asked 2017-03-05 05:22:25 -0600

KicoSVK gravatar image

updated 2017-03-05 16:52:06 -0600

Hello,

I have a problem with flashlight in my app. I do not know, How can I implement it to my Android app. On frame processing I use a CameraBridgeViewBase and I use a rear mobile camera.

How Can is implement simple camera flashlight ? :O Here is my code. Thanks EVERYONE !

public class MainActivity extends Activity implements CvCameraViewListener2 {

private CameraBridgeViewBase mOpenCvCameraView;
private JavaCameraView cameraView;

private Mat
        mRgba,
        imgGray,
        imgCanny,
        mResizedMat,
        hierarchy;

private TextView
        infoText,
        text01,
        text02,
        text03;

private Button
        button01,
        button02,
        button03;

private boolean
        isMode01Active = false,
        isMode02Active = false,
        isMode03Active = false;

private SeekBar
        seekBar01,
        seekBar02,
        seekBar03;

private int
        value01 = 64,
        value02 = 80,
        value03 = 5;

Camera camera;
Camera.Parameters parameters;

List<MatOfPoint> contours;

private BaseLoaderCallback mLoaderCallback = new BaseLoaderCallback(this) {
    @Override
    public void onManagerConnected(int status) {
        switch (status) {
            case LoaderCallbackInterface.SUCCESS: {
                camera = Camera.open(CAMERA_ID_BACK);
                parameters = camera.getParameters();
                parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                camera.setParameters(parameters);
                mOpenCvCameraView.enableView();
                mOpenCvCameraView.enableFpsMeter();
            }
            break;
            default: {
                super.onManagerConnected(status);
            }
            break;
        }
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // - - - > SETTING LAYOUT OF ACTIVITY
    setContentView(R.layout.activity_main);
    setRequestedOrientation(SCREEN_ORIENTATION_LANDSCAPE);
    // < - - - END

    // - - - > SET ORIENTATION OF SCREEN
    getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
            WindowManager.LayoutParams.FLAG_FULLSCREEN);
    // < - - - END

    // - - - > DECLARATION OF COMPONENTS
    infoText = (TextView)findViewById(R.id.infoText);
    text01 = (TextView)findViewById(R.id.text01);
    text02 = (TextView)findViewById(R.id.text02);
    text03 = (TextView)findViewById(R.id.text03);

    mOpenCvCameraView = (CameraBridgeViewBase) findViewById(R.id.camera_view);
    mOpenCvCameraView.setVisibility(SurfaceView.VISIBLE);
    mOpenCvCameraView.setCvCameraViewListener(this);

    button01 = (Button) findViewById(R.id.button01);
    button02 = (Button) findViewById(R.id.button02);
    button03 = (Button) findViewById(R.id.button03);

    seekBar01 = (SeekBar) findViewById(R.id.seekBar01);
    seekBar02 = (SeekBar) findViewById(R.id.seekBar02);
    seekBar03 = (SeekBar) findViewById(R.id.seekBar03);
    // < - - - END

    isMode01Active = true;
    infoText.setText("MODE1: ACTIVE");

    button01.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mOpenCvCameraView.enableFpsMeter();
            infoText.setText("MODE1: ACTIVE");
            setModelStatus("mode1");
        }
    });

    button02.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mOpenCvCameraView.enableFpsMeter();
            infoText.setText("MODE2: ACTIVE");
            setModelStatus("mode2");
        }
    });

    button03.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            mOpenCvCameraView.enableFpsMeter();
            infoText.setText("MODE3: ACTIVE");
            setModelStatus("mode3");
        }
    });

    seekBar01.setMax(150);
    seekBar02.setMax(150);
    seekBar03.setMax(45);
    seekBar01.setProgress(value01);
    seekBar02.setProgress(value02);
    seekBar03.setProgress(value03);
    text01.setText(""+value01);
    text02.setText(""+value02);
    text03.setText(""+value03);


    seekBar01.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            value01 = i;
            text01.setText(""+value01);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    seekBar02.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            value02 = i;
            text02.setText(""+value02);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });

    seekBar03.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
        @Override
        public void onProgressChanged(SeekBar seekBar, int i, boolean b) {

            if(i % 2 != 0)
            value03 = i;
            text03.setText(""+value03);
        }

        @Override
        public void onStartTrackingTouch(SeekBar seekBar) {

        }

        @Override
        public void onStopTrackingTouch(SeekBar seekBar) {

        }
    });
}

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

@Override
public void onResume() {
    super.onResume();
    if (!OpenCVLoader.initDebug()) {
        OpenCVLoader.initAsync(OpenCVLoader.OPENCV_VERSION_3_1_0, this, mLoaderCallback);
    } else {
        mLoaderCallback.onManagerConnected(LoaderCallbackInterface.SUCCESS);
    }
}

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

@Override
public void onCameraViewStarted(int ...
(more)
edit retag flag offensive close merge delete

Comments

there is no flashlight function in opencv.

LBerger gravatar imageLBerger ( 2017-03-05 10:01:58 -0600 )edit

Hmmm, thanks for answer. So, How can I implement it, is there any simply solution ?

KicoSVK gravatar imageKicoSVK ( 2017-03-05 15:06:25 -0600 )edit

Finally I have found this CV_CAP_PROP_ANDROID_FLASH_MODE. May it will be solved your problem

All specific android properties are here You can find some samples here

LBerger gravatar imageLBerger ( 2017-03-05 15:36:43 -0600 )edit

Thanks a lot for your time, but there is a no way how to implement it.

KicoSVK gravatar imageKicoSVK ( 2017-03-05 15:58:12 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2017-03-07 07:18:52 -0600

KicoSVK gravatar image

Solution is use a JavaCameraView ! In class JavaCameraView insert this functions with this body.

    public void turnOffTheFlash() {
       Camera.Parameters params = mCamera.getParameters();
       params.setFlashMode(params.FLASH_MODE_OFF);
       mCamera.setParameters(params);
    }

    public void turnOnTheFlash() {
       Camera.Parameters params = mCamera.getParameters();
       params.setFlashMode(params.FLASH_MODE_TORCH);
       mCamera.setParameters(params);
    }

Then in your activity class create instance of JavaCameraView class, for example private JavaCameraView mOpenCvCameraView;. Finally with this instance call these functions like this.

mOpenCvCameraView.turnOnTheFlash();

or

mOpenCvCameraView.turnOffTheFlash();
edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2017-03-05 05:22:25 -0600

Seen: 2,119 times

Last updated: Mar 07 '17