This article examples for you to explain how to easily realize the Android phone camera function, share for everyone to reference. Specifically as follows:
I. Layout file Main.xml
<?xml version= "1.0" encoding= "Utf-8"?> <framelayout xmlns:android= "http://schemas.android.com/apk/res/" Android "Android:layout_width=" Fill_parent "android:layout_height=" fill_parent "> <surfaceview android:id = "@+id/surfaceview" android:layout_width= "fill_parent" android:layout_height= "fill_parent"/> <RelativeLa
Yout android:id= "@+id/buttonlayout" android:layout_width= "fill_parent" android:layout_height= "Fill_parent" Android:visibility= "Gone" > <button android:id= "@+id/takepicture" android:layout_width= "Wrap_conte" NT "android:layout_height=" Wrap_content "android:layout_alignparentbottom=" true "Android:layout_alignpar Entright= "true" android:layout_marginright= "5DP" android:onclick= "Takepicture" android:text= "@string/tak Epicture "/> <button android:id= @+id/autofocus" android:layout_width= "Wrap_content" Android:
layout_height= "Wrap_content" android:layout_aligntop= "@id/takepicture" android:layout_marginright= "20DP" android:layout_toleftof= "@id/take Picture "android:onclick=" Takepicture "android:text=" @string/autofocus "/> </RelativeLayout> </
Framelayout>
Two, Mainactivity.java
Import Java.io.File;
Import Java.io.FileOutputStream;
Import android.app.Activity;
Import Android.hardware.Camera;
Import Android.hardware.Camera.PictureCallback;
Import Android.os.Bundle;
Import android.os.Environment;
Import android.view.MotionEvent;
Import Android.view.SurfaceHolder;
Import Android.view.SurfaceHolder.Callback;
Import Android.view.SurfaceView;
Import Android.view.View;
Import Android.view.ViewGroup;
Import Android.view.Window;
Import Android.view.WindowManager;
public class Mainactivity extends activity {private View layout;
Private Camera Camera;
@Override public void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Requestwindowfeature (Window.feature_no_title);
GetWindow (). SetFlags (WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Setcontentview (R.layout.main);
Layout = This.findviewbyid (r.id.buttonlayout);
Surfaceview Surfaceview = (Surfaceview) This . Findviewbyid (R.id.surfaceview);
Surfaceview.getholder (). SetType (Surfaceholder.surface_type_push_buffers);
Surfaceview.getholder (). Setfixedsize (176, 144);
Surfaceview.getholder (). Setkeepscreenon (True);
Surfaceview.getholder (). Addcallback (New Surfacecallback ());
public void Takepicture (View v) {if (camera!= null) {switch (V.getid ()) {case R.id.takepicture:
Camera.takepicture (NULL, NULL, New Mypicturecallback ());
Break
Case R.id.autofocus:camera.autofocus (NULL);
Break }} Private Final class Mypicturecallback implements Picturecallback {public void Onpicturetaken (byte[) da
TA, Camera Camera) {try {file Jpgfile = new File (Environment.getexternalstoragedirectory (),
System.currenttimemillis () + ". jpg");
FileOutputStream OutStream = new FileOutputStream (jpgfile);
Outstream.write (data);
Outstream.close (); Camera.startpreview ();
catch (Exception e) {e.printstacktrace (); }} Private Final class Surfacecallback implements Callback {public void surfacecreated (Surfaceholder hol
Der) {try {camera = Camera.open ();//Open camera camera.parameters Parameters = Camera.getparameters ();
LOG.I ("Mainactivity", Parameters.flatten ());
Parameters.setpreviewsize (800, 480);
Parameters.setpreviewframerate (5);
Parameters.setpicturesize (1024, 768);
Parameters.setjpegquality (80);
Camera.setparameters (parameters);
Camera.setpreviewdisplay (holder);
Camera.startpreview ()//Start preview of catch (Exception e) {e.printstacktrace (); } public void Surfacechanged (surfaceholder holder, int format, int width, int height) {} publ
IC void surfacedestroyed (Surfaceholder holder) {if (camera!= null) {camera.release ();
camera = null; @Override public boolean ontouchevent (Motionevent event) {if event.getaction () = = Motionevent.
Action_down) {layout.setvisibility (viewgroup.visible);
return true;
Return Super.ontouchevent (event);
}
}
Third, add permissions
<uses-permission android:name= "Android.permission.CAMERA"/>
<!--Create and delete file permissions in SDcard-->
< Uses-permission android:name= "Android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
<!--write data permissions to SDcard-->
<uses-permission android:name= "Android.permission.WRITE_EXTERNAL_STORAGE"/>
The effect is as follows:
I hope this article will help you learn about Android software programming.