In Android, the camera is called and auto-focus is taken.

Source: Internet
Author: User

Code and example:
First, add the following content to the manifest file:

To allow the program to obtain the permission to use the camera and auto focus function.

Cameratestactivity. Java

Import Android. App. activity;
Import Android. OS. Bundle;
Import Android. View. window;
Import Android. View. windowmanager;

Public class cameratestactivity extends activity {
@ Override
Public void oncreate (bundle ){
Super. oncreate (bundle );
Getwindow (). clearflags (windowmanager. layoutparams. flag_force_not_fullscreen );
Getwindow (). addflags (windowmanager. layoutparams. flag_fullscreen );
Requestwindowfeature (window. feature_no_title); // sets the Landscape mode and full screen mode.
Setcontentview (New cameraview (this); // sets the view
}
}

Cameraview. Java

Import java. Io. fileoutputstream;

Import Android. content. context;
Import Android. Hardware. camera;
Import Android. OS. environment;
Import Android. View. motionevent;
Import Android. View. surfaceholder;
Import Android. View. surfaceview;

Public class cameraview extends surfaceview implements surfaceholder. Callback, camera. picturecallback {
Private surfaceholder holder;
Private camera;
Private Boolean AF;

Public cameraview (context) {// Constructor
Super (context );

Holder = getholder (); // generate surface holder
Holder. addcallback (this );

Holder. settype (surfaceholder. surface_type_push_buffers); // specify the push Buffer
}

Public void surfacecreated (surfaceholder holder) {// process the Surface Generation event
Try {
Camera = camera. open (); // camera Initialization
Camera. setpreviewdisplay (holder );
} Catch (exception e ){
}
}

@ Override
Public void surfacechanged (surfaceholder holder, int format, int width, int height) {// process the surface change event
Camera. parameters = camera. getparameters ();
Parameters. setpreviewsize (width, height );
Camera. setparameters (parameters); // you can specify parameters.
Camera. startpreview (); // start previewing
}

Public void surfacedestroyed (surfaceholder holder) {// processing of surface destruction
Camera. setpreviewcallback (null );
Camera. stoppreview ();
Camera. Release ();
Camera = NULL;
}

@ Override
Public Boolean ontouchevent (motionevent event) {// screen touch event
If (event. getaction () = motionevent. action_down) {// auto focus upon pressing
Camera. autofocus (null );
AF = true;
}
If (event. getaction () = motionevent. action_up & amp; AF = true) {// take a photo after you release it.
Camera. takepicture (null, null, this );
AF = false;
}
Returntrue;
}

Public void onpicturetaken (byte [] data, camera) {// Save the photo after shooting
Try {
String Path = environment. getexternalstoragedirectory () + "/test.jpg ";
Data2file (data, PATH );
} Catch (exception e ){
}
Camera. startpreview ();
}

Private void data2file (byte [] W, string filename) throws exception {// function for converting binary data to a file
Fileoutputstream out = NULL;
Try {
Out = new fileoutputstream (filename );
Out. Write (w );
Out. Close ();
} Catch (exception e ){
If (OUT! = NULL)
Out. Close ();
Throw E;
}
}

}

Note: This is a simple example program. The auto focus function is used to take a photo and save it in the root directory of the SD card. Note that you must declare the relevant license in the manifest file. In this example, many parameters are not set. You can modify them as needed to meet specific requirements.

 

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.