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