Android Audio and video Development (iv): Capturing video data using the Camera API

Source: Internet
Author: User

This article is mainly about: using the camera API to capture video data and save to a file, using Surfaceview, Textureview to preview the Camera data, take the NV21 data callback.

Note: Requires permission:<uses-permission android:name= "Android.permission.CAMERA"/>

First, preview the Camera data

People who have done Android development generally know that there are two ways to do this: Surfaceview, Textureview.

Here's how to preview your data using Surfaceview:

 Surfaceview Surfaceview; 
Camera camera;

@Override
protected void onCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);

Surfaceview = (surfaceview) Findviewbyid (R.id.surface_view);
Surfaceview.getholder (). Addcallback (this);

//Turn on the camera and rotate the display direction by 90 degrees
Camera = Camera.open ();
Camera.setdisplayorientation (90);

}

//------Surface preview-------
@Override
public void surfacecreated (Surfaceholder surfaceholder) {
try {
Camera.setpreviewdisplay (surfaceholder);
Camera.startpreview ();
} catch (IOException e) {
E.printstacktrace ();
}
}


@Override
public void surfacechanged (surfaceholder surfaceholder, int format, int w, int h) {
br>}

@Override
public void surfacedestroyed (Surfaceholder surfaceholder) {
Camera.release ();
}

Here's how to preview your data using Textureview:

Textureview Textureview;    Camera camera; @Overrideprotected voidonCreate (Bundle savedinstancestate) {Super. OnCreate (savedinstancestate); Setcontentview (r.layout.activity_main);Textureview=(Textureview) Findviewbyid (R.id.texture_view); Textureview.setsurfacetexturelistener ( This);//turn on the camera and rotate the display direction 90 degreesCamera =Camera.open (); Camera.setdisplayorientation (90); }
//------Texture Preview-------@Override Public voidOnsurfacetextureavailable (Surfacetexture surfacetexture,intIintI1) { Try{camera.setpreviewtexture (surfacetexture); Camera.startpreview (); } Catch(IOException e) {e.printstacktrace (); }} @Override Public voidOnsurfacetexturesizechanged (Surfacetexture surfacetexture,intIintI1) {} @Override Public Booleanonsurfacetexturedestroyed (surfacetexture surfacetexture) {camera.release (); return false; } @Override Public voidonsurfacetextureupdated (Surfacetexture surfacetexture) {}

Android Audio and video Development (iv): Capturing video data using the Camera API

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.