example for you to share the specific code for Android to take pictures and record video functions for your reference. The specific content is as follows: Camera

in the

document is to use Camera. First, let's look at how document is introduced. Compared with most other classes, the introduction of the document to the Camera is more detailed, including the steps required in the process, which, of course, shows the complexity of its actual use.
first, you need to declare the following permissions and features in AndroidManifest.xml:

 < uses-permission android:name= "android.permission.CAMERA" /> < uses-feature android:name=. 

then, taking pictures, you need the following ten steps:

1. gets an instance
2. through the open (int) method to get the default setting of the
3. by the getParameters () method, if necessary, modify the returned Camera.Parameters object, and call setParameters (Camera.Parameters) to set up
4 If there is a need, it is important to call setDisplayOrientation (int) to set the direction
5. of the display, and it is important to pass in a SurfaceHolder that has been initialized through setPreviewDisplay (SurfaceHolder), otherwise the preview can not be previewed.
6. is also important, starting with startPreview () to update your preview interface. Before you take photos, it must start.
7. calls takePicture (Camera.ShutterCallback, Camera.PictureCallback, Camera.PictureCallback, Camera.PictureCallback) to take pictures and wait for its callback
8. to take a picture, and the preview display will stop. If you want to continue taking photos, you need to call startPreview () again.
9. calls stopPreview () to stop preview.
10. is very important, calling release () to release Camera so that other applications can also use the camera. Your application should be released when onPause () is invoked and then re open () when onResume ().

is an introduction to Camera's photo taking in the document. Then talk about my use scene.

directly code

layout:

 <? XML version= "1" encoding= "UTF-8"? >. Ch_parent "android:paddingBottom=" @dimen/activity_vertical_margin "android:paddingLeft=" @dimen/activity_horizontal_margin "android:paddingRight=" @dimen/activity_horizontal_margin "android:paddingTop=" @dimen/activity_vertical_margin "android:orientation=" vertical "> dialectical; UrfaceView android:layout_width= "match_parent" android:layout_height= "0dp" android:layout_weight= "1" android:id= "@+id/sv_main_surface" /> < Button android:layout_width=. Android:text= "photo" /> < /LinearLayout> 

Activity

 import android.graphics.Bitmap; import android.graphics.BitmapFactory; import; Id.os.Bundle; import android.view.SurfaceHolder; import android.view.SurfaceView; import android.view.View; import android.widget.Toast; import java.io.FileNotFoundException; import; {private SurfaceView sv_main_surface; private Camera camera; @Override protected void onCreate (Bundle savedInstanceState); Sv_main_surface.getHolder ().AddCallback (New SurfaceHolder.Callback () {@Override public void surfaceCreated (SurfaceHolder surfaceHolder) {/ / / / / / / / / open camera camera = Camera.open ()); / / / / Eters (); parameters.setPictureFormat (PixelFormat.JPEG); parameters.set ("jpeg-quality", 85); camera.setParameters (parameters); / / / / / / / / to display the picture to SurfaceView try {camera.setPreviewDisplay (sv_main_surface.getHolder ());} catch (IOException E) {e.printStackTrace ();} / / / / / / open the preview effect camera.startPreview ();} @Override public void surfaceChanged (SurfaceHolder surfaceHolder, int i,) {} {} Stroyed (SurfaceHolder surfaceHolder) {if (camera! =null) {camera.stopPreview (); camera.release (); camera=null;}});} public void takePhoto ({} {} {] @Override public void onPictureTaken (byte[] bytes, Camera camera) {/ / / / / / / / / / Technology: image compression technology Bitmap bitmap=BitmapFactory.decodeByteArray (bytes, 0, bytes.length); NtTimeMillis () + ".Png"); bitmap.compress (Bitmap.CompressFormat.PNG, 85, FOS); camera.stopPreview (); camera.startPreview ();} catch (FileNotFoundException E) {e.printStackTrace ();}});} 

recording Video:

layout

 <? XML version= "1" encoding= "UTF-8"? > < RelativeLayout. T "android:paddingBottom=" @dimen/activity_vertical_margin "android:paddingLeft=" @dimen/activity_horizontal_margin "android:paddingRight=" @dimen/activity_horizontal_margin "android:paddingTop=" @dimen/activity_vertical_margin "> < SurfaceView" Parent "android:layout_height=" match_parent "android:id=" @+id/sv_recorder_surface "/> < LinearLayout android:layout_width=" match_parent "android:layout_height=" wrap_content. Rap_content "android:layout_height=" wrap_content "android:text=" starts "android:onClick=" start "/> < Button android:layout_width=" wrap_content "android:layout_height=". Layout> < /RelativeLayout> 

Code:

 import android.media.MediaRecorder; import android.support.v7.app.AppCompatActivity; import; ARecorderActivity extends AppCompatActivity {private SurfaceView sv_recorder_surface; private MediaRecorder mediaRecorder; @Override protected void. Sv_recorder_surface = (SurfaceView) findViewById (R.id.sv_recorder_surface); / / / / instantiate the media recorder mediaRecorder = new MediaRecorder ();} public void start (View view) {}; MediaRecorder.setAudioSource (MediaRecorder.AudioSource.MIC); / / / set format mediaRecorder.setOutputFormat (MediaRecorder.OutputFormat.MPEG_4); mediaRecorder.setVideoEncoder (MediaRecorder.VideoEncoder.H263); mediaRecorder.setAudioEncoder (MediaRecorder.AudioEncoder.AMR_NB); / / set the save path mediaRecorder.setOutputFile ("/mnt/sdcard/G150820_" +System.currentTimeMillis () + ".Mp4"); mediaRecorder.setPreviewDisplay (sv_recorder_surface.getHolder ().GetSurface ()); try {mediaRecorder.prepare (); mediaRecorder.start ();} catch (IOExc) Eption E) {e.printStackTrace ();} public void stop (View view) {if (mediaRecorder! =null) {mediaRecorder.stop ();} Open the authority of the camera --> < uses-permission android:name= "android.permission.CAMERA" /> < uses-permission android:name= "android.permission.MOUNT_UNMOUNT_FILESYSTEMS" /> <. Droid:name= "android.permission.RECORD_AUDIO" > < /uses-permission> 

simple photography and video recording function has been realized.

above is the whole content of this article, I hope it will help everyone's learning, and I hope you will support the script home.

you may be interested in articles:

" with MediaRecorder class


This concludes the body part

This paper fixed link:http://www.script-home.com/android-implements-video-and-video-functions.html | Script Home | +Copy Link

Article reprint please specify:Android implements video and video functions | Script Home

You may also be interested in these articles!