java - 手电筒片段 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;
}
打开/关闭灯:
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
推荐阅读
- sql - 将 X 行转换为列用于计数 SQL
- ios - 包产品“MyLibrary”不能用作此目标的依赖项,因为它使用了不安全的构建标志
- java - 是否可以以编程方式指定 JNA 回调?
- xamarin.forms - 使用 Auth0 在 Xamarin 中进行用户管理
- hibernate - 休眠角色和权限
- excel - 努力在工作簿之间复制
- javascript - 实现innerHTML以使用表单显示输出
- excel - VBA 将复制的宏分配给目标工作簿而不是源工作簿
- python - 使用分配值的用户定义方法创建一个 numpy 数组
- reactjs - 使用firebase进行signInWithRedirect的正确语法是什么?