简体   繁体   English

Android手电筒代码不起作用

[英]Android flashlight code not working

I created a simple project to play with flashlight feature. 我创建了一个简单的项目以使用手电筒功能。 From many different places (which all do it basically the same way), I have assembled the following code, but the flashlight will not turn on (No exceptions are raised) - but it works with the camera when I take a photo: 我从许多不同的地方(基本上都以相同的方式进行操作)汇编了以下代码, 但是手电筒无法打开(不引发任何异常) -但是当我拍照时,它可以与相机配合使用:

Example code 范例程式码

Example code 2 示例代码2

Example Code 3 示例代码3

    @Override
    protected void onResume() {
        super.onResume();

        try {


            // check flashlight support
            hasFlash = 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(MainActivity.this).create();
                alert.setTitle("Error");
                alert.setMessage("Sorry, your device doesn't support flash light!");
                alert.setButton("OK", new DialogInterface.OnClickListener() {
                    public void onClick(DialogInterface dialog, int which) {

                        finish();
                    }
                });
                alert.show();
                return;
            }

            text_off = (TextView) findViewById(R.id.text_off);
            text_off.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    finish();
                    return;
                }
            });


            getCamera();
            turnOnFlash();

        } catch (Exception e) {
            Log.e(this.getClass().getSimpleName(), "onResume Error: "+e.getMessage());
        }

    }

    @Override
    protected void onPause() {      
        super.onPause();

        turnOffFlash();

    }






    private void getCamera() {
        if (camera == null) {
            try {
                camera = Camera.open();
                params = camera.getParameters();
            } catch (RuntimeException e) {
                Log.e(this.getClass().getSimpleName(), "Camera Error. Failed to Open. Error: "+e.getMessage());
            }
        }   
    }

    @SuppressWarnings("deprecation")
    private void turnOnFlash() {

        try {
            Log.d(this.getClass().getSimpleName(), "turnOnFlash CHECKPOINT ");


            if (camera == null || params == null) {
                return;
            }


            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_ON); 
            // ALSO TRIED: params.setFlashMode(Parameters.FLASH_MODE_TORCH); 
            camera.setParameters(params);
            camera.startPreview();
            isFlashOn = true;

            Log.d(this.getClass().getSimpleName(), "turnOnFlash CHECKPOINT EXIT");

        } catch (Exception e) {
            Log.e(this.getClass().getSimpleName(), "Camera Error. Failed to Open. Error: "+e.getMessage());
        }

    }


    @SuppressWarnings("deprecation")
    private void turnOffFlash() {

        try {

            Log.d(this.getClass().getSimpleName(), "turnOffFlash CHECKPOINT ");

            if (camera == null || params == null) {
                return;
            }

            params = camera.getParameters();
            params.setFlashMode(Parameters.FLASH_MODE_OFF);
            camera.setParameters(params);
            camera.stopPreview();
            camera.release();
            isFlashOn = false;

        } catch (Exception e) {
            Log.e(this.getClass().getSimpleName(), "Camera Error. Failed to Open. Error: "+e.getMessage());
        }

    }

In manifest: 在清单中:

<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />

Why doesn't this code turn the flashlight on/off? 为什么此代码不打开/关闭手电筒?

Hardware: Nexus 5 硬件:Nexus 5

OS: Android Marshmallow 操作系统:Android Marshmallow

I think you are missing one additional line of code, which is needed for some devices. 我认为您缺少一些设备需要的另一行代码。 These 3 lines are whats needed on all devices I encountered so far: 这三行是到目前为止我在所有设备上需要的:

// will work on some devices
mCamera.setParameters(parameters);
// Needed for some devices.
mCamera.setPreviewTexture(new SurfaceTexture(0));
// Needed for some more devices.
mCamera.startPreview();

If you add some dummy SurfaceTexture it should work. 如果添加一些虚拟的SurfaceTexture它将正常工作。 Also you can see a full code sample here . 您也可以在这里看到完整的代码示例。

The camera api varies highly between phones and those phone details are poorly documented. 相机api在不同手机之间差异很大,而这些手机的详细信息记录得很少。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM