How do I turn on the camera flash?

3

I can not activate the camera flash to use as a flashlight. The code I'm running has taken from tutorials on the internet.

package com.getten.home;

import android.hardware.Camera;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;

public class Login extends ActionBarActivity {

    private Toolbar pToolbar;

    private Camera mCamera;
    private boolean lFlashLigado = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.login);

        pToolbar = (Toolbar) findViewById(R.id.toolbar);
        pToolbar.setLogo(R.drawable.ic_launcher);
        setSupportActionBar(pToolbar);

        try {
            mCamera = Camera.open();
        } catch (Exception ex) {
            Log.e("Camera", "Impossível ouvir a câmera!");
        }
    }

    @Override
    protected void onPause() {
        if (mCamera != null) {
            mCamera.release();
            mCamera = null;
        }
        super.onPause();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == R.id.lanterna) {
            ligaDesligaFlash(lFlashLigado);
        }

        return true;
    }

    private void ligaDesligaFlash(boolean lDesliga) {
        if (lDesliga) {
            if (mCamera != null) {
                Camera.Parameters params = mCamera.getParameters();
                params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
                mCamera.setParameters(params);
                mCamera.startPreview();

                lFlashLigado = false;
            }
        } else {
            if (mCamera != null) {
                Camera.Parameters params = mCamera.getParameters();
                params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
                mCamera.setParameters(params);
                mCamera.startPreview();

                lFlashLigado = true;
            }
        }
    }
}

I am compiling the application in version "22". I have already added all that is allowed, but I have not been successful either:

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

Would anyone know how to solve it, or some other way?

    
asked by anonymous 01.09.2015 / 01:50

2 answers

1

First you should change your AndroidManifest.xml and add the permissions:

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

After this in the onClick event of a button, make the following code:

private Camera mCamera;

public void ligarLed(View v) {
    mCamera = Camera.open();
    if( mCamera != null ){
            Parameters params = mCamera.getParameters();
            params.setFlashMode( Parameters.FLASH_MODE_TORCH );
            mCamera.setParameters( params );
     }
}

What changes to Turn On or Turn Off Flash is the enumerator

Parameters.FLASH_MODE_TORCH
Parameters.FLASH_MODE_OFF 

Do not forget that after using Flash you should release the camera with the following code:

mCamera.release();
mCamera = null;
    
11.09.2015 / 21:29
1

First of all:

<uses-sdk android:minsdkversion="11" android:targetsdkversion="17">

Basically you need:

//Ligar camera
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
camera.startPreview();  

//DesLigar camera
camera = Camera.open();
Parameters p = camera.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(p);
camera.stopPreview();

The problem may lie in your condition. Try to use the OnClickListener event:

    ....
    //Detectando se o flash está ligado, inicialmente sim
    private boolean isLighOn = false;

    private Camera camera;

    private Button button;

    @Override
    protected void onStop() {
        super.onStop();

        if (camera != null) {
            camera.release();
        }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        button = (Button) findViewById(R.id.botaoFlash);

        Context context = this;
        PackageManager pm = context.getPackageManager();

        // O mais importante: O aparelho possui configuração para câmera?
        if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
            Log.e("err", "Aparelho não suporta camera!");
            return;
        }

        camera = Camera.open();
        final Parameters p = camera.getParameters();

        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                if (isLighOn) {

                    Log.i("informação", "Desligado!");

                    p.setFlashMode(Parameters.FLASH_MODE_OFF);
                    camera.setParameters(p);
                    camera.stopPreview();
                    isLighOn = false;

                } else {

                    Log.i("informação", "ligado!");

                    p.setFlashMode(Parameters.FLASH_MODE_TORCH);

                    camera.setParameters(p);
                    camera.startPreview();
                    isLighOn = true;

                }

            }
        });

    }
}
    
01.09.2015 / 03:21