gpt4 book ai didi

android - LED 手电筒不适用于 Samsung Galaxy Nexus

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:52:03 24 4
gpt4 key购买 nike

我遇到了以下问题:我的手电筒应用程序在我的 Samsung Galaxy S2 上运行良好,但不幸的是在 Samsung Galaxy Nexus 上运行不正常(问题:手电筒忽略按钮单击 -> 无 react 、无光、无崩溃、无异常)。我读过“Galaxy Nexus 上的 LED 手电筒可由什么 API 控制?”在 stackoverflow 中,但它对我没有帮助,因为我的问题仍然存在。这是我控制灯光的代码 fragment :

final Button FlashLightControl = (Button)findViewById(R.id.ledbutton);
FlashLightControl.setOnClickListener(new Button.OnClickListener()
{
public void onClick(View arg)
{
if(camera != null)
{
//in case light is on we will turn it off
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
camera.stopPreview();
camera.release();
camera = null;
}
else
{
// light is off - we turn it on
camera = Camera.open();
parameters = camera.getParameters();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameters);
camera.startPreview();
}
}});

有什么想法吗?为了完整起见 - 这些是我添加到 Androidmanifest.xml 的权限:

    <uses-feature android:name="android.hardware.camera.flash" />
<uses-sdk android:minSdkVersion="7" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-permission android:name="android.permission.WAKE_LOCK"/>

有人可以帮忙吗?

亲切的问候,及时行乐

最佳答案

我也遇到了同样的问题,但我试图通过服务打开 LED,所以我无法使用 1x1 SurfaceView。这是我为让它发挥作用所做的工作。

private void turnLEDOn() throws IOException
{
// In order to work, the camera needs a surface to turn on.
// Here I pass it a dummy Surface Texture to make it happy.
camera = Camera.open();
camera.setPreviewTexture(new SurfaceTexture(0));
camera.startPreview();
Parameters p = camera.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(p);
}

private void turnLEDOff()
{
if (camera != null)
{
// Stopping the camera is enough to turn off the LED
camera.stopPreview();
camera.release();
camera = null;
} else
throw new NullPointerException("Camera doesn't exist to turn off.");

}

SurfaceTexture 是在 API 级别 11 (Android 3.0) 中添加的,因此它只能在 Honeycomb 或更新版本上工作。对于较旧的 API 级别,您可以坚持使用其他答案中的 SurfaceView 技巧。

关于android - LED 手电筒不适用于 Samsung Galaxy Nexus,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9505945/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com