zoukankan      html  css  js  c++  java
  • 使用相机闪光灯开启

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


     

    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    感知器算法--python实现
    PHP appserv + ZendStudio12.5.1 + 注册码
    第九周-每周例行报告
    第八周-每周例行报告
    感谢THUNDER
    第七周-每周例行报告
    第六周-每周例行报告
    第五周-每周例行报告
    第四周-四则运算试题生成
    第四周-单元测试
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4658913.html
Copyright © 2011-2022 走看看