gpt4 book ai didi

java - 手电筒 fragment Android Studio

转载 作者:行者123 更新时间:2023-11-30 05:03:05 27 4
gpt4 key购买 nike

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

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

当我切换 fragment 时,灯会熄灭。

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

 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;
}

打开/关闭灯:

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;
}
}
}
}

list :

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

最佳答案

好的,如下所示更新 list 文件中的权限:

<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

关于java - 手电筒 fragment Android Studio,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57875887/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com