标签:
1 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:tools="http://schemas.android.com/tools" 3 android:layout_width="match_parent" 4 android:layout_height="match_parent" 5 android:orientation="vertical"> 6 7 <Button 8 android:id="@+id/btn_flash" 9 android:layout_width="match_parent" 10 android:layout_height="wrap_content" 11 android:text="手电筒已关闭!"/> 12 13 </LinearLayout>
1 <!-- 摄像头、手电筒 --> 2 <uses-permission android:name="android.permission.CAMERA" /> 3 <uses-permission android:name="android.permission.FLASHLIGHT" /> 4 <uses-feature android:name="android.hardware.camera" /> 5 <uses-feature android:name="android.hardware.camera.autofocus" /> 6 <uses-feature android:name="android.hardware.camera.flash" />
1 package com.example.test; 2 3 import android.app.Activity; 4 import android.hardware.Camera; 5 import android.hardware.Camera.Parameters; 6 import android.os.Bundle; 7 import android.view.View; 8 import android.view.View.OnClickListener; 9 import android.widget.Button; 10 import android.widget.Toast; 11 12 public class MainActivity extends Activity { 13 14 private boolean isopen = false; //记录手电筒状态 15 private Camera camera; // 16 private Button btn_flash; 17 18 @Override 19 protected void onCreate(Bundle savedInstanceState) { 20 // TODO Auto-generated method stub 21 super.onCreate(savedInstanceState); 22 setContentView(R.layout.activity_main); 23 24 btn_flash = (Button) findViewById(R.id.btn_flash); 25 btn_flash.setOnClickListener(new OnClickListener() { 26 @Override 27 public void onClick(View v) { 28 if (!isopen) { //如果手电筒已经开启 29 Toast.makeText(getApplicationContext(), "手电筒已开启", 0).show(); 30 camera = Camera.open(); 31 Parameters params = camera.getParameters(); 32 params.setFlashMode(Parameters.FLASH_MODE_TORCH); 33 camera.setParameters(params); 34 camera.startPreview(); // 开始亮灯 35 isopen = true; 36 btn_flash.setText("手电筒已开启!"); 37 } else { 38 Toast.makeText(getApplicationContext(), "关闭了手电筒", 39 Toast.LENGTH_SHORT).show(); 40 camera.stopPreview(); // 关掉亮灯 41 camera.release(); // 关掉照相机 42 isopen = false; 43 btn_flash.setText("手电筒已关闭!"); 44 } 45 } 46 }); 47 } 48 49 }
标签:
原文地址:http://www.cnblogs.com/WangKangReg/p/4266657.html