Android – flash torch on Google nexus 5

After reading all the posts of other users and encountering the same problem, I can create a simple working application to turn on the flash 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();
    }
}

I added these permissions to the list:

<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 in a more complex application, and it also uses cameras. How do I add it as a method / class or other content to make it work without conflict?

thank you

resolvent:

Finish editing:

According to your opinion, I understand that you want to use flashlight flash mode when using the camera. This is possible. Here are some pure proof of concept code. Please note that it implements very small exception handling and needs some adjustments to meet your needs, but it will show the basics of getting started

In 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 application starts, you need to click init to start the camera preview. After that, you can use the toggle button to open and close torch, and then click picture to take pictures that will be saved to the root directory of the external storage directory. You should click finish before exiting or minimizing the application

Editing: minimum

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 event:

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

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>