-2

I want to record a video with a flash light to be on to make focus on the capture.

fadden
  • 51,356
  • 5
  • 116
  • 166
PatidarNikunj
  • 110
  • 12

1 Answers1

2

Add this to your manifest

  <uses-permission android:name="android.permission.CAMERA" />
    <uses-feature android:name="android.hardware.camera" />

Check if device has camera

  boolean   hasFlash = getApplicationContext().getPackageManager()
                    .hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
  Camera camera = Camera.open();
  Parameters params = camera.getParameters();

to turn on the flash

  params = camera.getParameters();
  params.setFlashMode(Parameters.FLASH_MODE_TORCH);
  camera.setParameters(params);
  camera.startPreview();

to turn off the flash

params = camera.getParameters();
params.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();

in that case you can use SurfaceView add this to your xml

<SurfaceView
        android:layout_height="match_parent"
        android:layout_width="match_parent"
        android:id="@+id/surface"
        android:layout_weight="2"/>

then in activity

surfaceview=(SurfaceView)findViewById(R.id.surface);
surfaceholder=surfaceview.getHolder();
surfaceholder.addCallback(this);
surfaceholder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

@Override
    public void surfaceCreated(SurfaceHolder surfaceHolder) {
        try {
            camera = Camera.open();
            android.hardware.Camera.Parameters params = camera.getParameters();
            params = camera.getParameters();
            params.setFlashMode(android.hardware.Camera.Parameters.FLASH_MODE_TORCH);
            camera.setParameters(params);
            camera.setPreviewDisplay(surfaceholder);

            camera.startPreview();

            // Toast.makeText(getApplication(), "Create", Toast.LENGTH_LONG).show();
        } catch (IOException e) { }
    }
Nilesh Deokar
  • 2,975
  • 30
  • 53