Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to realize the function of flashlight through led in android

2025-03-27 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/03 Report--

这篇文章给大家分享的是有关android如何通过led实现手电筒功能的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

具体内容如下

第一步 添加权限:

第二步 实现手电筒工具类:

import android.hardware.Camera;import android.hardware.Camera.Parameters;import android.os.AsyncTask;/** *Caution: On some devices, this method may take a long time to complete. It is best *to call this method from a worker thread (possibly using android.os.AsyncTask) to *avoid blocking the main application UI thread. */public class FlashlightUtil extends AsyncTask { private Camera camera; private int cameraId = 0; // 此功能目前不实用,这里不做实现 , 但不能删除 private Parameters parameters; public boolean isTorch = false; private boolean canFinish = false; private static FlashlightUtil flashlightUtil; private FlashlightUtil() { } /** * 设置手电筒开关,打开或关闭手电筒,根据手电筒的状态来设置相反的状态 void 2016年1月12日 */ public static void setSwitch() { if (null == flashlightUtil) { flashlightUtil = new FlashlightUtil(); flashlightUtil.execute(""); } flashlightUtil.setONOFF(); } @Override protected String doInBackground(String... params) { // TODO Auto-generated method stub while (!canFinish) { if (null == camera) { camera = Camera.open(cameraId); } parameters = camera.getParameters(); if (isTorch) { if (parameters.getFlashMode().equals(Parameters.FLASH_MODE_OFF)) { // 打开镁光灯 parameters.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(parameters); camera.startPreview(); } } else { if (parameters.getFlashMode().equals( Parameters.FLASH_MODE_TORCH)) { // 关闭镁光灯 camera.stopPreview(); // 关掉亮灯 camera.release(); // 关掉照相机 camera = null; } } } return null; } /** * 此功能暂时关闭 * @hide */ public FlashlightUtil setCameraId(int cameraId) { this.cameraId = cameraId; return flashlightUtil; } /** * 打开关闭手电筒,默认第一次为打开 2016年1月12日 */ private void setONOFF() { isTorch = !isTorch; }}

第三步 添加手电筒按钮的触发事件(这里是在布局文件中通过onclick实现的)

/** * 打开手电筒 */public void openFlashlight(View view) { FlashlightUtil.setSwitch() ;}

手电筒功能比较简单,这里实现了一步开启关闭。

感谢各位的阅读!关于"android如何通过led实现手电筒功能"这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 301

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report