Android闪光灯的打开代码:

camera = Camera.open();Parameters params = camera.getParameters();params.setFlashMode(Parameters.FLASH_MODE_TORCH);camera.setParameters(params);camera.startPreview(); // 开始亮灯img_main.setBackgroundColor(Color.WHITE);isopen = true;Toast.makeText(getApplicationContext(), "手电筒已打开", 0).show();

Android闪光灯的关闭代码:

camera.stopPreview(); // 关掉亮灯camera.release(); // 关掉照相机img_main.setBackgroundResource(R.drawable.main_body);isopen = false;Toast.makeText(getApplicationContext(), "手电筒已关闭",                            Toast.LENGTH_SHORT).show();

Android闪光灯的权限设置:

<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.autofocus" /><uses-feature android:name="android.hardware.camera.flash" />

MainActivity完整代码:

package com.j*****.flashlight;import android.annotation.SuppressLint;import android.app.Activity;import android.graphics.Color;import android.hardware.Camera;import android.hardware.Camera.Parameters;import android.os.Bundle;import android.view.View;import android.view.Window;import android.view.WindowManager;import android.widget.TextView;import android.widget.Toast;public class MainActivity extends Activity {    private boolean isopen = false;    private Camera camera;    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        requestWindowFeature(Window.FEATURE_NO_TITLE);        getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,                WindowManager.LayoutParams.FLAG_FULLSCREEN);        View view = View.inflate(this, R.layout.main, null);        setContentView(view);        final TextView img_main = (TextView) findViewById(R.id.main_img);        img_main.setOnClickListener(new View.OnClickListener() {            @SuppressLint("ShowToast")            @Override            public void onClick(View v) {                if (!isopen) {                    camera = Camera.open();                    Parameters params = camera.getParameters();                    params.setFlashMode(Parameters.FLASH_MODE_TORCH);                    camera.setParameters(params);                    camera.startPreview(); // 开始亮灯                    img_main.setBackgroundColor(Color.WHITE);                    isopen = true;                    Toast.makeText(getApplicationContext(), "手电筒已打开", 0).show();                } else {                    camera.stopPreview(); // 关掉亮灯                    camera.release(); // 关掉照相机                    img_main.setBackgroundResource(R.drawable.main_body);                    isopen = false;                    Toast.makeText(getApplicationContext(), "手电筒已关闭",                            Toast.LENGTH_SHORT).show();                }            }        });    }}

即可;

更多相关文章

  1. android手电筒原理
  2. android 闪光灯控制
  3. S5PV210 ANDROID 为摄像头增加闪光灯
  4. Android之打开闪光灯关键代码
  5. Android(安卓)控制闪光灯
  6. Android(安卓)开启闪光灯做手电筒 源码
  7. android手电筒开发
  8. CloudBox范例代码:CloudLED
  9. android 带手电筒的扫一扫(1 可以自动打开手电筒,2 可以自动对焦,增

随机推荐

  1. Android消息队列模型——Thread,Handler,
  2. 一起学opengl android
  3. 为什么我们可以在非UI线程中更新UI
  4. Android实现使用微信登录第三方APP的方法
  5. 从纪念碑谷 Monument Valley 登陆 Androi
  6. Android中AsyncTask的基本用法
  7. Android(安卓)UI 设计——TextView 控件
  8. Android(安卓)问题杂笔
  9. android 系统权限大全
  10. Android-Lifecycle超能解析-生命周期的那