您好,登錄后才能下訂單哦!
這篇文章給大家分享的是有關(guān)Android如何開啟閃光燈的內(nèi)容。小編覺得挺實(shí)用的,因此分享給大家做個(gè)參考,一起跟隨小編過來看看吧。
前言
眾所周知,安卓的手電筒功能無非是開啟手機(jī)的閃光燈,既然開啟閃光燈就必須要與相機(jī)關(guān)聯(lián)。
代碼
寫了個(gè)小工具,一個(gè)文件搞定。
public class FlashUtils { private static FlashUtils utils; private static Camera camera; public static boolean mIsOpen = true; //使用單例模式在這里初始化相機(jī) public static FlashUtils getInstance() { if (utils == null) { utils = new FlashUtils(); } try { if (camera == null) { camera = Camera.open(); } } catch (Exception e) { if (camera != null) { camera.release(); } camera = null; } return utils; } //參考二維碼工具的閃光燈 public void switchFlash() { try { Parameters parameters = camera.getParameters(); if (mIsOpen) { if (parameters.getFlashMode().equals("torch")) { return; } else { parameters.setFlashMode("torch"); } } else { if (parameters.getFlashMode().equals("off")) { return; } else { parameters.setFlashMode("off"); } } camera.setParameters(parameters); } catch (Exception e) { finishFlashUtils(); } mIsOpen = !mIsOpen; } //頁面銷毀的時(shí)候調(diào)用此方法 public void finishFlashUtils() { if (camera != null) { camera.stopPreview(); camera.release(); } camera = null; } }
感謝各位的閱讀!關(guān)于“Android如何開啟閃光燈”這篇文章就分享到這里了,希望以上內(nèi)容可以對大家有一定的幫助,讓大家可以學(xué)到更多知識,如果覺得文章不錯(cuò),可以把它分享出去讓更多的人看到吧!
免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點(diǎn)不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報(bào),并提供相關(guān)證據(jù),一經(jīng)查實(shí),將立刻刪除涉嫌侵權(quán)內(nèi)容。