Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I had the same problem in one of my applications.</p> <p>My solution was to use a sensor listener and do auto focus when the user shook the device to some threshold. Here is the code.</p> <pre><code>public void setCameraFocus(AutoFocusCallback autoFocus){ if (mCamera.getParameters().getFocusMode().equals(mCamera.getParameters().FOCUS_MODE_AUTO) || mCamera.getParameters().getFocusMode().equals(mCamera.getParameters().FOCUS_MODE_MACRO)){ mCamera.autoFocus(autoFocus); } </code></pre> <p>}</p> <p>The callback for auto focus:</p> <pre><code>// this is the autofocus call back private AutoFocusCallback myAutoFocusCallback = new AutoFocusCallback(){ public void onAutoFocus(boolean autoFocusSuccess, Camera arg1) { //Wait.oneSec(); mAutoFocus = true; }}; </code></pre> <p>And the way to call the focus.</p> <pre><code>public void onSensorChanged(SensorEvent event) { if (mInvalidate == true){ mView.invalidate(); mInvalidate = false; } float x = event.values[0]; float y = event.values[1]; float z = event.values[2]; if (!mInitialized){ mLastX = x; mLastY = y; mLastZ = z; mInitialized = true; } float deltaX = Math.abs(mLastX - x); float deltaY = Math.abs(mLastY - y); float deltaZ = Math.abs(mLastZ - z); if (deltaX &gt; .5 &amp;&amp; mAutoFocus){ //AUTOFOCUS (while it is not autofocusing) mAutoFocus = false; mPreview.setCameraFocus(myAutoFocusCallback); } if (deltaY &gt; .5 &amp;&amp; mAutoFocus){ //AUTOFOCUS (while it is not autofocusing) mAutoFocus = false; mPreview.setCameraFocus(myAutoFocusCallback); } if (deltaZ &gt; .5 &amp;&amp; mAutoFocus){ //AUTOFOCUS (while it is not autofocusing) */ mAutoFocus = false; mPreview.setCameraFocus(myAutoFocusCallback); } mLastX = x; mLastY = y; mLastZ = z; </code></pre> <p>}</p> <p>You can see the complete project here: <a href="http://adblogcat.com/a-camera-preview-with-a-bounding-box-like-google-goggles/" rel="noreferrer">http://adblogcat.com/a-camera-preview-with-a-bounding-box-like-google-goggles/</a></p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload