CameraFlash手电筒
有时候晚上找不到电棒,电灯,咱们可以写个小程序,利用照相机的闪光灯临时顶替
上代码:
1 package com.linux.cameraflash; 2 3 import android.hardware.Camera; 4 import android.hardware.Camera.Parameters; 5 import android.os.Bundle; 6 import android.app.Activity; 7 import android.view.Menu; 8 import android.view.View; 9 import android.widget.TextView; 10 import android.widget.Toast; 11 12 public class MainActivity extends Activity { 13 private boolean isOpen=false; 14 private Camera camera; 15 @Override 16 protected void onCreate(Bundle savedInstanceState) { 17 super.onCreate(savedInstanceState); 18 setContentView(R.layout.activity_main); 19 TextView img=(TextView)findViewById(R.id.img); 20 img.setOnClickListener(new View.OnClickListener() { 21 //TextView上放一张图片,点击图片打开闪光灯 22 @Override 23 public void onClick(View arg0) { 24 // TODO Auto-generated method stub 25 if(!isOpen){ 26 Toast.makeText(getApplicationContext(), 27 "您已打开了手电筒", Toast.LENGTH_LONG).show(); 28 camera=Camera.open(); 29 Parameters params=camera.getParameters(); 30 params.setFlashMode(Parameters.FLASH_MODE_TORCH); 31 camera.setParameters(params); 32 camera.startPreview();//start flash! 33 isOpen=true; 34 }else{ 35 Toast.makeText(getApplicationContext(), 36 "已关闭", Toast.LENGTH_SHORT).show(); 37 camera.stopPreview();//关掉闪光灯 38 camera.release();//关掉照相机 39 isOpen=false; 40 } 41 } 42 }); 43 } 44 45 @Override 46 public boolean onCreateOptionsMenu(Menu menu) { 47 // Inflate the menu; this adds items to the action bar if it is present. 48 //getMenuInflater().inflate(R.menu.main, menu); 49 return true; 50 } 51 52 }
再在manifest中加入权限:
<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.autofocus"/>
<uses-feature
android:name="android.hardware.flash"/>
上述代码在我的4.2系统上运行良好,不知在其他厂商手机上怎么样
本文源自http://www.cnblogs.com/makefile/