Google Nexus 5上的Flash Torch

时间:2014-03-25 09:35:45

标签: android camera flashlight nexus-5

在阅读其他用户的所有帖子后遇到同样的问题我能够创建一个简单的工作应用程序来打开我的Nexus 5上的闪光灯,这就是" OnCreate()"方法:

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

在清单中我添加了这些权限:

<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" />

我需要把它放在一个更复杂的应用程序中,它也使用相机。如何将其添加为方法/类或其他内容以使其无冲突地工作?

谢谢

2 个答案:

答案 0 :(得分:2)

我刚刚修改了Nexus 5的手电筒代码,我想分享一下:https://github.com/EddyVerbruggen/Flashlight-PhoneGap-Plugin/issues/7

在Cordova上下文中,最简单的修复是在Camera类上设置setPreviewTexture(new SurfaceTexture(0))。

答案 1 :(得分:1)

完成编辑:

根据您的评论,我了解您希望在使用相机时使用手电筒闪光模式。这是可能的,下面是一些纯粹的概念验证代码。请注意,它实现了非常小的异常处理,并且需要进行一些调整才能满足您的需要,但它将演示基础知识以帮助您入门。

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

当应用启动时,您需要点击Init以启动相机预览。在此之后,您可以使用切换按钮打开和关闭Torch,然后单击Picture以拍摄将保存到外部存储目录根目录的图片。在退出或最小化应用之前,您应该点击Done

编辑:最小

向清单添加以下权限:

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

将以下内容添加到您的活动中:

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