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();
//camera = Camera.open();
parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
//camera.startPreview();
//mOpenCvCameraView.setCameraIndex(CAMERA_ID_BACK);
//mOpenCvCameraView.setCameraIndex(CAMERA_ID_BACK);
mOpenCvCameraView.enableView();
mOpenCvCameraView.enableFpsMeter();
}
break;
default: {
super.onManagerConnected(status);
}
break;
}
}
};
/*static {
if (!OpenCVLoader.initDebug()) {
} else {
//System.loadLibrary("opencv_java3");
//System.loadLibrary("MyFaceLibs");
//System.loadLibrary("opencv_java3");
}
}*/
@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 width, int height) {
mRgba = new Mat();
imgGray = new Mat();
imgCanny = new Mat();
hierarchy = new Mat();
mResizedMat = new Mat();
}
@Override
public void onCameraViewStopped() {
mRgba.release();
imgGray.release();
imgCanny.release();
hierarchy.release();
mResizedMat.release();
}
@Override
public Mat onCameraFrame(CameraBridgeViewBase.CvCameraViewFrame inputFrame) {
contours = new ArrayList<MatOfPoint>();
int rows = (int) mRgba.height();
int cols = (int) mRgba.width();
int left = cols / 8;
int top = rows / 8;
int width = cols * 3 / 4;
int height = rows * 3 / 4;
int dWidth = (int) mRgba.height();
int dHeight = (int) mRgba.width();
Point p3 = new Point();
p3.x = 150;
p3.y = 150;
Point p4 = new Point();
p3.x = 640;
p3.y = 480;
Mat rgbaInnerWindow;
if (isMode01Active == true) {
mRgba = inputFrame.rgba();
imgGray = inputFrame.gray();
return mRgba;
}
else if (isMode02Active == true) {
mRgba = inputFrame.rgba();
imgGray = inputFrame.gray();
Imgproc.GaussianBlur(imgGray, imgGray, new Size(value03, value03), 0);
Imgproc.Canny(imgGray, imgCanny, value01, value02);
return imgCanny;
}
else if (isMode03Active == true){
mRgba = inputFrame.rgba();
imgGray = inputFrame.gray();
hierarchy = new Mat();
contours = new ArrayList<MatOfPoint>();
Point c1 = new Point();
Point c2 = new Point();
int numberOfObject = 0;
double approxDistance;
MatOfPoint points;
MatOfPoint2f approxCurve, contour2f;
Imgproc.GaussianBlur(imgGray, imgGray, new Size(value03, value03), 0);
Imgproc.Canny(imgGray, imgCanny, value01, value02);
Imgproc.findContours(imgCanny, contours, hierarchy, Imgproc.RETR_EXTERNAL, Imgproc.CHAIN_APPROX_SIMPLE);
for ( int contourIdx=0; contourIdx < contours.size(); contourIdx++ ) {
// Minimum size allowed for consideration
// - - - > APROXIMATION OF CONTOURS
approxCurve = new MatOfPoint2f();
contour2f = new MatOfPoint2f(contours.get(contourIdx).toArray());
approxDistance = Imgproc.arcLength(contour2f, true)*Double.MIN_VALUE;
Imgproc.approxPolyDP(contour2f, approxCurve, approxDistance, true);
points = new MatOfPoint( approxCurve.toArray() );
// < - - - END
Rect rect = Imgproc.boundingRect(points);
c1.x = rect.x;
c1.y = rect.y;
c2.x = rect.x + rect.width;
c2.y = rect.y + rect.height;
Mat mContourArea = contours.get(contourIdx);
double contourArea = Imgproc.contourArea(mContourArea);
if((contourArea > 200 && contourArea < 10000)){
numberOfObject++;
Imgproc.rectangle(mRgba, c1, c2, new Scalar(0, 255, 255), 3);
//Imgproc.putText(mRgba, "Object " + String.valueOf(numberOfObject), new Point(rect.x+rect.width/2, rect.y-10), FONT_HERSHEY_PLAIN, 3, new Scalar(0,255,255), 5);
}
}
return mRgba;
}
else {
return mRgba;
}
}
public void setModelStatus(String status){
isMode01Active = false;
isMode02Active = false;
isMode03Active = false;
if(status == "mode1")
isMode01Active = true;
if(status == "mode2")
isMode02Active = true;
if(status == "mode3")
isMode03Active = true;
}
}