1. The function is not recorded.
The basic realization of the camera, recording video, the function of the front and rear cameras switch, you can turn the screen, focus.
code at the bottom, you can look at the code, run the APK to see the actual effect .
2. Tool class
Package Com.example.mycamera;import Android.content.context;import Android.hardware.camera;import Android.media.mediarecorder;import Android.util.log;import Android.view.surfaceholder;import Android.view.surfaceview;import Java.io.file;import Java.io.filenotfoundexception;import Java.io.FileOutputStream ; Import Java.io.ioexception;import java.util.list;import static android.content.contentvalues.tag;/** * Created by Guoxw on 2017/10/31. * <!-achieve photo, video: And save pictures, videos to local ></!-> */public class Camerautils {private Mediarecorder mediarecorder; Private camera camera; /*** ID is currently the front camera or rear camera back:0 front:1*/private int backorftont = 0; Private Surfaceholder.callback Callback; Private context context; Private Surfaceview Surfaceview; /*** recording video of videosize*/private int height, width; /***photo height, width*/private int heightphoto, Widthphoto; public void Create (Surfaceview Surfaceview, Context context) {This.context = context; This.surfAceview = Surfaceview; Surfaceview.getholder (). SetType (Surfaceholder.surface_type_push_buffers); Surfaceview.setkeepscreenon (TRUE); callback = new Surfaceholder.callback () {public void surfacecreated (Surfaceholder holder) {Cam ERA = Camera.open (); Getvideosize (); Mediarecorder = new Mediarecorder (); } public void Surfacechanged (surfaceholder holder, int format, int width, int height) {dochange (holder); Focus (); } @Override public void surfacedestroyed (Surfaceholder holder) {if (camera! = null) {camera.release (); camera = null; } } }; Surfaceview.getholder (). Addcallback (callback); } private void Dochange (Surfaceholder holder) {try {camera.setpreviewdisplay (holder); Camera.setdisplayorientation (90); Camera.startpreview (); } catch (IOException e) {e.printstacktrace (); }}/** * Toggle camera */public void Changecamera () {int cameracount = 0; Camera.camerainfo camerainfo = new Camera.camerainfo (); Cameracount = Camera.getnumberofcameras (); for (int i = 0; i < Cameracount; i++) {Camera.getcamerainfo (I, camerainfo); if (camerainfo.facing = = Camera.CameraInfo.CAMERA_FACING_FRONT && Backorftont = = 0) {CAMERA.STOPPR Eview (); Camera.release (); camera = null; Camera = Camera.open (i); try {camera.setpreviewdisplay (Surfaceview.getholder ()); Camera.setdisplayorientation (90); } catch (IOException e) {e.printstacktrace (); } Backorftont = 1; Camera.startpreView (); Break } else if (camerainfo.facing = = Camera.CameraInfo.CAMERA_FACING_BACK && Backorftont = = 1) {Camera. Stoppreview (); Camera.release (); camera = null; Camera = Camera.open (i); try {camera.setpreviewdisplay (Surfaceview.getholder ()); Camera.setdisplayorientation (90); } catch (IOException e) {e.printstacktrace (); } camera.startpreview (); Backorftont = 0; Break }}} public void Stoprecord () {mediarecorder.release (); Camera.release (); Mediarecorder = null; Camera = Camera.open (); Mediarecorder = new Mediarecorder (); Dochange (Surfaceview.getholder ()); } public void Stop () {if (Mediarecorder! = NULL && camera! = null) {mediarecorder.release (); Camera.release (); }} public void Destroy () {if (Mediarecorder! = NULL && camera! = null) {MEDIARECORDER.R Elease (); Camera.release (); Mediarecorder = null; camera = null; }}/** * @param path saved paths * @param name recording video names (not including suffixes) */public void Startrecord (String path, Strin G name) {Camera.unlock (); Mediarecorder.setcamera (camera); Mediarecorder.setaudiosource (MediaRecorder.AudioSource.CAMCORDER); Mediarecorder.setvideosource (MediaRecorder.VideoSource.CAMERA); Mediarecorder.setprofile (Camcorderprofile.get (Camcorderprofile.quality_low)); Mediarecorder.setoutputformat (MediaRecorder.OutputFormat.MPEG_4); Mediarecorder.setaudioencoder (MediaRecorder.AudioEncoder.DEFAULT); Mediarecorder.setvideoencoder (MediaRecorder.VideoEncoder.H264); Mediarecorder.setvideoencodingbitrate (700 * 1024); Mediarecorder.setvideosiZe (width, height); Mediarecorder.setvideoframerate (24); Getvideosize (); File File = new file (path); if (!file.exists ()) {file.mkdirs (); } mediarecorder.setoutputfile (path + file.separator + name + ". mp4"); File File1 = new file (path + file.separator + name + ". mp4"); if (file1.exists ()) {file1.delete (); } mediarecorder.setpreviewdisplay (Surfaceview.getholder (). Getsurface ()); /*** is not set, the recorded video is always inverted, the flip screen causes the video to roll up and down */mediarecorder.setorientationhint (0); try {mediarecorder.prepare (); Mediarecorder.start (); } catch (IOException e) {e.printstacktrace (); }}/*** * get supportedvideosizes control output video width between 300 and 600 (as small as possible) * Get the size of the picturesize (control between w:1000-2000) */ public void Getvideosize () {camera.parameters Parameters = Camera.getparameters (); list<camera.size> videosize = parameters.getsupportedvideosizeS (); for (int i = 0; i < videosize.size (); i++) {int width1 = Videosize.get (i). width; int height1 = Videosize.get (i). Height; if (width1 >= && width1 <=) {if (height1 >= && height1 <= 600) { width = width1; Height = height1; }} log.d (TAG, "getvideosize:----w:--" + videosize.get (i). Width + "---h:--" + videosize.get (i). Heig HT); } list<camera.size> photosize = parameters.getsupportedpicturesizes (); for (int i = 0; i < photosize.size (); i++) {int width1 = Photosize.get (i). width; int height1 = Photosize.get (i). Height; if (width1 >= && width1 <=) {if (height1 >= && height1 <=2000) { Widthphoto = width1; Heightphoto = height1; } } }} public void Takepicture (string photopath, String photoname) {camera.takepicture (null, NULL, new picture CallBack (Photopath, photoname)); }/** * Focus */public void focus () {camera.parameters parameters=camera.getparameters (); Parameters.setpicturesize (Widthphoto,heightphoto); Parameters.setfocusmode (Camera.Parameters.FOCUS_MODE_CONTINUOUS_PICTURE); Camera.setparameters (parameters); Camera.cancelautofocus (); }/*** Photo Function */Private class Picturecallback implements Camera.picturecallback {/*** photo saved path and name */priv Ate String path; private String name; Public Picturecallback (string path, string name) {This.path = path; THIS.name = name; } @Override public void Onpicturetaken (byte[] bytes, camera camera) {File File1 = new File (path) ; if (!file1.exists ()) {file1.mkdirs (); } File File = NEW File (path, name); if (file.exists ()) {file.delete (); } FileOutputStream fos = null; try {fos = new FileOutputStream (file); try {fos.write (bytes); } catch (IOException e) {e.printstacktrace (); }} catch (FileNotFoundException e) {e.printstacktrace (); } camera.startpreview (); } }}
3. The method is called directly using.
Package Com.example.mycamera;import Android.os.bundle;import Android.os.environment;import Android.support.v7.app.appcompatactivity;import Android.util.log;import Android.view.surfaceview;import Android.view.view;import Android.view.window;import Android.widget.imageview;public class MainActivity extends appcompatactivity {private static final String TAG = "playactivity"; Private Surfaceview Surfaceview; Private Camerautils camerautils; Private String path, name; Private ImageView btn; Private ImageView camera; Private ImageView change; int x = 0; @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Requestwindowfeature (Window.feature_no_title); Setcontentview (R.layout.activity_main); LOG.D (TAG, "onCreate:"); Getsupportactionbar (). Hide (); BTN = (ImageView) Findviewbyid (R.ID.BTN); Camera = (ImageView) Findviewbyid (R.id.camera); Change = (ImageView) findviEwbyid (R.id.change); Surfaceview = (Surfaceview) Findviewbyid (R.id.surfaceview); Camerautils = new Camerautils (); Camerautils.create (Surfaceview, this); Path = Environment.getexternalstoragedirectory (). GetAbsolutePath (); Name = "Video"; Btn.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) { if (x = = 0) {//Camerautils.changecamera (); Camerautils.startrecord (path, name); Btn.setimageresource (R.drawable.video); x = 1; } else if (x = = 1) {Camerautils.stoprecord (); Btn.setimageresource (R.DRAWABLE.VIDEO1); x=0; } } }); Camera.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) { Camerautils.takepicture (Path, "name.")PNG "); } }); Change.setonclicklistener (New View.onclicklistener () {@Override public void OnClick (view view) { Camerautils.changecamera (); } }); } @Override protected void Onresume () {super.onresume (); LOG.D (TAG, "Onresume:"); } @Override protected void OnPause () {super.onpause (); LOG.D (TAG, "OnPause:"); } @Override protected void OnStop () {super.onstop (); LOG.D (TAG, "onStop:"); Camerautils.stop (); } @Override protected void OnDestroy () {Super.ondestroy (); LOG.D (TAG, "OnDestroy:"); Camerautils.destroy (); }}
Code
HTTPS://pan.baidu.com/s/1hr7kna0
Android photo, record video, Camera simple function implementation