Implementation of autofocus-----loop Auto-focus for Android camera

Source: Internet
Author: User

Reference: Http://blog.sina.com.cn/s/blog_7dbac1250101mloj.html uses the gravity sensing method has realized to realize the Android camera autofocus, is not difficult, But it will take a bit of effort to achieve automatic, continuous autofocus. This is where my research ideas and solutions are recorded. To achieve auto-focus, the method is as follows:

First : The most common implementation---use a button or a one-time autofocus before taking a photo

Declare the variable private autofocuscallback myautofocuscallback = null in activity;

Auto Focus Variable Callback
Myautofocuscallback = new Autofocuscallback () {

public void Onautofocus (Boolean success, camera camera) {
TODO auto-generated Method Stub
if (success)//success indicates a successful focus
{
LOG.I (Tag, "myautofocuscallback:success ...");
Mycamera.setoneshotpreviewcallback (NULL);

}
Else
{
No focus succeeded
LOG.I (Tag, "Myautofocuscallback: Failed ...");

}


}
};

After Camera.startpreview (), call Mycamera.autofocus (Myautofocuscallback) before taking a picture of camera.takepicture ().

Second : Set a timer through TimerTask to cycle, call Mycamera.autofocus (Myautofocuscallback) at a certain time. Refer to my previous blog http://blog.csdn.net/yanzi1225627/article/details/7926994 and http://www.cnblogs.com/liuan/archive/ 2012/01/10/2318300.html.

This method may work, but I didn't succeed in the experiment. See http://blog.csdn.net/yanzi1225627/article/details/7929953 here. The result of this approach is that the camera has been self-focused, repetitive and clear-blurred----clear---blurred process.

Third : Open a thread for auto-focus via handler and thread. The essence is the same as the second. Later I will write a demo to send up.

Fourth : Trigger the autofocus by monitoring the sensor. An angle sensor or accelerometer is commonly used, triggering when the value of the listener exceeds a certain threshold. This is also a common idea used by foreign programmers. See also: Http://stackoverflow.com/questions/5878042/android-camera-autofocus-on-demand

Fifth : Set the focus mode to Myparam.setfocusmode (camera.parameters. Focus_mode_continuous_video); Refer here

Http://stackoverflow.com/questions/5856500/android-camera-continuous-focus can see the official explanation:

Continuous auto focus mode intended for video recording. The camera continuously tries to focus. The choice for video recording because the focus changes smoothly. Applications still can call in this mode but the subject is not being in takePicture(Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback) focus. Auto focus starts when the parameter is set.

Since API level, applications can call in this autoFocus(AutoFocusCallback) mode. The focus callback would immediately return with a Boolean, indicates whether the focus is sharp or not. The focus position is locked after autoFocus call. If applications want to resume the continuous focus, cancelautofocus must is called. Restarting the preview would not resume the continuous autofocus. To stop continuous focus, applications should change the focus mode to other modes.

This means that it is OK to call this focus mode when recording, which may not be successful in the case of a photograph.

In my experiment, this way can be. But strangely, after setting Myparam.set ("Rotation", 90); The setting of this sentence does not work. The photos and previews taken are 90 degrees in the wrong direction. After you need to get bitmap, use the matrix to rotate yourself. Example of rotation:

Mbitmap = Bitmapfactory.decodebytearray (data, 0, data.length);//data is byte data, parsing it into a bitmap

Mycamera.stoppreview ();
Ispreview = false;

After setting Focus_mode_continuous_video), Myparam.set ("Rotation", 90) fails. The picture can't be rotated, so it's going to spin down here.
Matrix matrix = new Matrix ();
Matrix.postrotate ((float) 90.0);
Bitmap Rotabitmap = Bitmap.createbitmap (mbitmap, 0, 0, mbitmap.getwidth (), Mbitmap.getheight (), Matrix, false);

This is the way I am currently using, after setting it to Focus_mode_continuous_video, then calling Camera.autofocus () will focus on failure. But there are drawbacks to this approach, but it is not known when it will focus well. If I want to capture a photo and analyze it every 100 milliseconds. There is no guarantee that I have focused on the photos when I snapped them! But this problem can also be ignored, it must be told that the user has focused on the situation and then press the photo, usually the camera stays about 2 seconds, automatically focused. The fourth method must be used in order to be precise when necessary.

Sixth : This is my own way of thinking, not yet verified. is by analyzing Onpreviewframe to get preview frame video, analyze its brightness change, to trigger Camera.autofocus (). If the brightness changes, there must be a relative movement between the camera and the object being photographed, which needs to trigger the autofocus (). This is probably the way the camera software comes with some phones. I'll write a demo to verify its effectiveness later.

Implementation of autofocus-----loop Auto-focus for Android camera

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.