我正在使用SurfaceView和CameraSource访问相机,并在屏幕上显示图像。该应用程序还需要使用闪光灯。当我打开闪光灯时,相机视图会冻结。我不希望这种情况发生。我不明白为什么。闪光灯打开时,必须同时启动相机。例如,用手机上的闪光灯拍照。我正在使用以下代码。如果您能帮助或指出导致问题的原因,我们将不胜感激。
*我的代码正在运行。但是当我打开手电筒时,相机图像仍然保持附着状态。
MyActivity
public class MyActivity extends AppCompatActivity {
SurfaceView cameraPreview;
CameraSource cameraSource;
final int RequestCameraPermissionID = 1001;
ImageButton Flash_On_Button;
private Camera cam;
Camera.Parameters p;
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
switch (requestCode) {
case RequestCameraPermissionID: {
if (grantResults[0] == PackageManager.PERMISSION_GRANTED) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
return;
}
try {
cameraSource.start(cameraPreview.getHolder());
} catch (IOException e) {
e.printStackTrace();
}
}
}
break;
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
Flash_On_Button = findViewById(R.id.Flash_On_Button);
Flash_On_Button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
FlashON();
}
});
cameraPreview = findViewById(R.id.cameraPreview);
cameraSource = new CameraSource
.Builder(this, barcodeDetector)
.setAutoFocusEnabled(true)
.build();
CameraStart() ;
}
private void CameraStart() {
cameraPreview.getHolder().addCallback(new SurfaceHolder.Callback() {
@Override
public void surfaceCreated(SurfaceHolder surfaceHolder) {
if (ActivityCompat.checkSelfPermission(getApplicationContext(), android.Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(ScanActivity.this,
new String[]{Manifest.permission.CAMERA},RequestCameraPermissionID);
return;
}
try {
cameraSource.start(cameraPreview.getHolder());
Toast.makeText(ScanActivity.this, "surface Created", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void surfaceChanged(SurfaceHolder surfaceHolder, int i, int i1, int i2) {
Toast.makeText(ScanActivity.this, "surface Changed", Toast.LENGTH_SHORT).show();
}
@Override
public void surfaceDestroyed(SurfaceHolder surfaceHolder) {
cameraSource.stop();
Toast.makeText(ScanActivity.this, "surface Destroyed", Toast.LENGTH_SHORT).show();
}
});
}
public void FlashON(){
try {
if (getPackageManager().hasSystemFeature(
PackageManager.FEATURE_CAMERA_FLASH)) {
cam = Camera.open();
p = cam.getParameters();
p.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
}
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(getBaseContext(), "Exception flashLightOn()", Toast.LENGTH_SHORT).show();
}
}
}
答案 0 :(得分:1)
您可以尝试实现以下代码:
闪光灯的可用性
context.getPackageManager().hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
启用/禁用闪光灯,将其包含在Android清单中
<!-- Allows access to the flashlight -->
<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" />
打开相机手电筒
Camera cam = Camera.open();
Parameters p = cam.getParameters();
p.setFlashMode(Parameters.FLASH_MODE_TORCH);
cam.setParameters(p);
cam.startPreview();
关闭相机手电筒
cam.stopPreview();
cam.release();
完整的讨论可见here