0

我正在制作一个手电筒应用程序。该应用程序具有从后置摄像头闪光灯切换到前置摄像头闪光灯的选项。问题是,手电筒打开时,如果我将后置摄像头切换到前面,前置摄像头会闪烁不到一秒钟然后关闭。有时这不会发生。所以,这是一个错误。我希望该应用程序在切换相机后检查手电筒是否仍然亮着。我想我必须使用火炬回调。但是谷歌的文档描述性不够,无法理解。另外,关于这个的讨论也不多。所以,我不知道如何使用火炬回调。我试过了,我得到了错误......我一定是尝试了一些错误。这是我的代码: package com.danielfernandzz.flashlight;

import android.content.Context;
import android.hardware.camera2.CameraManager;
import android.hardware.camera2.CameraAccessException;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.Switch;
public class MainActivity extends AppCompatActivity {
public boolean switchState = false;
Button switchbutton;
String cameraId;
CameraManager camManager;
Switch frontToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.activity_main);
    switchbutton = (Button) findViewById(R.id.Switch);
    camManager = (CameraManager) getSystemService(Context.CAMERA_SERVICE);
    frontToggle = (Switch) findViewById(R.id.frontToggle);
    try{cameraId = camManager.getCameraIdList()[0];}
    catch (CameraAccessException ex){finish();}
}

public void switched(View v){
    if(!switchState){
        switchState = true;
        switchbutton.setText("ON");
        try {camManager.setTorchMode(cameraId,true);}
        catch (CameraAccessException ex) {finish();}
    }
    else{
        switchState = false;
        switchbutton.setText("OFF");
        try {camManager.setTorchMode(cameraId,false);}
        catch (CameraAccessException ex) {finish();}
    }
}
public void frontFlash(View z){
    if (frontToggle.isChecked()){
        if(switchState) {
            try {camManager.setTorchMode(cameraId, false);}
            catch (CameraAccessException ex) {finish();}
        }
        try{cameraId = camManager.getCameraIdList()[1];}
        catch (CameraAccessException ex){finish();}
        if(switchState) {
            try {camManager.setTorchMode(cameraId, true);}
            catch (CameraAccessException ex) {finish();}
        }
        //I want to add the callback to take place here
    }
    else {
        if(switchState) {
            try {camManager.setTorchMode(cameraId, false);}
            catch (CameraAccessException ex) {finish();}
        }
        try{cameraId = camManager.getCameraIdList()[0];}
        catch (CameraAccessException ex){finish();}
        if(switchState) {
            try {camManager.setTorchMode(cameraId, true);}
            catch (CameraAccessException ex) {finish();}
        }
        //And here
    }
}

}

任何帮助,将不胜感激。如果您认为错误是由我的代码中的错误引起的,那么也请告诉我。任何帮助,将不胜感激。

4

0 回答 0