Hi,

We are trying to capture images using phone's camera, which is HTC Hero in this 
case, using the Camera class in Android. However, we haven't been able to let 
the phone's camera to autofocus on any object it is pointing at for some 
reason. Has anybody experienced a similar problem before? Any suggestions on 
how to fix it ?

The code is pasted below in case you'd like to have a look at it. In short, 
here's what we have in the AndroidManifest file:

 

...
 
<activity android:name=".CameraActivity" android:launchMode="standard"/>
 
...
 

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

<uses-sdk android:minSdkVersion="3" />


 

What is the proper way of using the AutoFocusCallback class ? Any ideas ?


Cheers,

Emre


***************************************************************************

 

package ...;

 

import ...;

 

public class CameraActivity extends Activity  implements SurfaceHolder.Callback{
 private SurfaceHolder mHolder;
    private Camera mCamera;
    private SurfaceView mSurfaceView;
    private boolean isFocused = false;
    
        
    @Override
 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        // Hide the window title.
        requestWindowFeature(Window.FEATURE_NO_TITLE);
    
        // Create the preview view and set it as the content of the activity.
        setContentView(R.layout.surfaceview);

  mSurfaceView = (SurfaceView)findViewById(R.id.surface);  
  mHolder = mSurfaceView.getHolder();  
  mHolder.addCallback(this);  
  mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 
    }

 

 @Override  
 protected void onRestoreInstanceState(Bundle savedInstanceState)  {  
  super.onRestoreInstanceState(savedInstanceState);  
 }
 
 @Override 
 protected void onResume()  {  
  Log.e(getClass().getSimpleName(), "onResume");  
  super.onResume();  
 }  
   
 @Override 
 protected void onSaveInstanceState(Bundle outState)  {  
  super.onSaveInstanceState(outState);  
 }  
 
 @Override 
 protected void onStop()  {  
  Log.e(getClass().getSimpleName(), "onStop");  
  super.onStop();  
 }  


    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, acquire the camera and tell it where
        // to draw.
        mCamera = Camera.open();
        try {
           mCamera.setPreviewDisplay(holder);
        } catch (IOException exception) {
            mCamera.release();
            mCamera = null;
        }
    }

    public void surfaceDestroyed(SurfaceHolder holder) {
        // Surface will be destroyed when we return, so stop the preview.
        // Since the CameraDevice object is not a shared resource, it's very
        // important to release it when the activity is paused.
        mCamera.stopPreview();
        mCamera.release();
        mCamera = null;
    }

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // Now that the size is known, set up the camera parameters and begin
        // the preview.
        Camera.Parameters parameters = mCamera.getParameters();
        parameters.setPreviewSize(w, h);
        parameters.setPreviewFormat(PixelFormat.JPEG);
        mCamera.setParameters(parameters);
        mCamera.startPreview();
    }
    
 private class autoFocusCallback implements AutoFocusCallback {
  public void onAutoFocus(boolean focused, Camera camera) {
   isFocused = focused;
   Log.d("auto focus", "isFocused = " + isFocused);
  }
  
 }
  
 Camera.PictureCallback mPictureCallback = new Camera.PictureCallback () {
  public void onPictureTaken(byte [] data, Camera camera) {
   //mCamera.startPreview();
  }
 };
 
 Camera.ShutterCallback mShutterCallback = new Camera.ShutterCallback() {  
  public void onShutter() {  
   
  }  
 };  

 private class ImageCaptureCallback implements PictureCallback {
  private OutputStream os;
  private String filename;
  
  public ImageCaptureCallback(OutputStream os, String filename){
   this.os = os;
   this.filename = filename;  
  }
  
  @Override
  public void onPictureTaken(byte[] data, Camera camera) { 
         try { 
          
                Log.v(getClass().getSimpleName(), "onPictureTaken=" + data + " 
length = " + data.length); 
                this.os.write(data);
             this.os.flush();          
         } catch(Exception ex) { 
             ex.printStackTrace(); 
         } finally {
          if (this.os != null)
           try{
            this.os.close();
           } catch (Exception ex) {
            ex.printStackTrace();
            Log.d(getClass().getSimpleName(), "*** Error while taking photo!!");
           }
         }
         
        
         isFocused = false;

         Bundle bundle = new Bundle();
         
         if (this.filename != null) {
          bundle.putString("uri", this.filename);
          Log.d("On Picture Taken", "filename = " + this.filename);
          Intent intent = new Intent();
          intent.putExtras(bundle);
          setResult(RESULT_OK, intent);
          finish();
         }
         
  }
 }
 
 @Override 
 public boolean onKeyDown(int keyCode, KeyEvent event)  {
  
  if (keyCode == KeyEvent.KEYCODE_BACK) {  
   setResult(RESULT_CANCELED);
   return super.onKeyDown(keyCode, event);  
  }  
    
  if (keyCode == KeyEvent.KEYCODE_DPAD_CENTER || keyCode == 
KeyEvent.KEYCODE_CAMERA) {  
   ImageCaptureCallback imageCaptureCallback = null;  
    try { 
     String filename = "sample1.jpg";
     ContentValues values = new ContentValues();  
     values.put(MediaStore.MediaColumns.DISPLAY_NAME, filename); 
     
     values.put(MediaStore.Images.ImageColumns.DESCRIPTION, "Image capture by 
camera");  
     Uri uri = getContentResolver().insert(Media.EXTERNAL_CONTENT_URI, values); 
     
     imageCaptureCallback = new 
ImageCaptureCallback(getContentResolver().openOutputStream(uri), 
uri.toString());  
    } catch(Exception ex ){  
     ex.printStackTrace();  
     Log.e(getClass().getSimpleName(), ex.getMessage(), ex);  
    } 
    
    mCamera.takePicture(mShutterCallback, mPictureCallback, 
imageCaptureCallback); 
    
    return true;

  }  
    
  return false;  
 }

}

 

 
                                          
_________________________________________________________________
MSN Dating: Find someone special. Start now.
http://go.microsoft.com/?linkid=9729707

-- 
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

Reply via email to