android 手电筒的实现

android手机用闪光灯做成手电筒的应用非常多,可是有的不能用。

后来发现是除了把 camera device的 flashmode设置成torch外还要打开预览:

以下是代码:


MainActivity.java

package com.android.flashlight;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.widget.ImageView;

public class MainActivity extends Activity{

	private FlashlightSurface mSurface;
	private ImageView mImageView;
	private boolean isFlashlightOn = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		Contants.LogI("MainActivity: onCreate()");
		
		mSurface = (FlashlightSurface) findViewById(R.id.surfaceview);
		mImageView = (ImageView) findViewById(R.id.image);
		
	}

	@Override
	public boolean onTouchEvent(MotionEvent event) {
		if(MotionEvent.ACTION_UP == event.getAction()){
			Contants.LogI("MainActivity: onTouchEvent() : ACTION_UP");
			if(isFlashlightOn){
				mSurface.setFlashlightSwitch(false);
				isFlashlightOn = false;
				mImageView.setImageResource(R.drawable.flashlight_off);
			}else{
				mSurface.setFlashlightSwitch(true);
				isFlashlightOn = true;
				mImageView.setImageResource(R.drawable.flashlight_on);
			}
		}
		return super.onTouchEvent(event);
	}
	
}

FlashlightSurface.java

package com.android.flashlight;

import android.content.Context;
import android.graphics.PixelFormat;
import android.hardware.Camera;
import android.util.AttributeSet;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

public class FlashlightSurface extends SurfaceView implements SurfaceHolder.Callback{

	private SurfaceHolder mHolder;
	private Camera mCameraDevices;
	private Camera.Parameters mParameters;
	
	public FlashlightSurface(Context context, AttributeSet attrs) {
		super(context, attrs);
		Contants.LogI("FlashlightSurface");
		mHolder = this.getHolder();
		mHolder.addCallback(this);
		mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
	}

	@Override
	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
		Contants.LogI("surfaceChanged");
		mParameters = mCameraDevices.getParameters();
		if(mParameters != null)
			mParameters.setPictureFormat(PixelFormat.JPEG);
		mParameters.setPreviewSize(320, 480);
		mParameters.setPictureSize(320, 480);
		mCameraDevices.setParameters(mParameters);
		mCameraDevices.startPreview();
		
	}

	@Override
	public void surfaceCreated(SurfaceHolder holder) {
		Contants.LogI("surfaceCreated");
		try {
			mCameraDevices = Camera.open();
			mCameraDevices.setPreviewDisplay(mHolder);
		} catch (Exception e) {
			if(mCameraDevices != null)
				mCameraDevices.release();
			mCameraDevices = null;
		}
	}

	@Override
	public void surfaceDestroyed(SurfaceHolder holder) {
		Contants.LogI("surfaceDestroyed");
		if(mCameraDevices == null) return;
		mCameraDevices.stopPreview();
		mCameraDevices.release();
		mCameraDevices = null;
	}
	
	/**
	 * 设置手电筒的开关状态
	 * @param on : true则打开,false则关闭
	 */
	public void setFlashlightSwitch(boolean on){
		if(mCameraDevices == null) return;
		if(mParameters == null){
			mParameters = mCameraDevices.getParameters();
		}
		if(on){
			mParameters.setFlashMode(Contants.FLASH_MODE_TORCH);
		}else{
			mParameters.setFlashMode(Contants.FLASH_MODE_OFF);
		}
		Contants.LogI("setFlashlightSwitch-----------------" + on);
		mCameraDevices.setParameters(mParameters);
	}

}

布局文件main.xml

<?xml version="1.0" encoding="utf-8"?>   
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_width="fill_parent"  
    android:layout_height="fill_parent" >

    <com.android.flashlight.FlashlightSurface
        android:id="@+id/surfaceview"
        android:layout_width="fill_parent"  
    	android:layout_height="fill_parent"
        ></com.android.flashlight.FlashlightSurface>
    <ImageView 
        android:id="@+id/image"
        android:layout_width="fill_parent"  
    	android:layout_height="fill_parent"
    	android:src="@drawable/flashlight_off"
    	android:background="#FFFFFFFF"
        />
    
</RelativeLayout>

显然这里打开了一个预览,可是被图片盖上了,看不见而已。不然闪光灯不亮。


这里有个Demo:http://download.csdn.net/detail/liu_zhen_wei/4801779

包含widget窗体小部件的使用方法和手电筒的功能实现,安装后,加入�窗体小部件(手电筒)到桌面,然后点击小部件后,打开手电筒的界面

点击界面,实现 打开/关闭 手电筒。

你可能感兴趣的:(android)