3

I am building a custom camera with auto focus, and was merely wondering if there is a way to invoke the same auto-focus rectangular indicator that the native camera has or if i have to build that from scratch.. any examples or tutorial links would be greatly appreciated.

erik
  • 4,946
  • 13
  • 70
  • 120

2 Answers2

11

It might be helpful to look at the way the most recent Jelly Bean 4.2 camera handles this. You can download the Camera source as follows:

git clone https://android.googlesource.com/platform/packages/apps/Camera.git

Once you have the code, navigate to the FocusOverlayManager class and the PieRenderer class. If you haven't tried out this newest version before, the focus meter is a pie-like circle that rotates upon focus completion. You can make your own square in photoshop or use one of these two that I have used in the past (one is an iPhone ripoff I made and the other is a nine-patch used in some version of the android camera):

enter image description here enter image description here

The Jelly Bean example may be a little complicated for what you are looking for, so below are some guidelines for the way I implemented visual feedback for autofocus. The process can be somewhat complicated. I don't pretend my way is the best way to do this, but here is some example code that gives you the general idea...

In my camera preview layout xml file:

<!-- Autofocus crosshairs -->

<RelativeLayout
    android:id="@+id/af_casing"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_centerInParent="true"
    android:clipChildren="false" >

    <com.package.AutofocusCrosshair
        android:id="@+id/af_crosshair"
        android:layout_width="65dp"
        android:layout_height="65dp"
        android:clipChildren="false" >
    </com.package.AutofocusCrosshair>
</RelativeLayout>

This AutofocusCrosshair class is the following:

public class AutofocusCrosshair extends View {

    private Point mLocationPoint;

    public AutofocusCrosshair(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private void setDrawable(int resid) {
        this.setBackgroundResource(resid);
    }

    public void showStart() {
        setDrawable(R.drawable.focus_crosshair_image);
    }

    public void clear() {
        setBackgroundDrawable(null);
    }

}

And when, in my activity, I want to start autofocus I do the following:

mAutofocusCrosshair = (AutofocusCrosshair) findViewById(R.id.af_crosshair);
//Now add your own code to position this within the view however you choose
mAutofocusCrosshair.showStart();
//I'm assuming you'll want to animate this... so start an animation here
findViewById(R.id.af_casing).startAnimation(mAutofocusAnimation);

And make sure at the end of your animation to clear the image:

mAutofocusAnimation.setAnimationListener(new AnimationListener() {
    @Override public void onAnimationEnd(Animation arg0) {
        mAutofocusCrosshair.clear();            
    }
    @Override public void onAnimationRepeat(Animation arg0) {}
    @Override public void onAnimationStart(Animation arg0) {}
});
Daniel Smith
  • 8,561
  • 3
  • 35
  • 58
  • Very nice answer! Makes mine look positively mean :-( – emrys57 Dec 06 '12 at 22:50
  • Awe emrys57 you are both sweet hearts lol.. Daniel thanks.. but I'm a little confused as to the animation. Part.. how do you animate it with the actual focus – erik Dec 07 '12 at 00:45
  • Well the autofocus itself is a completely separate thing which involves a call to the camera which can be found at http://developer.android.com/reference/android/hardware/Camera.html#autoFocus(android.hardware.Camera.AutoFocusCallback). The animation can happen while the autofocus is occurring. I kind of left the animation up to you because that is personal and differs across cameras. Something for another question methinks. I also didn't want to give you *all* the pieces to the puzzle :) – Daniel Smith Dec 07 '12 at 01:10
  • @DanielSmith where is defined **mAutofocusAnimation** animation ? – Android dev Aug 24 '16 at 06:54
1

If you mean the little rectangle which changes colour in the preview screen of the camera app, I'm pretty sure you have to draw that yourself. Sorry if that's not the answer you wanted!

However, you can call autoFocus() and it will later provide a result which tells whether or not the camera is in focus. Since API 14, that will work even if the camera is in FOCUS_MODE_CONTINUOUS_PICTURE.

I'm sorry, too, that I don't know of a good tutorial that describes using the focus mechanisms. One thing I've learnt in the past week: don't call autoFocus() before starting the preview images, because it crashes an HTC Nexus One.

I built my first Android camera app from example code at http://marakana.com/forums/android/examples/39.html but beware, the code as written there writes every preview frame to SD card and fills it up quickly! And there's no code in there about autofocus.

Edit: Of course, the ultimate example code, including the focus indicator, is in the camera app source code. This question: Where can I get the Android camera application source code? tells how to get it. I just followed the instructions there and got about 35Mbytes of source, and I'm afraid I haven't found the little focussing rectangle yet!

Community
  • 1
  • 1
emrys57
  • 6,679
  • 3
  • 39
  • 49