如何在 Android 中以编程方式打开前置闪光灯? [英] How to turn on front flash light programmatically in Android?
问题描述
我想在 Android 中以编程方式打开前置闪光灯(不带相机预览).我用谷歌搜索了它,但我发现的帮助让我参考了this页面
有人有任何链接或示例代码吗?
对于 2021 年,有了 CameraX,现在变得非常简单:https://stackoverflow.com/a/66585201/294884
对于这个问题,你应该:
检查手电筒是否可用与否?
如果是,则关闭/打开
如果没有,那么你可以根据你的应用做任何事情需要.
对于检查设备中闪存的可用性:
您可以使用以下内容:
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
如果 flash 可用则返回 true,否则返回 false.
请参阅:
http://developer.android.com/reference/android/content/pm/PackageManager.html 了解更多信息.
用于打开/关闭手电筒:
我在谷歌上搜索了关于 android.permission.FLASHLIGHT 的信息.Android manifests 的许可看起来很有希望:
<permission android:name="android.permission.FLASHLIGHT";android:permissionGroup="android.permission-group.HARDWARE_CONTROLS";android:protectionLevel=正常"android:label="@string/permlab_flashlight";android:description="@string/permdesc_flashlight";/>
然后使用 Camera 并设置 Camera.Parameters.这里使用的主要参数是FLASH_MODE_TORCH.>
例如
用于打开相机手电筒的代码片段.
Camera cam = Camera.open();参数 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/(注意:此链接现已失效)
对于手电筒直接查看http://code.google.com/p/quick-settings/source/browse/trunk/quick-settings/#quick-settings/src/com/bwx/bquick/flashlight(注意:此链接现已失效)
更新 6您也可以尝试添加一个 SurfaceView,如本答案 可以通过什么 API 控制 Galaxy Nexus 上的 LED 手电筒?这似乎是适用于许多手机的解决方案.
更新 5 重大更新
我找到了一个替代链接(对于上面断开的链接):http://www.java2s.com/Open-Source/Android/Tools/quick-settings/com.bwx.bequick.flashlight.htm 您现在可以使用这个链接.[更新:14/9/2012 此链接现已失效]
更新 1
另一个开源代码:http://code.google.com/p/torch/source/browse/
更新 2
显示如何在摩托罗拉 Droid 上启用 LED 的示例:http://code.google.com/p/droidled/
另一个开源代码:
http://code.google.com/p/covedesigndev/
http://code.google.com/p/search-light/
更新 3(用于打开/关闭摄像头 LED 的小工具)
如果您想开发一个可以打开/关闭摄像头 LED 的小部件,那么您必须参考我的回答 用于在 android 中打开/关闭相机手电筒的小部件.
更新 4
如果您想设置从摄像头 LED 发出的光强度,您可以参考 我可以更改 Android 设备的 LED 强度吗? 完整帖子.请注意,只有 HTC 设备支持此功能.
** 问题:**
打开/关闭手电筒时也有一些问题.例如.对于没有 FLASH_MODE_TORCH
的设备,或者即使有,手电筒也不会打开等.
通常三星会产生很多问题.
您可以参考以下列表中的问题:
在三星 Galaxy Ace 2.2.1 & 中打开/关闭相机 LED/闪光灯Galaxy Tab
I want to turn on front flash light (not with camera preview) programmatically in Android. I googled for it but the help i found referred me to this page
Does anyone have any links or sample code?
For 2021, with CameraX, it is now dead easy: https://stackoverflow.com/a/66585201/294884
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屋!