gpt4 book ai didi

安卓:LED 不打开/关闭

转载 作者:太空宇宙 更新时间:2023-11-03 11:16:50 26 4
gpt4 key购买 nike

我正在尝试编写一个带有按钮的 Android 小部件,该按钮可以打开/关闭相机手电筒。我知道,那里有成千上万个,但我想学习 Android(而且小步骤似乎是最好的方法)。

现在我已经阅读了官方文档,网络上的一些免费教程,并在 stackoverflow 上搜索了它。到目前为止我没有收到任何错误,LogCat 说一切都按预期工作。但是,当我在我的 Galaxy Nexus 上测试该应用程序时,开关会正常打开/关闭,但摄像头 LED 不会打开/关闭。

这是我的代码(只有 LED 实际打开/关闭的部分):

if (isLightOn) {
Log.d("receiver", "flashlight is on, disabling it");
if (camera != null) {
param.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(param);
camera.release();
camera = null;
isLightOn = false;
}
} else {
Log.d("receiver", "flashlight is off, enabling it");
camera = Camera.open();

if(camera == null) {
Toast.makeText(context, R.string.no_camera, Toast.LENGTH_SHORT).show();
} else {
// Set the torch flash mode
param.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
try {
camera.setParameters(param);
isLightOn = true;
} catch (Exception e) {
Toast.makeText(context, R.string.no_flash, Toast.LENGTH_SHORT).show();
}
}
}

知道为什么这不能按预期工作吗?

最佳答案

大体看看here .解释正确。

编辑:

链接上的内容:

  1. 开启

    相机 = Camera.open();参数 p = camera.getParameters();p.setFlashMode(Parameters.FLASH_MODE_TORCH);camera.setParameters(p);camera.startPreview();

  2. 关闭

    相机 = Camera.open();参数 p = camera.getParameters();p.setFlashMode(Parameters.FLASH_MODE_OFF);camera.setParameters(p);camera.stopPreview();

并且,在 AndroidManifest.xml 上设置以下权限。

P.S 该项目是在 Eclipse 3.7 中开发的,并使用 Samsung Galaxy S2 (Android 2.3.3) 进行了测试。

  1. Android 布局只有一个按钮。

文件:res/layout/main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
android:id="@+id/buttonFlashlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="Torch" />

</RelativeLayout>
  1. Activity 阅读代码,一个打开/关闭手电筒的按钮,应该是不言自明的。

    package com.mkyong.android;

    import android.app.Activity;
    import android.content.Context;
    import android.content.pm.PackageManager;
    import android.hardware.Camera;
    import android.hardware.Camera.Parameters;
    import android.os.Bundle;
    import android.util.Log;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.widget.Button;

    public class FlashLightActivity extends Activity {

    //flag to detect flash is on or off
    private boolean isLighOn = false;

    private Camera camera;

    private Button button;

    @Override
    protected void onStop() {
    super.onStop();

    if (camera != null) {
    camera.release();
    }
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    button = (Button) findViewById(R.id.buttonFlashlight);

    Context context = this;
    PackageManager pm = context.getPackageManager();

    // if device support camera?
    if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {
    Log.e("err", "Device has no camera!");
    return;
    }

    camera = Camera.open();
    final Parameters p = camera.getParameters();

    button.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View arg0) {

    if (isLighOn) {

    Log.i("info", "torch is turn off!");

    p.setFlashMode(Parameters.FLASH_MODE_OFF);
    camera.setParameters(p);
    camera.stopPreview();
    isLighOn = false;

    } else {

    Log.i("info", "torch is turn on!");

    p.setFlashMode(Parameters.FLASH_MODE_TORCH);

    camera.setParameters(p);
    camera.startPreview();
    isLighOn = true;

    }

    }
    });

    }
    }
  2. 安卓权限分配 CAMERA 权限。

文件:AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mkyong.android"
android:versionCode="1"
android:versionName="1.0" >

<uses-sdk android:minSdkVersion="10" />

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

<application
android:debuggable="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:label="@string/app_name"
android:name=".FlashLightActivity" >
<intent-filter >
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

关于安卓:LED 不打开/关闭,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15413053/

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