4

after reading all the posts of the other users with the same problem I was able to create a simple working app for turning on flash light on my Nexus 5, this is the "OnCreate()" method:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Camera mCamera;
    SurfaceView preview;
    mCamera = Camera.open();
    Parameters params = mCamera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_TORCH);
    mCamera.setParameters(params);  
    mCamera.startPreview();
    try {
        mCamera.setPreviewTexture(new SurfaceTexture(0));
        } catch (IOException e) {
        e.printStackTrace();
        }

    if (savedInstanceState == null) {
        getSupportFragmentManager().beginTransaction()
                .add(R.id.container, new PlaceholderFragment()).commit();
    }
}

While inside the manifest I added these permissions:

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

I need to put it inside a more complicated application which also uses camera. How can I add it as a method/class or someting else in order that it will works without conflicts ?

Thanks

divibisan
  • 11,659
  • 11
  • 40
  • 58
user3455333
  • 43
  • 1
  • 4

2 Answers2

2

I just fixed my flashlight code for Nexus 5, thought I'd share: https://github.com/EddyVerbruggen/Flashlight-PhoneGap-Plugin/issues/7

In a Cordova context, the easiest fix was setting setPreviewTexture(new SurfaceTexture(0)) on the Camera class.

Eddy Verbruggen
  • 3,550
  • 1
  • 15
  • 27
1

Complete Edit:

Per your comments, I understand that you wish to use the Torch Flash Mode while using the Camera. This is possible, and below is some purely proof-of-concept code. Please note that it implements very minimal exception handling, and will need some adjustment to work as you need it to, but it will demonstrate the basics to get you started.

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center"
    android:orientation="vertical" >

    <android.view.SurfaceView android:id="@+id/surfaceView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="initialize"
            android:text="Init" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="takePicture"
            android:text="Picture" />

        <Button
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:onClick="release"
            android:text="Done" />

        <ToggleButton android:id="@+id/tgbToggle"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:layout_weight="1" />

    </LinearLayout>

</LinearLayout>

MainActivity.java

public class MainActivity extends Activity
{
    Camera camera;
    Parameters params;
    SurfaceView surfaceView;
    SurfaceHolder surfaceHolder;
    PictureCallback callBackJpeg;

    Button start, stop, capture;
    ToggleButton toggle;

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

        toggle = (ToggleButton) findViewById(R.id.tgbToggle);
        toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton button, boolean checked)
                {
                    toggleTorch(checked);
                }           
            }
        );

        surfaceView = (SurfaceView)findViewById(R.id.surfaceView);
        surfaceHolder = surfaceView.getHolder();
        surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);

        callBackJpeg = new PictureCallback() {
            public void onPictureTaken(byte[] data, Camera camera)
            {
                FileOutputStream fos = null;
                String filename = String.format(Environment.getExternalStorageDirectory().toString() +
                                                "/%d.jpg", System.currentTimeMillis());
                try
                {
                    fos = new FileOutputStream(filename);
                    fos.write(data);
                    fos.close();

                    Toast.makeText(MainActivity.this, "Picture saved - " + filename, 0).show();
                }
                catch (FileNotFoundException e)
                {
                    e.printStackTrace();
                }
                catch (IOException e)
                {
                    e.printStackTrace();
                }
            }
        };
    }

    public void initialize(View v)
    {
        camera = Camera.open();
        params = camera.getParameters();
        try
        {
            camera.setPreviewDisplay(surfaceHolder);
        }
        catch (IOException e)
        {
            Toast.makeText(this, "Unable to set preview display.", 0).show();
            return;
        }       
        camera.startPreview();
    }

    public void takePicture(View v)
    {
        camera.takePicture(null, null, callBackJpeg);
    }

    public void release(View v)
    {
        camera.stopPreview();
        camera.release();
    }

    private void toggleTorch(boolean turnOn)
    {
        params.setFlashMode(turnOn ? Parameters.FLASH_MODE_TORCH : Parameters.FLASH_MODE_OFF);
        camera.setParameters(params);
        camera.startPreview();
    }
}

When the app starts, you will want to click Init to start the Camera preview. After this, you can turn the Torch on and off with the Toggle Button, and click Picture to take a picture that will be saved to the root of your External Storage Directory. You should click Done before exiting or minimizing the app.

Edit: Minimal

Add the following permissions to the manifest:

<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.FLASHLIGHT" />

Add the following to your Activity:

Camera camera;
Parameters params;

@Override
public void onResume()
{
    super.onResume();
    camera = Camera.open();
    params = camera.getParameters();
    params.setFlashMode(Parameters.FLASH_MODE_TORCH);
    camera.setParameters(params);
    camera.startPreview();
}

@Override
public void onPause()
{
    super.onPause();
    camera.release();
}
Mike M.
  • 38,532
  • 8
  • 99
  • 95
  • @ Mike M., Thank you for your answer !! I'm going to try to implement your code inside my application...but before of this I have a question: In my app I need to use the flash light and the camera both on at the same time...it is possible to do that with your code ? Thanks – user3455333 Mar 25 '14 at 20:51
  • Ok, give the new code a shot. Lemme know if you've any problems. – Mike M. Mar 26 '14 at 05:12
  • I'll try to include your code in my app and I'll let you know! Thanks a lot – user3455333 Mar 26 '14 at 06:55
  • I'm still not able to integrate the essentials parts of your code in my app...please can you tell me what is really necessary in order that the code could work ? What if I make your app as the firt activity and my app the second activity of a unique app ? Do you think that the flash will be still On ? The last question...it is possible to avoid the camera to turn on ? Many thanks – user3455333 Mar 31 '14 at 15:30
  • I'm not following. What's your end goal? I thought you wanted to be able to toggle the torch and use the camera simultaneously. If not, and you just want to access the torch separately, you can use the separate static-method class I posted originally. It's in the edit history for my answer. – Mike M. Mar 31 '14 at 21:05
  • Well, I will shortly explain you what my app do and what I miss. Basically, when the app starts running also the camera goes on...and I need to have also the flash light on! The code works correctly on other devices, but on Nexus 5 the flash light doesn't turn on. so I would like to understand what must be added in my code which is necessary for the Nexus 5 to work. – user3455333 Apr 01 '14 at 11:10
  • Hmm, I can't be certain. If the code you posted is able to turn on the Nexus 5 torch, I would think mine would as well. Did my original static-method test app work on the Nexus? – Mike M. Apr 01 '14 at 19:28
  • Both codes works but as they are...meaning that when I try to put them inside in my app the flash doesn't turn on. So, my question is...which lines of your code are strictly necessary just for turning on the torch ? I mean that I don't need buttons and so on (I already tried to modify the code in this way but with no results)...I just need few lines that when I launch the app, the flash turns on. – user3455333 Apr 02 '14 at 07:53
  • I tried to create a new project and to put your code in the main activity just to see what appen, but the flash doesn't go on. Is it normal ? – user3455333 Apr 04 '14 at 14:03
  • As far as the Nexus 5 is concerned, I don't know. All I can tell you is if the code you posted works, start with that and make small changes until it doesn't. Then you'll know what the problem is. I know at least one other person is having trouble getting the torch to work on the Nexus 5. I don't have one to test on. – Mike M. Apr 04 '14 at 19:08
  • Ok, I will put anyway your code as working. Really thanks a lot ! – user3455333 Apr 04 '14 at 19:50
  • No sweat. As I said, you're not the only one having trouble with the Nexus 5, so you might keep checking SO periodically. Someone may eventually figure out what the catch is with that model. Or, ya know, you could just ship your Nexus to me. I'll get it goin'! ;-) – Mike M. Apr 04 '14 at 19:56
  • Ahahaha...sure, I've just shipped it to you ! Go out and start to wait for it...it will come ! don't worry about the rain ! :D – user3455333 Apr 05 '14 at 10:56