Android 手电筒源码

时间:2014-04-29 13:37:22   收藏:0   阅读:473

最近由于公司需要,做了一个手电筒,其实手电筒原理很简单,就是调用照相机的闪光灯,控制闪光灯的开关,就可以实现手电筒的效果,

强调一下,代码中一定要注意在结束的时候对闪光灯进行释放,否则就会导致使用照相机的时候出现——无法连接到相机 这个问题

手电筒APK下载地址:点击打开下载链接

手电筒项目源码下载:点击打开下载链接

主要代码如下:

package com.techainsh.flashlight;

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;
		}

	}

}
需要用到的权限如下:

 <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" /> 

界面布局如下:

<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>

实现的效果如下:

mamicode.com,码迷mamicode.com,码迷


Android 手电筒源码,码迷,mamicode.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!