Dylan_null的专栏

大神是从菜鸟开始的……

闪光灯

闪光灯主要是控制Camera 类的开关,来实现一闪一闪

代码如下:

import android.app.Activity;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.LinearLayout;

public class MainActivity extends Activity {
	private Camera camera = null;
	// 创建一个Handler对象
	Handler handler = new Handler();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		flashclose();
		camera.startPreview();
	}

	// 判断camera是否打开
	private boolean openCamera() {
		if (camera != null) {
			return true;
		}
		try {
			camera = Camera.open();
		} catch (Exception e) {
			return false;
		}
		return true;
	}

	// 关闭camera
	private void releaseCamera() {
		handler.removeCallbacks(mFlashOnRunnable);
		handler.removeCallbacks(mFlashOffRunnable);
		if (camera != null) {
			camera.stopPreview();
			camera.release();
			camera = null;
		}
	}

	// 开始闪光
	private void flashopen() {
		if (!openCamera()) {
			return;
		}
		Parameters parameters = camera.getParameters();
		parameters.setFlashMode(Parameters.FLASH_MODE_TORCH);
		camera.setParameters(parameters);
	}

	// 停止闪光
	private void flashclose() {
		if (!openCamera()) {
			return;
		}
		Parameters parameters = camera.getParameters();
		parameters.setFlashMode(Parameters.FLASH_MODE_OFF);
		camera.setParameters(parameters);
	}

	private Runnable mFlashOnRunnable = new Runnable() {
		@Override
		public void run() {
			flashopen();
			//闪光灯的频率(自己可以静态设置,也可以动态设置)
			int delayTime = getDelayTime();
			if (delayTime > 0) {
				handler.postDelayed(mFlashOffRunnable, delayTime);
			}
		}
	};

	private Runnable mFlashOffRunnable = new Runnable() {
		@Override
		public void run() {
			flashclose();
			handler.postDelayed(mFlashOnRunnable, getDelayTime());
		}
	};

	// 获的闪关的频率
	private int getDelayTime() {
		//这就是获取刻度尺的值,上篇文章有讲解
		return staff.getRate() * 100 + 100;
	}
}



阅读更多
个人分类: Android 工作总结
上一篇刻度尺
下一篇错误日志
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页

关闭
关闭