Android之打开闪光灯关键代码
在AndroidManifest中注册相应的权限:
<uses-permission android:name="android.permission.FLASHLIGHT" />
<uses-permission android:name="android.permission.CAMERA"/>
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.autofocus"/>
打开闪光灯代码:
Camera camera = Camera.open();
camera.startPreview();
parameter = camera.getParameters();
parameter.setFlashMode(Parameters.FLASH_MODE_TORCH);
camera.setParameters(parameter);
关闭闪关灯代码:
parameter = camera.getParameters();
parameter.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameter);
MainActivity
package com.example.flashlight;
import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
@SuppressWarnings("deprecation")
public class MainActivity extends Activity {
private static int count = 0;// 计数器
private Camera camera = Camera.open();// 注意是import android.hardware.Camera;
private Parameters parameters = camera.getParameters();
private Button mFlashLight;
/*-
* 通过Camera控制摄像头闪光灯的步骤:
* 1.调用Camera的open()方法打开相机。
* 2.调用Camera的getParameters()方法获取拍照参数。该方法返回一个Camera.Parameters对象。
* 3.调用parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);设置闪光灯的模式
* 4.调用Camera.setParamers(parameters)方法设置拍照参数
* 6.调用Camera的setParameters,并将Camera.Paramers作为参数传入,这样即可对相机的拍照参数进行控制
* 6.调用Camera的startPreview()方法开始预览取景,在预览取景之前需要调用Camera的setPreViewDisplay(SurfaceHolder holder)方法设置使用哪个SurfaceView来显示取景图片。
* 7.结束程序时,调用Camera的StopPriview()结束取景预览,并调用release()方法释放资源。
*/
public void onClickLight(View view) {
if (count % 2 == 0) {
camera.startPreview();
parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
// FLASH_MODE_TORCH & FLASH_MODE_AUTO & FLASH_MODE_ON &
// FLASH_MODE_OFF
camera.setParameters(parameters);
count++;
mFlashLight.setText("关闭闪光灯");
} else {
parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
camera.setParameters(parameters);
count++;
mFlashLight.setText("开启闪光灯");
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
mFlashLight = (Button) findViewById(R.id.btn_flashlight);
}
public void onClickGreen(View view) {
// 第一步:
// 获取当前窗体->设置窗体的背景颜色--import android.graphics.drawable.ColorDrawable;
// 第二步:
// new ColorDrawable(Color.GREEN)||ColorDrawable extends Drawable
ColorDrawable colorDrawable = new ColorDrawable();
colorDrawable.setColor(Color.GREEN);
getWindow().setBackgroundDrawable(colorDrawable);
}
public void onClickBlue(View view) {
getWindow().setBackgroundDrawable(new ColorDrawable(Color.BLUE));
}
public void onClickRed(View view) {
getWindow().setBackgroundDrawable(new ColorDrawable(Color.RED));
}
public void onClickWhite(View view) {
getWindow().setBackgroundDrawableResource(R.drawable.color_white);
}
public void onClickPhoto(View view) {
// 也可以是图片
getWindow().setBackgroundDrawableResource(R.drawable.cjk);
}
/**
* 在activity中释放照相机的资源
*/
@Override
protected void onDestroy() {
camera.stopPreview();
camera.release();// release:释放
camera = null;
super.onDestroy();
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.flashlight.MainActivity" >
<Button
android:id="@+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:onClick="onClickRed"
android:text="红色" />
<Button
android:id="@+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button4"
android:layout_alignBottom="@+id/button4"
android:onClick="onClickGreen"
android:layout_toRightOf="@+id/button4"
android:text="绿色" />
<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="@+id/button1"
android:onClick="onClickWhite"
android:layout_alignParentRight="true"
android:text="白色" />
<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button3"
android:layout_alignBottom="@+id/button3"
android:onClick="onClickBlue"
android:layout_toLeftOf="@+id/button3"
android:text="蓝色" />
<Button
android:id="@+id/btn_flashlight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button4"
android:layout_alignRight="@+id/button5"
android:layout_below="@+id/button4"
android:layout_marginTop="18dp"
android:onClick="onClickLight"
android:text="开启闪光灯" />
<Button
android:id="@+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/btn_flashlight"
android:layout_alignBottom="@+id/btn_flashlight"
android:layout_alignLeft="@+id/button2"
android:layout_alignRight="@+id/button3"
android:onClick="onClickPhoto"
android:text="设置背景" />
</RelativeLayout>
drawable
<?xml version="1.0" encoding="utf-8"?>
<color xmlns:android="http://schemas.android.com/apk/res/android"
android:color="#fff" >
</color>
manifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.flashlight"
android:versionCode="1"
android:versionName="1.0" >
> <uses-permission android:name="android.permission.FLASHLIGHT" />
> <uses-permission android:name="android.permission.CAMERA" />
> <uses-feature android:name="android.hardware.camera" />
> <uses-feature android:name="android.hardware.autofocus" />
<uses-sdk
android:minSdkVersion="14"
android:targetSdkVersion="22" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
值的注意的一点是开启了闪光灯后再关闭,一定要释放资源
camera.stopPreview();
camera.release();// release:释放
camera = null;
引用:开启闪关灯的权限及方法