当前位置: 首页 > 工具软件 > flash light > 使用案例 >

Android Camera Flashlight控制

厍光霁
2023-12-01

根据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();
                }            
        }
    };  
}
 类似资料: