Flash Torch on Google Nexus 5

北慕城南 提交于 2019-12-21 19:56:15

问题


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


回答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();
}



回答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.



来源:https://stackoverflow.com/questions/22630453/flash-torch-on-google-nexus-5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!