欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

android通過(guò)led實(shí)現(xiàn)手電筒功能

 更新時(shí)間:2019年09月23日 11:17:04   作者:Code Man  
這篇文章主要為大家詳細(xì)介紹了android通過(guò)led實(shí)現(xiàn)手電筒功能,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下

本文實(shí)例為大家分享了android通過(guò)led實(shí)現(xiàn)手電筒功能的具體代碼,供大家參考,具體內(nèi)容如下

第一步 添加權(quán)限:

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

第二步 實(shí)現(xiàn)手電筒工具類:

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<String, String, String> {

 private Camera camera;
 private int cameraId = 0; // 此功能目前不實(shí)用,這里不做實(shí)現(xiàn) , 但不能刪除
 private Parameters parameters;
 public boolean isTorch = false;
 private boolean canFinish = false;
 private static FlashlightUtil flashlightUtil;

 private FlashlightUtil() {

 }

 /**
  * 設(shè)置手電筒開(kāi)關(guān),打開(kāi)或關(guān)閉手電筒,根據(jù)手電筒的狀態(tài)來(lái)設(shè)置相反的狀態(tài) 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)) {
     // 打開(kāi)鎂光燈
     parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
     camera.setParameters(parameters);
     camera.startPreview();
    }
   } else {
    if (parameters.getFlashMode().equals(
      Parameters.FLASH_MODE_TORCH)) {
     // 關(guān)閉鎂光燈
     camera.stopPreview(); // 關(guān)掉亮燈
     camera.release(); // 關(guān)掉照相機(jī)
     camera = null;
    }
   }
  }

  return null;
 }

 /**
  * 此功能暫時(shí)關(guān)閉
  * @hide
  */
 public FlashlightUtil setCameraId(int cameraId) {
  this.cameraId = cameraId;
  return flashlightUtil;
 }

 /**
  * 打開(kāi)關(guān)閉手電筒,默認(rèn)第一次為打開(kāi) 2016年1月12日
  */
 private void setONOFF() {
  isTorch = !isTorch;
 }


}

第三步 添加手電筒按鈕的觸發(fā)事件(這里是在布局文件中通過(guò)onclick實(shí)現(xiàn)的)

/**
 * 打開(kāi)手電筒
 */
public void openFlashlight(View view) {
 FlashlightUtil.setSwitch() ;
}

手電筒功能比較簡(jiǎn)單,這里實(shí)現(xiàn)了一步開(kāi)啟關(guān)閉。

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論