Play to Android Camera Development (ii): Preview camera Base photo demo using Textureview and Surfacetexture

Source: Internet
Author: User

Google has textureview from Android4.0. Why launch it? is to make up for Surfaceview's shortcomings. On the other hand is also to balance Glsurfaceview. Of course, this is my speculate.

About the relationship between Textureview, Surfaceview, Surfacetexture, Glsurfaceview, we will launch the Glsurfaceview preview camera after our special analysis.

This article focuses on previewing a camera using Textureview.

In fact about how to use Textureview preview camera, the official website has given the demo, see here. In addition, link 1 Link 2 also gives a complete preview of the camera's demo. But it's all a bunch of things dyed in one piece. This article uses a lightweight camera frame built in the previous article to replace Surfaceview at high speed. Because with Surfaceview preview words to pass a surfaceholder in, with Textureview preview words need to pass in a surfacetexture. Other camera processes are the same.

First, the new Cameratextureview class inherits the Textureview, and implements the Textureview.surfacetexturelistener interface.

Implementing this interface is like implementing Surfaceholder.callback, and the basic purpose is to know when Surfacetexture is ready, that is, onsurfacetextureavailable the function.

Cameratextureview.java

Package Org.yanzi.camera.preview;import Org.yanzi.camera.camerainterface;import Android.content.context;import Android.graphics.pixelformat;import Android.graphics.surfacetexture;import Android.util.attributeset;import Android.util.log;import Android.view.surfaceholder;import Android.view.surfaceview;import Android.view.textureview;public class Cameratextureview extends Textureview implements Textureview.surfacetexturelistener {private static final String TAG = "Yanzi"; Context Mcontext; Surfacetexture Msurface;public Cameratextureview (context context, AttributeSet Attrs) {Super (context, attrs);//TODO auto-generated constructor Stubmcontext = Context;this.setsurfacetexturelistener (this);} @Overridepublic void onsurfacetextureavailable (surfacetexture surface, int width,int height) {//TODO auto-generated Method Stublog.i (TAG, "onsurfacetextureavailable ..."); msurface = Surface;//camerainterface.getinstance (). Dostartpreview (surface, 1.33f);} @Overridepublic Boolean onsurfacetexturedestroyed (Surfacetexture surface) {//TODO auto-generated method stublog.i (TAG, "onsurfacetexturedestroyed ..."); Camerainterface.getinstance (). Dostopcamera (); return true;} @Overridepublic void onsurfacetexturesizechanged (surfacetexture surface, int width,int height) {//TODO auto-generated Method Stublog.i (TAG, "onsurfacetexturesizechanged ...");} @Overridepublic void onsurfacetextureupdated (Surfacetexture surface) {//TODO auto-generated method stublog.i (TAG, " Onsurfacetextureupdated ... "); /* Allow activity to get textureview surfacetexture * @see android.view.textureview#getsurfacetexture () */public Surfacetexture _getsurfacetexture () {return msurface;}}
second, in the layout file to add it, because his father is the view. As a general view can be

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android" xmlns:tools= "http// Schemas.android.com/tools "android:layout_width=" match_parent "android:layout_height=" Match_parent "Tools:context =". Cameraactivity "> <framelayout android:layout_width=" wrap_content "android:layout_height=" Wrap_cont            Ent "> <org.yanzi.camera.preview.cameratextureview android:id=" @+id/camera_textureview "        Android:layout_width= "0dip" android:layout_height= "0dip"/> </FrameLayout> <imagebutton        Android:id= "@+id/btn_shutter" android:layout_width= "wrap_content" android:layout_height= "Wrap_content" android:background= "@drawable/btn_shutter_background" android:layout_alignparentbottom= "true" android:l Ayout_centerhorizontal= "true" android:layout_marginbottom= "10dip"/></relativelayout>

Three, in Camerainterface, I encapsulated two functions:

/** use Surfaceview to open Preview * @param holder * @param previewrate */public void Dostartpreview (surfaceholder holder, float preview Rate) {log.i (TAG, "Dostartpreview ..."); if (ispreviewing) {Mcamera.stoppreview (); return;} if (Mcamera! = null) {try {mcamera.setpreviewdisplay (holder);} catch (IOException e) {//TODO auto-generated catch BLOCKE.P Rintstacktrace ();} Initcamera (previewrate);}}  /** using Textureview Preview camera * @param surface * @param previewrate */public void Dostartpreview (surfacetexture surface, float Previewrate) {log.i (TAG, "Dostartpreview ..."); if (ispreviewing) {Mcamera.stoppreview (); return;} if (Mcamera! = null) {try {mcamera.setpreviewtexture (surface)} catch (IOException e) {//TODO auto-generated catch Blocke. Printstacktrace ();} Initcamera (previewrate);}}
Corresponding Surfaceview and Textureview preview respectively. To see is not the same as the number of participants, Initcamera () things are the same.

private void Initcamera (float previewrate) {if (Mcamera! = null) {Mparams = Mcamera.getparameters (); Mparams.setpictureformat (pixelformat.jpeg);//Set the picture format stored after the photo//camparautil.getinstance (). Printsupportpicturesize ( Mparams);//camparautil.getinstance (). Printsupportpreviewsize (Mparams);//Set previewsize and Picturesizesize Picturesize = Camparautil.getinstance (). Getproppicturesize (Mparams.getsupportedpicturesizes (), PreviewRate, 800); Mparams.setpicturesize (Picturesize.width, picturesize.height); Size previewsize = Camparautil.getinstance (). Getproppreviewsize (Mparams.getsupportedpreviewsizes (), PreviewRate, Mparams.setpreviewsize (Previewsize.width, previewsize.height); Mcamera.setdisplayorientation (90);// Camparautil.getinstance (). Printsupportfocusmode (Mparams); list<string> focusmodes = Mparams.getsupportedfocusmodes (), if (Focusmodes.contains ("Continuous-video")) { Mparams.setfocusmode (Camera.Parameters.FOCUS_MODE_CONTINUOUS_VIDEO);} Mcamera.setparameters (Mparams); Mcamera.startpreview ();//Open Preview IspreviEwing = True;mpreviwrate = Previewrate;mparams = Mcamera.getparameters (); Again get once log.i (TAG, "finally set: Previewsize--with =" + mparams.getpreviewsize (). width+ "Height =" + mparams.getpreviewsize ( ). height); LOG.I (TAG, "finally set: Picturesize--with =" + mparams.getpicturesize (). width+ "height =" + mparams.getpicturesize (). height);} }

Four, in the activity, still open a thread to open Camera:

Thread openthread = new Thread () {@Overridepublic void Run () {//TODO auto-generated method Stubcamerainterface.getinstanc E (). Doopencamera (cameraactivity.this);}};o Penthread.start ();

Preview in the camera open callback:

@Overridepublic void camerahasopened () {//TODO auto-generated method Stubsurfacetexture surface = Textureview._ Getsurfacetexture (); Camerainterface.getinstance (). Dostartpreview (surface, previewrate);}

Then it will work properly. Be able to see with the previous Surfaceview preview camera modifications are very small.



Several caveats:

1, Textureview is the increase after Android 4.0, the lower version number of this class. Textureview must work in an environment where hardware acceleration is enabled. This is also the setting of the activity in the configuration file: Android:hardwareaccelerated= "True" the default property is True, so no more writing. But if you write false, you can see Onsurfacetextureavailable () This callback will not come in. Textureview don't have surfacetexture to play a fart.

2. The normal log for the demo to open camera and preview is:

Line 417:06-22 12:37:43.682 I/yanzi (4917): Camera open .... Line 489:06-22 12:37:43.758 I/yanzi (4917): Onsurfacetextureavailable ... Line 533:06-22 12:37:43.819 I/yanzi (4917): Camera open over .... Line 535:06-22 12:37:43.819 I/yanzi (4917): Dostartpreview ... Line 537:06-22 12:37:43.825 I/yanzi (4917): picturesize:w = 1280h = 720Line 539:06-22 12:37:43.825 I/yanzi (4917 ): Previewsize:w = 800h = 448Line 555:06-22 12:37:43.874 i/yanzi (4917): finally set: Previewsize--with = 800Height = 448Line   557:06-22 12:37:43.874 I/yanzi (4917): finally set: Picturesize--with = 1280Height = 720Line 577:06-22 12:37:44.106 I/yanzi (4917): onsurfacetextureupdated ... Line 579:06-22 12:37:44.138 I/yanzi (4917): onsurfacetextureupdated ... Line 583:06-22 12:37:44.169 I/yanzi (4917): onsurfacetextureupdated ... Line 585:06-22 12:37:44.220 I/yanzi (4917): onsurfacetextureupdated ... Line 587:06-22 12:37:44.253 I/yanzi (4917): onsurfacetextureupdated ...

Test cell phone for ZTE Geek. This phone camera is still very good, than the hand of Huawei G700 Strong. Is that occasionally you can't even get on camera Service, Khan.

Can be seen from log. Onsurfacetextureavailable This callback takes some time. Camera.open () This sentence used more than 130 Ms. But two points are different from Surfaceview. First, Textureview is not entered into the onsurfacetexturesizechanged () function during the creation process. and Surfaceview in the creation process, from scratch into the size of changes in the callback.

second. onsurfacetextureupdated () This function comes in one frame of data each time .

This is compared with Surfaceview. One of the greatest places.

Through this interface. Can send the surfacetexture to OpenGL to deal with.

This callback is real-time. Instead of using the camera's previewcallback, this is a 2-time callback method.

From the time, basically every 32ms or so up a frame of data, that is, 30 frames per second. Match the performance of this phone's camera.

3, Camera again run Startpreview must ensure that the Textureview surfacetexture come up, assuming for some performance reasons onsurfacetextureavailable () This callback does not come on the preview, It's not going to open. Assuming this happens, run open and Startpreview operations in the onsurfacetextureavailable () callback to ensure foolproof.

4, Textureview itself has getsurfacetexture () This function, I also encapsulated a:

/* Allow activity to get textureview surfacetexture * @see android.view.textureview#getsurfacetexture () */public Surfacetexture _getsurfacetexture () {return msurface;}
The msurface here is the surfacetexture in the Onsurfacetextureavailable () callback. Test proof. Direct adjustment when opening preview

Textureview.getsurfacetexture (), pass it to Camera:mCamera.setPreviewTexture (surface), and you can preview it normally.

But it is recommended to use the former, for the reasons see the official paragraph:

A Textureview ' s surfacetexture can be obtained either by invokinggetSurfaceTexture()or by using aTextureView.SurfaceTextureListener. It is important to know that a surfacetexture are available only after the Textureview are attached to a window ( andonAttachedToWindow()Has been invoked.) It is therefore highly recommended your use of a listener to being notified when the surfacetexture becomes available.

Two ways to get surfacetexture, the recommended use of monitoring. Only when the Textureview run out of Onattachedtowindow, its tsurfacetexture came up.

5, surfacetexture and Textureview relationships:

Using a textureview is simple:all the need to does is get its  surfacetexture . the  Surfacetexture  can then being used to render content

Assuming that Textureview is a painting, that surfacetexture is the canvas, the real vector of rendering is surfacetexture.

6,Textureview can run a variety of changes like general view. There is a textureview.setalpha (1.0f), the default does not write this sentence, its alpha is also 1.0f, that is, opaque.

If you set it to transparent 0.0f, you can see nothing, which is just the opposite of Surfaceview.

Surfaceview Surfaceholder generally want to set a transparent that is transparent. But Textureview because it is a view, no matter what a PNG photo transparency set to 0 must not see anything.

7, assuming that the preview camera this is the mission of Textureview and Surfacetexture, it is a big mistake, the real intention is to seamlessly connect with OpenGL.

--------------------this article is original. Reprint Please specify the author yanzi1225627

Version number: Playcamera_v2.0.0[2014-6-22].zip

csdn Download Link: http://download.csdn.net/detail/yanzi1225627/7540903

Baidu Cloud Disk:



Play to Android Camera Development (ii): Preview camera Base photo demo using Textureview and Surfacetexture

Related Article

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.