相机不会为相机 2 开启 on/off 闪光灯
Camera won't turn on/off flash for camera2
我需要帮助为 camera2 转动 on/off 闪光灯。该代码无效,拍照时不会打开闪光灯。我尝试了很多不同的方法,但效果不佳。这是我的代码。请帮忙!
MainActivity.java
public class MainActivity extends AppCompatActivity {
private CameraCaptureSession mSession;
private CaptureRequest.Builder mBuilder;
private CameraDevice mCameraDevice;
private CameraManager mCameraManager;
Boolean isOn = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (null == savedInstanceState) {
getFragmentManager().beginTransaction()
.replace(R.id.container, Camera2BasicFragment.newInstance())
.commit();
}
final Button flashOn = (Button) findViewById(R.id.flah_off);
flashOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isOn) {
flashOn.setBackgroundResource(R.drawable.flash_on);
turnOnFlashLight();
}else{
flashOn.setBackgroundResource(R.drawable.flah_off);
turnOffFlashLight();
}
isOn = !isOn;
}
});
}
public void turnOnFlashLight() {
try {
mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
mSession.setRepeatingRequest(mBuilder.build(), null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
public void turnOffFlashLight() {
try {
mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
mSession.setRepeatingRequest(mBuilder.build(), null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
我在 Camera2 上为手电筒使用此功能
@TargetApi(Build.VERSION_CODES.M)
public void toggleMarshmallowFlashlight(boolean enable) {
try {
final CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
final String[] list = manager.getCameraIdList();
manager.setTorchMode(list[0], enable);
} catch (CameraAccessException e) {
}
}
我需要帮助为 camera2 转动 on/off 闪光灯。该代码无效,拍照时不会打开闪光灯。我尝试了很多不同的方法,但效果不佳。这是我的代码。请帮忙!
MainActivity.java
public class MainActivity extends AppCompatActivity {
private CameraCaptureSession mSession;
private CaptureRequest.Builder mBuilder;
private CameraDevice mCameraDevice;
private CameraManager mCameraManager;
Boolean isOn = false;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (null == savedInstanceState) {
getFragmentManager().beginTransaction()
.replace(R.id.container, Camera2BasicFragment.newInstance())
.commit();
}
final Button flashOn = (Button) findViewById(R.id.flah_off);
flashOn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isOn) {
flashOn.setBackgroundResource(R.drawable.flash_on);
turnOnFlashLight();
}else{
flashOn.setBackgroundResource(R.drawable.flah_off);
turnOffFlashLight();
}
isOn = !isOn;
}
});
}
public void turnOnFlashLight() {
try {
mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_TORCH);
mSession.setRepeatingRequest(mBuilder.build(), null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
public void turnOffFlashLight() {
try {
mBuilder.set(CaptureRequest.FLASH_MODE, CameraMetadata.FLASH_MODE_OFF);
mSession.setRepeatingRequest(mBuilder.build(), null, null);
} catch (Exception e) {
e.printStackTrace();
}
}
我在 Camera2 上为手电筒使用此功能
@TargetApi(Build.VERSION_CODES.M)
public void toggleMarshmallowFlashlight(boolean enable) {
try {
final CameraManager manager = (CameraManager) mContext.getSystemService(Context.CAMERA_SERVICE);
final String[] list = manager.getCameraIdList();
manager.setTorchMode(list[0], enable);
} catch (CameraAccessException e) {
}
}