Tim
Tim

Reputation: 5767

HTC One (M8) FOCUS_MODE_CONTINUOUS_PICTURE not working

I can't get my HTC M8 rear facing camera to autofocus. There is nothing wrong with the hardware in that the stock camera will focus perfectly (and there is no dirt on the lens).

Initially I thought I might have been something wrong in my code but then I tried Commonsware's camera app/library and I get the same result - an image which is blurry from the start which makes no effort to focus. I've tried the same code on a Nexus 4 and Galaxy 3 and they work perfectly. I've also done a params.getSupportedFocusModes().contains(Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); test on the M8 and it returns true.

I tried mCamera.autoFocus(...) also and it didn't work either so I am kinda out of ideas.

The front facing camera works fine for autofocus with Commonsware's camera also. Instagram's app also works fine so they are doing something special to get it to work.

EDIT Just tried the same code on a friends HTC One Dual Sim and the focus works fine. I also did a factory reset with the M8 and its still doesn't work.

Upvotes: 1

Views: 755

Answers (2)

shield
shield

Reputation: 139

I had a similar issue. My code was working fine on several devices, but not on a LG G6 (the autofocus was never triggered on that device).

I compared the result of Camera.getParameters() between my code, and the code from zxing (CameraManager.java) which was working fine on all the devices I tested it with. The only difference was the value of the "scene-mode" attribute: it was set to "auto" when continuous focus was working fine, and "steadyphoto" in my case.

To fix the problem with my LG G6 phone I added a call to:

parameters.setSceneMode( Camera.Parameters.SCENE_MODE_AUTO );

Note that you must make sure this mode is supported, and that you should set the scene mode before setting other parameters as it may reset some of them (like the focus mode, for example).

Upvotes: 1

Ted Yu
Ted Yu

Reputation: 304

Hi I have the same problem on auto focus with HTC M8. My code work fine on HTC m7 and other devices.

I have change the code as following

import android.hardware.Camera;
import android.hardware.Sensor;
import android.hardware.SensorEvent;
import android.hardware.SensorEventListener;
import android.hardware.SensorManager;
import android.hardware.Camera.AutoFocusCallback;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.Window;
import android.view.WindowManager;

public class Main extends Activity implements SurfaceHolder.Callback, SensorEventListener, AutoFocusCallback {
Camera mCamera;
SurfaceView mPreview;
Sensor mAccelerometer;
SensorManager mSensorManager;

float motionX = 0;
float motionY = 0;
float motionZ = 0;

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN 
            | WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
    setContentView(R.layout.main);

    mSensorManager = (SensorManager)getSystemService(SENSOR_SERVICE);
    mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);

    mPreview = (SurfaceView)findViewById(R.id.preview);
    mPreview.getHolder().addCallback(this);
    mPreview.getHolder().setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
}

public void onResume() {
    Log.d("System","onResume");
    super.onResume();
    mCamera = Camera.open();
    mSensorManager.registerListener(this, mAccelerometer
            , SensorManager.SENSOR_DELAY_NORMAL);
}

public void onPause() {
    Log.d("System","onPause");
    super.onPause();
    mCamera.release();
    mSensorManager.unregisterListener(this, mAccelerometer);
}

public void onAccuracyChanged(Sensor arg0, int arg1) { }

public void onSensorChanged(SensorEvent event) {
    if(Math.abs(event.values[0] - motionX) > 0.2 
        || Math.abs(event.values[1] - motionY) > 0.2 
        || Math.abs(event.values[2] - motionZ) > 0.2 ) {
        Log.d("Camera System", "Refocus");
        try {
            mCamera.autoFocus(this);
        } catch (RuntimeException e) { }
    }

    motionX = event.values[0];
    motionY = event.values[1];
    motionZ = event.values[2];
}

public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
    Log.d("CameraSystem","surfaceChanged");
    Camera.Parameters params = mCamera.getParameters();
    List<Camera.Size> previewSize = params.getSupportedPreviewSizes();
    List<Camera.Size> pictureSize = params.getSupportedPictureSizes();
    params.setPictureSize(pictureSize.get(0).width, pictureSize.get(0).height);
    params.setPreviewSize(previewSize.get(0).width, previewSize.get(0).height);
    params.setJpegQuality(100);
    mCamera.setParameters(params);

    try {
        mCamera.setPreviewDisplay(mPreview.getHolder());
        mCamera.startPreview();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

public void surfaceCreated(SurfaceHolder arg0) { }

public void surfaceDestroyed(SurfaceHolder arg0) { }

public void onAutoFocus(boolean success, Camera camera) {
    Log.d("CameraSystem","onAutoFocus");
}
}

And the xml as the following :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<SurfaceView
    android:id="@+id/preview"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" />

</RelativeLayout>

Hope this help you

Upvotes: -1

Related Questions