使用 Android camera2 API 打开/关闭闪光灯不起作用 [英] Turning on/off flash with Android camera2 API not working

查看:122
本文介绍了使用 Android camera2 API 打开/关闭闪光灯不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在创建一个带有自定义相机的 Android 应用程序,我正在切换到新的 camera2 API.我有一个按钮,可以在后置摄像头打开时打开和关闭闪光灯(无需停止相机,就像任何经典的相机应用程序一样).

I'm creating an Android app with a custom camera and I'm switching to the new camera2 API. I have a button allowing to turn ON and OFF the flash when the back camera is on (without stopping the camera, like any classic camera app).

当我点击 flash 图标时,没有任何反应,这是 logcat 返回的内容:

When I tap the flash icon, nothing happens and this is what the logcat returns:

D/ViewRootImpl: ViewPostImeInputStage processPointer 0
D/ViewRootImpl: ViewPostImeInputStage processPointer 1

我不知道为什么它不起作用.代码如下:

I don't know why it's not working. Here is the code:

我有一个使用 RecordVideoFragmentRecordVideoActivity.这是包含 flash 按钮代码的片段的 XML 部分:

I have a RecordVideoActivity using a RecordVideoFragment. Here is the fragment's XML part that contains the flash button code:

<ImageButton
    android:id="@+id/button_flash"
    android:src="@drawable/ic_flash_off"
    android:layout_alignParentLeft="true"
    style="@style/actions_icons_camera"
    android:onClick="actionFlash"/>

Java 代码:

ImageButton flashButton;
private boolean hasFlash;
private boolean isFlashOn = false;

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

这里是 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();
        }
    }
}

知道哪里出了问题吗?

(我已经看过这个问题但它没有解决我的问题)

(I already looked at this question but it doesn't address my problem)

非常感谢您的帮助.这让我发疯.

Thank you very much for your help. This is driving me crazy.

推荐答案

创建这些变量:

    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;

然后添加这些方法:

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

在此之后line 添加以下代码:

after this line add this code:

Boolean available = characteristics.get(CameraCharacteristics.FLASH_INFO_AVAILABLE);
isFlashSupported = available == null ? false : available;

setupFlashButton();

最后在您想要的点击监听器中调用 switchFlash().

at the end call switchFlash() in your desired click listener.

等等!

附注.这可能会有所帮助 - 前置/后置摄像头切换器

PS. This might be helpful - front/back camera switcher

这篇关于使用 Android camera2 API 打开/关闭闪光灯不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆