Surfaceview: displays the video taken by camera.

Source: Internet
Author: User

Generally, you can use mediarecorder to record videos. Video data can also be displayed on surfaceview, but data must be written to the storage device. In addition, if our project is video chat, media cannot be used.

I found the information online and changed it myself:

Public class cameravedioactivity extends activity {Private Static final string tag = "cameravedioactivity"; private camera; private Boolean Preview = false; private seekbar mseekbar; @ override public void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate);/** set window properties: Be sure to go to setcontentview (R. layout. main) before * // window title, you can register the // requestwindowfeature (window. feature _ No_title); // full screen getwindow (). setflags (windowmanager. layoutparams. flag_fullscreen, windowmanager. layoutparams. flag_fullscreen); setcontentview (R. layout. main); surfaceview = (surfaceview) findviewbyid (R. id. surfaceview); surfaceview. getholder (). settype (surfaceholder. surface_type_push_buffers); surfaceview. getholder (). setfixedsize (200,200); surfaceview. getholder (). addcallback (New surfacevi Ewcallback ();/*** seekbar is used to implement zoom logic */mseekbar = (seekbar) findviewbyid (R. id. seekbar); mseekbar. setonseekbarchangelistener (New seekbar. onseekbarchangelistener () {@ overridepublic void onprogresschanged (seekbar, int progress, Boolean fromuser) {// todo auto-generated method stublog. D (TAG, "Progress:" + progress) ;}@ overridepublic void onstarttrackingtouch (seekbar) {// todo auto-generate D METHOD stublog. D (TAG, "onstarttrackingtouch") ;}@ overridepublic void onstoptrackingtouch (seekbar) {// todo auto-generated method stublog. D (TAG, "onstarttrackingtouch ");}});} private final class surfaceviewcallback implements callback {/*** call this method after surfaceview is created successfully */@ overridepublic void surfacecreated (surfaceholder holder) {log. D (TAG, "surfacecreated");/** open the camera after surfaceview is created. * Note that Ndroid. hardware. camera */camera = camera. open ();/** this method must be called before startpreview (). otherwise surfaceview no image */try {Camera. setpreviewdisplay (holder);} catch (ioexception e) {// todo auto-generated catch blocke. printstacktrace ();} camera. parameters = camera. getparameters ();/* set the size of the previewed photo. Set this parameter to full screen * // windowmanager WM = (windowmanager) getsystemservice (context. window_servi CE); // get the current screen manager object // display = WM. getdefadisplay display (); // The description class for obtaining screen information // parameters. setpreviewsize (display. getwidth (), display. getheight (); // set parameters. setpreviewsize (200,200);/* capture 5 frames per second from the camera, */parameters. setpreviewframerate (5);/* set the photo output format: JPG */parameters. setpictureformat (pixelformat. JPEG);/* photo quality */parameters. set ("Jpeg-quality", 85);/* set the Photo size. The photo size is equal to the screen size. * // parameters. setpicture Size (display. getwidth (), display. getheight (); parameters. setpicturesize (200,200);/* assign the parameter object to the camera object * // camera. setparameters (parameters); mseekbar. setmax (100); camera. startpreview ();/*** installa callback to be invoked for every preview frame in addition to displaying them on the screen. * The callback will be repeatedly called for as long as preview is active. this method can be called * Any time, even while preview is live. any other preview Callbacks are overridden. * A callback object that copies es a copy of each preview frame, or null to Stop locking ing */camera. setpreviewcallback (new camera. previewcallback () {@ overridepublic void onpreviewframe (byte [] data, camera) {// todo auto-generated method stub // In the video chat, transmit the local frame data to the remote log. D (TAG, "camera:" + camera); log. D (tag, "Byte:" + Data) ;}}); Preview = true ;}@ overridepublic void surfacechanged (surfaceholder holder, int format, int width, int height) {log. D (TAG, "surfacechanged");}/*** release the camera when the surfaceview is destroyed */@ overridepublic void surfacedestroyed (surfaceholder holder) {If (camera! = NULL) {/* if the camera is working, stop it first */If (PreView) {Camera. stoppreview (); Preview = false;} // if this callback is registered, it is called before release; otherwise, it is called after release, crashcamera. setpreviewcallback (null); camera. release () ;}}/ *** process events after a photo is taken */private final class takepicturecallback implements picturecallback {@ overridepublic void onpicturetaken (byte [] data, camera) {}}}

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"    >    <LinearLayout    android:layout_width="fill_parent"    android:layout_height="fill_parent"    android:orientation="vertical"    ><SurfaceView android:id="@+id/surfaceView"    android:layout_width="300dip"     android:layout_height="300dip"     android:visibility="visible"    />    <LinearLayout    android:layout_width="fill_parent"    android:layout_height="wrap_content"    >    <SeekBar    android:id="@+id/seekbar"    android:layout_width="fill_parent"    android:layout_height="wrap_content"    />    </LinearLayout>    </LinearLayout></FrameLayout>

I tested surfaceview on my machine and it must be included in framelayout.

Manifest file:

<? XML version = "1.0" encoding = "UTF-8"?> <Manifest xmlns: Android = "http://schemas.android.com/apk/res/android" package = "cn.edu. WTU "Android: versioncode =" 1 "Android: versionname =" 1.0 "> <application Android: icon =" @ drawable/icon "Android: label = "@ string/app_name"> <! -- Android: screenorientation = "Landscape" indicates the horizontal interface --> <activity Android: Name = ". cameravedioactivity "Android: Label =" @ string/app_name "Android: screenorientation =" portrait "> <intent-filter> <action Android: Name =" android. intent. action. main "/> <category Android: Name =" android. intent. category. launcher "/> </intent-filter> </activity> </Application> <uses-SDK Android: minsdkversion =" 7 "/> <! -- Create and delete file permissions in sdcard --> <uses-Permission Android: Name = "android. Permission. mount_unmount_filesystems"/> <! -- Write data permission to sdcard --> <uses-Permission Android: Name = "android. Permission. write_external_storage"/> <! -- Apply for permission to use the camera --> <uses-Permission Android: Name = "android. Permission. Camera"/> </manifest>

Note: Some mobile phones do not support zoom, so setting the camera parameter will crash. To implement the zoom logic, first determine whether the mobile phone supports smoothzoom.

SDK 2.1 still does not support setzoom, 2.3.3 is acceptable, and 2.2 is not tested

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.