J.M*_*.M. 4 camera android camera2 android-camera2
我正在创建一个带有自定义相机的Android应用程序,我正在切换到新的camera2 API.我有一个按钮,可以在后置摄像头开启时打开和关闭闪光灯(不停止摄像头,就像任何经典的相机应用程序一样).
当我点击闪存图标时,没有任何反应,这就是logcat返回的内容:
D/ViewRootImpl: ViewPostImeInputStage processPointer 0
D/ViewRootImpl: ViewPostImeInputStage processPointer 1
Run Code Online (Sandbox Code Playgroud)
我不知道为什么它不起作用.这是代码:
我有一个RecordVideoActivity使用RecordVideoFragment.这是片段的XML部分,其中包含flash按钮代码:
<ImageButton
android:id="@+id/button_flash"
android:src="@drawable/ic_flash_off"
android:layout_alignParentLeft="true"
style="@style/actions_icons_camera"
android:onClick="actionFlash"/>
Run Code Online (Sandbox Code Playgroud)
和Java代码:
ImageButton flashButton;
private boolean hasFlash;
private boolean isFlashOn = false;
Run Code Online (Sandbox Code Playgroud)
随着onViewCreated:
@Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
...
[some code]
...
// Flash on/off button
flashButton = (ImageButton) view.findViewById(R.id.button_flash);
// Listener for Flash on/off button
flashButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
actionFlash();
}
});
Run Code Online (Sandbox Code Playgroud)
这是actionFlash()函数定义:
private void actionFlash() {
/* First check if device is supporting flashlight or not */
hasFlash = getActivity().getApplicationContext().getPackageManager()
.hasSystemFeature(PackageManager.FEATURE_CAMERA_FLASH);
if (!hasFlash) {
// device doesn't support flash
// Show alert message and close the application
AlertDialog alert = new AlertDialog.Builder(this.getActivity())
.create();
alert.setMessage("Sorry, your device doesn't support flash light!");
alert.setButton(DialogInterface.BUTTON_POSITIVE, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
alert.show();
return;
}
else { // the device support flash
CameraManager mCameraManager = (CameraManager) getActivity().getSystemService(Context.CAMERA_SERVICE);
try {
String mCameraId = mCameraManager.getCameraIdList()[0];
if (mCameraId.equals("1")) { // currently on back camera
if (!isFlashOn) { // if flash light was OFF
// Turn ON flash light
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mCameraManager.setTorchMode(mCameraId, true);
}
} catch (Exception e) {
e.printStackTrace();
}
// Change isFlashOn boolean value
isFlashOn = true;
// Change button icon
flashButton.setImageResource(R.drawable.ic_flash_off);
} else { // if flash light was ON
// Turn OFF flash light
try {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
mCameraManager.setTorchMode(mCameraId, false);
}
} catch (Exception e) {
e.printStackTrace();
}
// Change isFlashOn boolean value
isFlashOn = false;
// Change button icon
flashButton.setImageResource(R.drawable.ic_flash_on);
}
}
} catch (CameraAccessException e) {
Toast.makeText(getActivity(), "Cannot access the camera.", Toast.LENGTH_SHORT).show();
getActivity().finish();
}
}
}
Run Code Online (Sandbox Code Playgroud)
知道什么可能是错的吗?
(我已经看过这个问题,但它没有解决我的问题)
非常感谢您的帮助.这真让我抓狂.
MrO*_*zko 14
创建这些变量:
public static final String CAMERA_FRONT = "1";
public static final String CAMERA_BACK = "0";
private String cameraId = CAMERA_BACK;
private boolean isFlashSupported;
private boolean isTorchOn;
Run Code Online (Sandbox Code Playgroud)
然后添加这些方法:
public void switchFlash() {
try {
if (cameraId.equals(CAMERA_BACK)) {
if (isFlashSupported) {
if (isTorchOn) {
mPreviewBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_OFF);
mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, null);
flashButton.setImageResource(R.drawable.ic_flash_off);
isTorchOn = false;
} else {
mPreviewBuilder.set(CaptureRequest.FLASH_MODE, CaptureRequest.FLASH_MODE_TORCH);
mPreviewSession.setRepeatingRequest(mPreviewBuilder.build(), null, null);
flashButton.setImageResource(R.drawable.ic_flash_on);
isTorchOn = true;
}
}
}
} catch (CameraAccessException e) {
e.printStackTrace();
}
}
public void setupFlashButton() {
if (cameraId.equals(CAMERA_BACK) && isFlashSupported) {
flashButton.setVisibility(View.VISIBLE);
if (isTorchOn) {
flashButton.setImageResource(R.drawable.ic_flash_off);
} else {
flashButton.setImageResource(R.drawable.ic_flash_on);
}
} else {
flashButton.setVisibility(View.GONE);
}
}
Run Code Online (Sandbox Code Playgroud)
在此行之后添加此代码:
Boolean available = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
isFlashSupported = available == null ? false : available;
setupFlashButton();
Run Code Online (Sandbox Code Playgroud)
最后在你想要的点击监听器中调用switchFlash().
etvoilà!
PS.这可能会有所帮助 - 前/后摄像头切换器
| 归档时间: |
|
| 查看次数: |
11552 次 |
| 最近记录: |