android手电筒

手电筒,原理很简单 ,控制摄像头的闪光灯持续闪光进行照明,废话不多说直接上源码大笑

主文件夹中mainactivity代码如下:

package com.madeLi.shoudian;


import java.util.List;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.PowerManager;
import android.os.Vibrator;
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;


public class MainActivity extends Activity implements OnClickListener {


public ImageView btnimageView = null;
public RelativeLayout bgmageView = null;
private Camera camera;
private boolean isOpen = true;
public final static int OPEN_CAMERA = 1011;
public final static int OPEN_LIGHT = 1012;
public final static int CLOSE_LIGHT = 1013;
private Vibrator vibrator;
long[] pattern = { 100, 200 };
PowerManager.WakeLock wakeLock;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
handler.sendEmptyMessage(OPEN_CAMERA);
vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
btnimageView = (ImageView) findViewById(R.id.btn_image);
bgmageView = (RelativeLayout) findViewById(R.id.bg_image);
bgmageView.setBackgroundResource(R.drawable.bg_flashlight_off);
btnimageView.setImageResource(R.drawable.btn_flash_light_off);
btnimageView.setOnClickListener(this);
bgmageView.setBackgroundResource(R.drawable.bg_flashlight_on);
btnimageView.setImageResource(R.drawable.btn_flash_light_on);
handler.sendEmptyMessage(OPEN_LIGHT);
vibrator.vibrate(pattern, -1);


}


Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case OPEN_CAMERA:
camera = Camera.open();
break;
case OPEN_LIGHT:
Parameters params = camera.getParameters();
List<String> list = params.getSupportedFlashModes();
if (list.contains(Parameters.FLASH_MODE_TORCH)) {
params.setFlashMode(Parameters.FLASH_MODE_TORCH);
} else {
Toast.makeText(getApplicationContext(), "此设备不支持闪光灯模式",
Toast.LENGTH_SHORT).show();
}
camera.setParameters(params);
camera.startPreview();
isOpen = true;
break;
case CLOSE_LIGHT:
if (isOpen) {
Parameters closepParameters = camera.getParameters();
closepParameters
.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(closepParameters);
camera.stopPreview();
// camera.release();
isOpen = false;
// sendEmptyMessage(OPEN_CAMERA);
}
break;
default:
break;
}
}
};


@Override
protected void onDestroy() {
super.onDestroy();
if (null != camera) {
camera.release();
camera = null;
}
}


public void onStop() {
super.onStop();
vibrator.cancel();
if (null != camera) {
camera.release();
camera = null;
}


MainActivity.this.finish();
}


@Override
protected void onResume() {
// TODO Auto-generated method stub
super.onResume();
PowerManager pm = (PowerManager) getSystemService(POWER_SERVICE);
wakeLock = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK
| PowerManager.ON_AFTER_RELEASE, "test");
wakeLock.acquire();
}


@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
if (wakeLock != null)
wakeLock.release();
}


@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_image:
if (isOpen) {
bgmageView.setBackgroundResource(R.drawable.bg_flashlight_off);
btnimageView.setImageResource(R.drawable.btn_flash_light_off);
handler.sendEmptyMessage(CLOSE_LIGHT);
vibrator.vibrate(pattern, -1);
} else {
bgmageView.setBackgroundResource(R.drawable.bg_flashlight_on);
btnimageView.setImageResource(R.drawable.btn_flash_light_on);
handler.sendEmptyMessage(OPEN_LIGHT);
vibrator.vibrate(pattern, -1);
}
break;
default:
break;
}


}


}

布局文件:

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:background="@drawable/bg_flashlight" >


    <RelativeLayout
        android:id="@+id/bg_image"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@drawable/bg_flashlight_off" >


        <ImageView
            android:id="@+id/btn_image"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
           android:layout_centerHorizontal="true"
             android:layout_alignParentBottom="true"
             android:layout_marginBottom="100dip"
            android:src="@drawable/btn_flash_light_on" />
    </RelativeLayout>


</RelativeLayout>


再最后我们添加上一个权限的设置就OK了

    <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" />
    <uses-permission android:name="android.permission.VIBRATE" />
    <uses-permission android:name="android.permission.WAKE_LOCK" />



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值