Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

LED flashlight on xperia Z5

I am using following code to turn LED flashlight on and off:

public Flashlight(SurfaceView preview, Context context){
        this.preview = preview;
        this.context = context;
        mHolder = preview.getHolder(); //mHolder is surfaceHolder
        mHolder.addCallback(this);
        try {
            mCamera = Camera.open();
            params = mCamera.getParameters();
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

            //AUTOFOCUS LASER FIX ON LG G3
            List<String> focusModes = params.getSupportedFocusModes();
            if (focusModes.contains(params.FOCUS_MODE_INFINITY)) {
                params.setFocusMode(params.FOCUS_MODE_INFINITY);
            }
            else{
                if (focusModes.contains(params.FOCUS_MODE_FIXED))
                    params.setFocusMode(params.FOCUS_MODE_FIXED);
            }
            mCamera.setParameters(params);
            cameraOpened = true;
        }catch (Exception e){
            cameraOpened = false;
            e.printStackTrace();
        }
    }
    private void turnOnFlashlight(){
        flashlightOn = true;
        params.setFlashMode(Camera.Parameters.FLASH_MODE_TORCH);
        mCamera.setParameters(params);
    }
    private void turnOffFlashlight(){
        flashlightOn = false;
        params.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);
        mCamera.setParameters(params);
    }

It works great on most phones but I cant get it working on xperia Z5. I dont have Z5 for testing so I only know it from user response. So I would like to ask if there is any other (preferably working) way to turn on flashlight on Xperia Z5.

Thanks in forward

like image 992
horin Avatar asked Oct 30 '22 16:10

horin


1 Answers

As already mentioned in the comment, I found there to be 3 steps of making the flash appear (seems to be working on all devices so far)

cam.setParameters(p); // will trigger flash on most devices
// Needed for some devices.
cam.setPreviewTexture(new SurfaceTexture(0));
// Needed for some more devices.
cam.startPreview();

Since you did 2 of those, try adding the PreviewTexture and it should work. The whole code of a working flashlight can be found here at Flashlight Widget

like image 93
David Medenjak Avatar answered Nov 17 '22 09:11

David Medenjak