闪光火炬在谷歌Nexus 5 [英] Flash Torch on Google Nexus 5

查看:204
本文介绍了闪光火炬在谷歌Nexus 5的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 ?

感谢

推荐答案

完成编辑:

每您的意见,我知道你想,而的使用相机使用的手电筒闪光模式的。这是可能的,而且下面是一些纯粹的证明了概念code。请注意,它实现了极少的例外处理,并且需要进行一些调整工作,因为你需要它,但它会表现出基础知识让你开始。

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

在应用程序启动时,你会想点击初始化启动相机preVIEW。在此之后,你可以打开和关闭与切换按钮的火炬,然后点击图片来采取将被保存到外部存储目录的根目录中的图片。你应该点击完成之前退出或最小化应用程序。

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.

编辑:最小

添加以下权限清单:

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

这篇关于闪光火炬在谷歌Nexus 5的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆