首页 > 代码库 > 使用照相机开启闪光灯
使用照相机开启闪光灯
package com.home.light;import android.app.Activity;import android.hardware.Camera;import android.hardware.Camera.Parameters;import android.os.Bundle;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import com.home.cameralight.R;public class TestActivity extends Activity implements OnClickListener { Camera camera; Parameters params; private Button onBtn; private Button offBtn; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); camera = Camera.open(); params = camera.getParameters(); findView(); } private void findView() { onBtn = (Button) findViewById(R.id.main_btn_on); onBtn.setOnClickListener(this); offBtn = (Button) findViewById(R.id.main_btn_off); offBtn.setOnClickListener(this); } @Override public void onClick(View v) { if (v == onBtn) {// open1(); open2(); } else if (v == offBtn) {// off1(); off2(); } } private void open1() { if (camera == null) { camera = Camera.open(); params = camera.getParameters(); } params.setFlashMode(Parameters.FLASH_MODE_TORCH); camera.setParameters(params); } private void off1() { if (camera == null) { return; } params.setFlashMode(Parameters.FLASH_MODE_OFF); camera.setParameters(params); camera.release(); camera = null; } private void open2() { if (camera == null) { camera = Camera.open(); params = camera.getParameters(); } params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH); camera.setParameters(params); camera.autoFocus(new Camera.AutoFocusCallback() { public void onAutoFocus(boolean success, Camera camera) { } }); camera.startPreview(); } private void off2() { if (camera == null) { return; } camera.stopPreview(); camera.release(); camera = null; }}
权限:
<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"/>
使用照相机开启闪光灯
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。