- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我正在尝试编写一个带有按钮的 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 .解释正确。
编辑:
链接上的内容:
开启
相机 = Camera.open();参数 p = camera.getParameters();p.setFlashMode(Parameters.FLASH_MODE_TORCH);camera.setParameters(p);camera.startPreview();
关闭
相机 = 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) 进行了测试。
文件: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>
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;
}
}
});
}
}
安卓权限分配 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/
嘿,我 wounderd 如果有什么。我可以继续阅读以改进此代码的突击队吗?至少它现在可以工作,但需要做一些微调:) 如您所见,代码非常困惑且难以阅读。希望有人能帮忙。 提前谢谢。 int potPi
我使用 AVR 作为微 Controller ,ATMEGA8 作为处理器(在微 Controller 内部)。带有微 Controller 的电路板有 4 个 LED。我能够刻录程序并点亮 LEDS
谁能解释一下两者之间的关系 LED 像素密度, LED像素高度和宽度 , 和 分辨率在创建模拟器时方法很详细。。并在启动时再次默认值:皮肤尺寸、密度;缩放密度以实现大小? 最佳答案 像素密度:每英寸像
我有一个由大小为 64*64 的 LED 组成的阵列。每个 LED 都可以点亮各种颜色,并且可以非常快速地改变颜色。我的最终目标是:对于网络摄像头接收到的每一帧,我希望能够从帧中“提取”LED 数组,
我正在使用 Arduino Uno 软件进行编码,但遇到了问题。我试图以随机顺序打开 8 个 LED,然后立即将它们全部关闭。我的问题是 LED 会多次点亮相同的 LED,但最终不会全部点亮。这使得电
当我使用Ethernet.begin(mac,ip)时,LED灯不会打开和关闭。但是当我不使用那条线时它就可以工作。但我需要使用以太网和 UPP 模块来打开和关闭。我怎样才能? 板卡型号:以太网08T
我试图顺时针和逆时针方向点亮我的 LED 灯。我使用mpu6050作为LED的方向控制。我检查了我的 mpu6050 驱动代码,它们没问题。顺时针方向工作时效果很好,所有 LED 灯均呈红色亮起。像这
关闭。这个问题需要多问focused 。目前不接受答案。 想要改进此问题吗?更新问题,使其仅关注一个问题 editing this post . 已关闭 4 年前。 Improve this ques
我是一名优秀的程序员,十分优秀!