Android Camera Flashlight控制

在Android 6.0及更高版本中,无需打开Camera设备即可使用setTorchMode()方法控制闪光灯。当相机设备不可用或其它资源占用时,闪光灯会自动关闭。可以注册回调以监听闪光灯状态变化,当成功开启或关闭闪光灯时,onTorchModeChanged()方法会被调用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

根据Android6.0新特性,在最新的API下,使用闪光灯,不再需要打开Camera,setTorchMode()的方式打开闪光灯。
Flashlight API
If a camera device has a flash unit, you can call the setTorchMode() method to switch the flash unit’s torch mode on or off without opening the camera device. The app does not have exclusive ownership of the flash unit or the camera device. The torch mode is turned off and becomes unavailable whenever the camera device becomes unavailable, or when other camera resources keeping the torch on become unavailable. Other apps can also call setTorchMode() to turn off the torch mode. When the last app that turned on the torch mode is closed, the torch mode is turned off.
You can register a callback to be notified about torch mode status by calling the registerTorchCallback() method. The first time the callback is registered, it is immediately called with the torch mode status of all currently known camera devices with a flash unit. If the torch mode is turned on or off successfully, the onTorchModeChanged() method is invoked.

以下的代码展示了如何在Android6.0下打开闪光灯。

import android.app.Activity;
import android.content.Context;
import android.hardware.Camera;
import android.hardware.camera2.CameraAccessException;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.CompoundButton;
import android.widget.ToggleButton;


public class MainActivity extends Activity {

    private CameraManager manager;//通过CameraManager控制闪光灯
    private Camera camera = null;
    private Camera.Parameters parameters = null;
    public static boolean kaiguan = true; // 定义开关状态,状态为false,打开状态,状态为true,关闭状态


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        super.onCreate(savedInstanceState);

        manager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);//获得camera_service服务

        try {
            String [] camerList = manager.getCameraIdList();
            for (String str:camerList
                 ) {
                Log.d("List",str);
            }
        } catch (CameraAccessException e) {
            Log.e("error",e.getMessage());
        }
        Button open_btn = (Button) findViewById(R.id.open_btn);

        open_btn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {                
                    try {
                        manager.setTorchMode("0", true);//设置闪光灯模式
                    } catch (CameraAccessException e) {
                        e.printStackTrace();
                    }
                }            
        });

        Button close_btn = (Button) findViewById(R.id.close_btn);
        close_btn.setOnClickListener(closeOnClickListener);

        ToggleButton toggle_btn = (ToggleButton) findViewById(R.id.toggle_btn);
        toggle_btn.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                try {
                    manager.setTorchMode("1", isChecked);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }
            }
        });
    }

    private View.OnClickListener closeOnClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {            
                try {
                    manager.setTorchMode("0", false);
                } catch (CameraAccessException e) {
                    e.printStackTrace();
                }            
        }
    };  
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值