手电筒碎片 Android 工作室

Flashlight Fragment Android Studio

我为我的应用程序创建了一个手电筒片段,我已经实现了这段代码并打开了手电筒,但我无法将其关闭。

我尝试用一​​个按钮或像现在这样用两个按钮打开和关闭灯,但没有任何区别。

当我切换片段时,灯熄灭了。

我希望任何人都知道该怎么做。 谢谢

 public class Flashlight extends Fragment implements SurfaceHolder.Callback {
    private View view;
    Button button_On, button_Off;
    private boolean lightIsOn = false;
    Camera camera;
    android.hardware.Camera.Parameters parameters;

    @Override
    public void onStart() {
        super.onStart();
        SurfaceView preview = (SurfaceView) getView().findViewById(R.id.PREVIEW);
        SurfaceHolder mHolder = preview.getHolder();
        mHolder.addCallback(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        turnOffLight();
    }

    @Override
    public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
                             final Bundle savedInstanceState) {

      view = inflater.inflate(R.layout.fragment_flashlight, container, false);
      button_On = (Button) view.findViewById(R.id.button_On);
      button_Off = (Button) view.findViewById(R.id.button_Off);
      if (ContextCompat.checkSelfPermission(getActivity(), Manifest.permission.CAMERA)
                != PackageManager.PERMISSION_GRANTED) {
         requestPermissions(new String[]{Manifest.permission.CAMERA,}, PackageManager.PERMISSION_GRANTED);
      }
      if (!getActivity().getApplicationContext().getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH)) {
         Toast.makeText(getActivity(), "Device has no flashlight", Toast.LENGTH_SHORT).show();
      }


      button_On.setOnClickListener(new View.OnClickListener() {
      @Override
          public void onClick(View v) {
              turnOnLight();
          }
      });
      button_Off.setOnClickListener(new View.OnClickListener() {
      @Override
         public void onClick(View v) {
              turnOffLight();
         }
      });
        return view;
    }

转On/Off灯:

private void turnOnLight() {
 if (!lightIsOn) {
  if (camera == null || parameters == null) {
            return;
  }
  parameters = camera.getParameters();
  parameters.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
  camera.setParameters(parameters);
  camera.startPreview();
  lightIsOn = true;
 }
}

private void turnOffLight() {

    if (lightIsOn) {
        if (camera == null || parameters == null) {
            return;
        }

        parameters = camera.getParameters();
        parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        camera.setParameters(parameters);
        camera.stopPreview();
        lightIsOn = false;
    }
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {

}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
    if (camera != null) {
        camera.stopPreview();
        camera.setPreviewCallback(null);
        camera.release();
        camera = null;
    }
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
    if (camera == null) {
        camera = Camera.open();
        parameters = camera.getParameters();
        try {
            camera.setPreviewDisplay(holder);
        } catch (IOException e) {
            camera.release();
            camera = null;
        }
    }
  }
}

清单:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-permission android:name="android.permission.FLASHLIGHT" />

XML:

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Flashlight">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <SurfaceView
            android:id="@+id/PREVIEW"
            android:layout_width="1dp"
            android:layout_height="1dp"
            android:layout_marginBottom="730dp"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

            <Button
                android:id="@+id/button_On"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="on" />

            <Button
                android:id="@+id/button_Off"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:text="off" />

        </LinearLayout>


    </androidx.constraintlayout.widget.ConstraintLayout>
</FrameLayout>

好的,如下更新清单文件中的权限:

<permission android:name="android.permission.FLASHLIGHT"
             android:permissionGroup="android.permission-group.HARDWARE_CONTROLS"
             android:protectionLevel="normal"
             android:label="@string/permlab_flashlight"
             android:description="@string/permdesc_flashlight" />

尝试使用以下代码关闭灯:

cam.stopPreview();
cam.release(); // Very Important line

在哪一行?

private void turnOffLight() {

        if (lightIsOn) {
            if (camera == null || parameters == null) {
                return;
            }
            camera = Camera.open();

            parameters = camera.getParameters();
            parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
            camera.setParameters(parameters);
            camera.stopPreview();
            camera.release(); // Very Important line
            lightIsOn = false;
            button_OnOff.setText("On");

        }
    }