如何在Android中以编程方式打开前闪光灯? [英] How to turn on front flash light programmatically in Android?
问题描述
我想在Android中以编程方式打开前闪光灯(不使用相机预览).我用Google搜索了它,但是找到的帮助却把我引到了此页面
有人有任何链接或示例代码吗?
对于此问题,您应该:
-
检查手电筒是否 有空吗?
-
如果是,则关闭/打开
-
如果没有,那么您可以根据自己的应用执行任何操作 需求.
对于检查设备中闪存的可用性:
您可以使用以下内容:
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
如果有闪存可用,则返回true,否则返回false.
请参阅:
http://developer.android.com/reference/android/content/pm/PackageManager.html 了解更多信息.
用于打开/关闭手电筒:
我在Google上搜索了有关android.permission.FLASHLIGHT的信息. Android清单的许可看起来很有希望:
<!-- Allows access to the flashlight -->
<permission android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal"
android:label="@string/permlab_flashlight"
android:description="@string/permdesc_flashlight" />
然后使用相机并设置 FLASH_MODE_TORCH .
例如
代码段可打开相机手电筒.
Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
代码段可关闭相机LED灯.
cam.stopPreview();
cam.release();
我刚找到一个使用此权限的项目.检查快速设置的src代码.此处 http://code.google.com/p/quick-settings/(注意:此链接现在已断开)
对于Flashlight,直接看 Galaxy Nexus上的LED手电可由什么API控制? 这似乎是可以在许多手机上使用的解决方案.
更新5 主要更新
我找到了一个替代链接(针对上面的断开链接): http://code.google.com/p/torch/source/browse/
更新2
显示如何在Motorola Droid上启用LED的示例: http://code.google.com/p/droidled/
另一个开源代码:
http://code.google.com/p/covedesigndev/
http://code.google.com/p/search-light/ >
更新3(用于打开/关闭相机指示灯的小部件)
如果您要开发一个打开/关闭相机LED的小部件,则必须参考我的回答我可以更改Android设备的LED强度吗?全文.请注意,只有植根的HTC设备才支持此功能.
**问题:**
打开/关闭手电筒时也有一些问题.例如.对于没有FLASH_MODE_TORCH
或什至没有FLASH_MODE_TORCH
的设备,则手电筒无法打开等.
三星通常会产生很多问题.
您可以在下面的列表中参考问题:
this page
Does anyone have any links or sample code?
For this problem you should:
Check whether the flashlight is available or not?
If so then Turn Off/On
If not then you can do whatever, according to your app needs.
For Checking availability of flash in the device:
You can use the following:
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
which will return true if a flash is available, false if not.
See:
http://developer.android.com/reference/android/content/pm/PackageManager.html for more information.
For turning on/off flashlight:
I googled out and got this about android.permission.FLASHLIGHT. Android manifests' permission looks promising:
<!-- Allows access to the flashlight -->
<permission android:name="android.permission.FLASHLIGHT"
android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
android:protectionLevel="normal"
android:label="@string/permlab_flashlight"
android:description="@string/permdesc_flashlight" />
Then make use of Camera and set Camera.Parameters. The main parameter used here is FLASH_MODE_TORCH.
eg.
Code Snippet to turn on camera flashlight.
Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
Code snippet to turn off camera led light.
cam.stopPreview();
cam.release();
I just found a project that uses this permission. Check quick-settings' src code. here http://code.google.com/p/quick-settings/ (Note: This link is now broken)
For Flashlight directly look http://code.google.com/p/quick-settings/source/browse/trunk/quick-settings/#quick-settings/src/com/bwx/bequick/flashlight (Note: This link is now broken)
Update6 You could also try to add a SurfaceView as described in this answer LED flashlight on Galaxy Nexus controllable by what API? This seems to be a solution that works on many phones.
Update 5 Major Update
I have found an alternative Link (for the broken links above): http://www.java2s.com/Open-Source/Android/Tools/quick-settings/com.bwx.bequick.flashlight.htm You can now use this link. [Update: 14/9/2012 This link is now broken]
Update 1
Another OpenSource Code : http://code.google.com/p/torch/source/browse/
Update 2
Example showing how to enable the LED on a Motorola Droid: http://code.google.com/p/droidled/
Another Open Source Code :
http://code.google.com/p/covedesigndev/
http://code.google.com/p/search-light/
Update 3 (Widget for turning on/off camera led)
If you want to develop a widget that turns on/off your camera led, then you must refer my answer Widget for turning on/off camera flashlight in android..
Update 4
If you want to set the intensity of light emerging from camera LED you can refer Can I change the LED intensity of an Android device? full post. Note that only rooted HTC devices support this feature.
** Issues:**
There are also some problems while turning On/Off flashlight. eg. for the devices not having FLASH_MODE_TORCH
or even if it has, then flashlight does not turn ON etc.
Typically Samsung creates a lot of problems.
You can refer to problems in the given below list:
Use camera flashlight in Android
Turn ON/OFF Camera LED/flash light in Samsung Galaxy Ace 2.2.1 & Galaxy Tab
这篇关于如何在Android中以编程方式打开前闪光灯?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!