📅  最后修改于: 2023-12-03 15:09:10.766000             🧑  作者: Mango
制作一个简单的Flashlight TorchLight Android应用程序很简单,只需要遵循以下步骤:
要制作这个应用程序,您需要了解一些基本的Java和Android开发知识,以及以下工具:
private Camera camera;
private boolean isFlashOn;
camera = Camera.open();
isFlashOn = false;
toggleButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
if (isFlashOn) {
turnOffFlash();
isFlashOn = false;
} else {
turnOnFlash();
isFlashOn = true;
}
}
});
private void turnOnFlash() {
Camera.Parameters params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
}
private void turnOffFlash() {
Camera.Parameters params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
}
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.flash" />
您可以运行应用程序并测试它是否正常工作。手电筒应该在用户开启开关之后点亮,并在关闭之后关闭。
public class MainActivity extends AppCompatActivity {
private Camera camera;
private boolean isFlashOn;
private boolean hasFlash;
Parameters params;
ImageButton btnOnOff;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// initialize camera
hasFlash = getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
// redirecting to MainActivity screen because flashlight not supported
Toast.makeText(MainActivity.this, "Flash not supported on your Camera", Toast.LENGTH_LONG).show();
finish();
}
getCamera();
// Switch button click event handle
btnOnOff.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isFlashOn) {
// turn off flash
turnOffFlash();
} else {
// turn on flash
turnOnFlash();
}
}
});
}
/**
* Get the camera
*/
private void getCamera() {
if (camera == null) {
try {
camera = Camera.open();
params = camera.getParameters();
} catch (RuntimeException e) {
Log.e("Camera Error. Failed to Open. Error: ", e.getMessage());
}
}
}
/**
* Turning On flash
*/
private void turnOnFlash() {
if (!isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
playSound();
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
camera.setParameters(params);
camera.startPreview();
isFlashOn = true;
// changing button/switch image
toggleButtonImage();
}
}
/**
* Turning Off flash
*/
private void turnOffFlash() {
if (isFlashOn) {
if (camera == null || params == null) {
return;
}
// play sound
playSound();
params = camera.getParameters();
params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
camera.setParameters(params);
camera.stopPreview();
isFlashOn = false;
// changing button/switch image
toggleButtonImage();
}
}
/**
* Playing sound
* will play button toggle sound on flash on / off
* */
private void playSound(){
if(isFlashOn){
mp = MediaPlayer.create(MainActivity.this, R.raw.flash_off);
}else{
mp = MediaPlayer.create(MainActivity.this, R.raw.flash_on);
}
mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
@Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
mp.start();
}
/*
* Toggle switch button images
* changing image states to on / off
* */
private void toggleButtonImage(){
if(isFlashOn){
btnOnOff.setImageResource(R.drawable.btn_switch_on);
}else{
btnOnOff.setImageResource(R.drawable.btn_switch_off);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
}
@Override
protected void onPause() {
super.onPause();
// on pause turn off the flash
turnOffFlash();
}
@Override
protected void onRestart() {
super.onRestart();
}
@Override
protected void onResume() {
super.onResume();
// on resume turn on the flash
if(hasFlash)
turnOnFlash();
}
@Override
protected void onStart() {
super.onStart();
// on starting the app get the camera params
getCamera();
}
@Override
protected void onStop() {
super.onStop();
// on stop release the camera
if (camera != null) {
camera.release();
camera = null;
}
}
}