火炬应用程序安卓

发布于 2024-11-02 16:19:12 字数 1550 浏览 3 评论 0原文

你好,我想在 android 中实现 torch 应用程序,所以这是我的代码

    setContentView(R.layout.main);
    camera = Camera.open();
    if(camera == null)
        Toast.makeText(getBaseContext(),"CAMERA FAILED", Toast.LENGTH_LONG).show();
    else
        Toast.makeText(getBaseContext(),"camera opened",Toast.LENGTH_LONG).show();
}
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_UP) {
                if(camera!=null)
                {
                     Toast.makeText(getBaseContext(),"came to volume up",Toast.LENGTH_LONG).show();
                     Parameters params = camera.getParameters();
                     params.setFlashMode( Parameters.FLASH_MODE_ON );
                     camera.setParameters(params);

                }
            }
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (action == KeyEvent.ACTION_UP) {
                Parameters params = camera.getParameters();
                params.setFlashMode( Parameters.FLASH_MODE_OFF );
                camera.setParameters(params);
                camera.release();
                camera = null;
                Toast.makeText(getBaseContext(),"came to volume down",Toast.LENGTH_LONG).show();
            }
            return true;
        default:
            return super.dispatchKeyEvent(event);
        }
    }

}

为什么这个代码不起作用???

hi i want to implement torch application in android so here is my code

    setContentView(R.layout.main);
    camera = Camera.open();
    if(camera == null)
        Toast.makeText(getBaseContext(),"CAMERA FAILED", Toast.LENGTH_LONG).show();
    else
        Toast.makeText(getBaseContext(),"camera opened",Toast.LENGTH_LONG).show();
}
public boolean dispatchKeyEvent(KeyEvent event) {
    int action = event.getAction();
    int keyCode = event.getKeyCode();
        switch (keyCode) {
        case KeyEvent.KEYCODE_VOLUME_UP:
            if (action == KeyEvent.ACTION_UP) {
                if(camera!=null)
                {
                     Toast.makeText(getBaseContext(),"came to volume up",Toast.LENGTH_LONG).show();
                     Parameters params = camera.getParameters();
                     params.setFlashMode( Parameters.FLASH_MODE_ON );
                     camera.setParameters(params);

                }
            }
            return true;
        case KeyEvent.KEYCODE_VOLUME_DOWN:
            if (action == KeyEvent.ACTION_UP) {
                Parameters params = camera.getParameters();
                params.setFlashMode( Parameters.FLASH_MODE_OFF );
                camera.setParameters(params);
                camera.release();
                camera = null;
                Toast.makeText(getBaseContext(),"came to volume down",Toast.LENGTH_LONG).show();
            }
            return true;
        default:
            return super.dispatchKeyEvent(event);
        }
    }

}

why this code is not working???

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

也只是曾经 2024-11-09 16:19:12

杜尔加,

我相信你想要FLASH_MODE_TORCH< /a> 不是 FLASH_MODE_ON 当您按音量增大键时。

也许可能需要以下权限:FLASHLIGHT< /a> 和 CAMERA

Durga,

I believe you want FLASH_MODE_TORCH not FLASH_MODE_ON when you press the volume up key.

Perhaps the following permissions may be required: FLASHLIGHT and CAMERA

带刺的爱情 2024-11-09 16:19:12

对于三星,您需要有相机预览的表面视图才能使手电筒模式发挥作用。

你让它工作了吗?

For Samsung you're required to have a surface view for the camera preview inorder for the torch mode to work.

DID you get it to work yet?

舟遥客 2024-11-09 16:19:12

有一个扩展 SurfaceView 并实现 SurfaceView.Callback 的类。在构造函数中,向持有者添加回调并将其类型设置为 PUSH_BUFFERS。

对于此类,在 surfaceCreated 方法中初始化您的 Camera 对象并在其中设置您的相机参数。

在surfaceDestroy中,释放相机对象。

此外,实施其他方法来确定三星设备是否有 LED 以及设备 LED 是否亮起。

在您的活动中,声明您的表面视图对象,将其添加到您的布局中,然后设置ContentView。

Have a class that extends SurfaceView and implements SurfaceView.Callback. In teh constructor, add a callback to the holder and set its type to PUSH_BUFFERS.

For this class, initialize your Camera object in the surfaceCreated method and set your camera parameters there.

In the surfaceDestroy, release camera object.

In addition, implement additional methods to determine if Samsung device have LED and device LED is lit.

In your activity, declare your surface view object add it to your layout, then setContentView.

原来分手还会想你 2024-11-09 16:19:12

William Tate 说得对,您需要 FLASH_MODE_TORCH 以及这两个权限,但在您开始视频预览之前,相机硬件不会被触及。

这段代码将打开火炬(尽管您可能想在实际应用程序中进行一些错误捕获):

Camera c = Camera.open();
Camera.Parameters p = c.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
c.setParameters(p);
c.startPreview();

并且格式正确的权限在您的 Android 清单中将如下所示:

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

William Tate was correct saying that you need FLASH_MODE_TORCH along with the two permissions, but the camera hardware doesn't get touched until you start a video preview.

This code will turn on the torch (though you may want to do some error catching in your actual app):

Camera c = Camera.open();
Camera.Parameters p = c.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
c.setParameters(p);
c.startPreview();

And the properly formatted permissions will look like this in your android manifest:

<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.FLASHLIGHT"/>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文