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.