Android Development--Custom camera Development Summary

Source: Internet
Author: User

Recently I have been developing a custom camera, Google some online demo, found that there are a lot of various problems. Finally, start learning from the API's camera class and make improvements. Here are some summaries of the previous implementations.

Official Camera API:
Http://developer.android.com/guide/topics/media/camera.html
Chinese translation:
Http://www.cnblogs.com/over140/archive/2011/11/16/2251344.html

Customize the camera to achieve the approximate process:


Preview camera This one, there are three classes can be implemented: Surfaceview, Glsurfaceview, Textureview. The Android4.0 camera app uses a surfaceview preview, Android4.2 is previewed with Glsurfaceview. Android4.4 uses Textureview.
The difference between the three and contact please see this article in Android 5.0 (Lollipop) in Surfacetexture,textureview, Surfaceview and Glsurfaceview
Here I summarize the development of custom cameras with Surfaceview preview camera.

1. manifest Statement
Make sure to add camera permissions, store permissions, and so on in the manifest file.
2. Create a preview class
Use the Surfaceview control to implement a real-time camera preview screen.
Surfaceview is a subclass of view, so it has all the methods and properties of view. At the same time, the extra surface is dedicated to drawing classes.
Surfaceview has the following three features:

    A. 具有独立的绘图表面;    B. 需要在宿主窗口上挖一个洞来显示自己;    C. 它的UI绘制可以在独立的线程中进行,这样就可以进行复杂的UI绘制,并且不会影响应用程序的主线程响应用户输入。

The implementation principle analysis of Surfaceview from Android view
Create a class that inherits Surfaceview and implements Surfaceholder.callback, Surfaceholder.callback is used to listen for changes in surface, implementing three methods:
(1) surfacecreated (Surfaceholder holder): Called when the class is created. To open the camera operation, how to implement see next.
(2) surfacechanged (surfaceholder holder, int format, int width,int height): Called when surface changes, which enables drawing of the graph. To start the preview operation, how to implement see next.
(3) surfacedestroyed (Surfaceholder holder): Called when surface is destroyed, where resources are generally released. To release the camera and other operations.
3. Package Camerainterface class
Create a Camerainterface class using a singleton mode, which operates camera detection, opening, previewing, taking pictures, closing, and saving callbacks for images.

CheckCamera(Context mContext)doOpenCamera(Context mContext,int cameraId)doStartPreview(SurfaceHolder holder, float previewRate)doStopCamera()...

So in the previous step, we just need to get the Camerainterface instance and call the method directly. This approach reduces the UI coupling of the camera's logic and interface. It also ensures the extensibility and reusability of the program.
4. Creating a custom layout

The second step of the camera preview class must be placed in an activity layout, together with other user interface controls (photo button, camera switch), to achieve photo or video function. Note that to use Framelayout to accommodate the camera preview class, you can overlay additional picture information or controls onto the live preview screen. For example, to achieve a real-time preview of the dark surrounding, we need to customize the view here.
5. Create a camera activity
Here is the control registration, monitoring the control click event and so on.
The same takepicture take pictures, and after switching the camera, recreating the camera instance is encapsulated in the Camerainterface class.
6. Take photos and save pictures
The photo takes place in the previous step of the control click event, triggering Mcamera.takepicture (Mshuttercallback, Mrawcallback, Mjpegpicturecallback);
Here, you need to implement the shutter sound for the photo, and the callback method for taking photos. are also implemented in the Singleton class Camerainterface.
Also pay attention to the requirements, if you continue to preview the real-time after the photo, you want to trigger Dostartpreview again. If it is jump to preview activity then jump through intent.
7. Release the camera

    /**     * 停止预览,释放Camera     */    publicvoiddoStopCamera(){        if(null != mCamera)        {            mCamera.setPreviewCallback(null);            mCamera.stopPreview();             false;             mPreviwRate = -1f;            mCamera.release();            null;             }    }

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Android Development--Custom camera Development Summary

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.