Android custom Camera (1)

Android custom camera , has a basic camera, front and rear camera , flash mode , delay shooting , framing square frame , add watermark effect solution

Get the camera and release the camera

First of all, there can only be a camera instance, after the camera must be released, then it also has its own life cycle, first get the camera, where an ID, this id can be set before the camera and rear camera, behind the blog I will join Code.

     * getCamera
     * @return
    private Camera getCamera(int id){
        Camera camera = null;
            camera =;
        }catch (Exception e){

        return camera;

     * releaseCamera
    private void releaseCamera(){
        if(mCamera != null){
            mCamera = null;

Use the release of the camera after the use of resources, here is a reason to write, one step can not be less, I sometimes forget to write mCamera.setPreviewCallback (null);

Because the camera to real-time preview, that ordinary View can not meet the requirements of the drawing, and here to use a double buffer mechanism SurfaceView, the following we have to do is to Camera and SurfaceView binding, it needs another class The SurfaceHolder.

Here is a special mention of CameraUtil.getInstance (). SetCameraDisplayOrientation (this, mCameraId, camera);

I will write the source address, because the default preview of the Android system are horizontal, the basic operation is camera.setDisplayOrientation (90); direct rotation 90 degrees correction, but I was in the development of some mobile phones on the preview Came, and finally use this method to solve, and its basic principle is that some of the bottom of the mobile phone system on the preview of the preview, and some did not, that can be judged by the cameraInfo. SO this perfect solution to my problem, hope can help you:

     * Preview
    private void startPreview(Camera camera, SurfaceHolder holder){
        try {
            CameraUtil.getInstance().setCameraDisplayOrientation(this, mCameraId, camera);
//            camera.setDisplayOrientation(90);
        } catch (IOException e) {

Set the SurfaceHolder

surfaceView = (SurfaceView)findViewById(;
        mHolder = surfaceView.getHolder();

Where SurfaceHolder wants to add a callback method to see the comment:

    public void surfaceCreated(SurfaceHolder holder) {
    //Open the camera preview when the surface is created
        startPreview(mCamera, holder);

    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
    //Call this method when the camera changes, at which point you should stop previewing and then restart
        startPreview(mCamera, holder);

    public void surfaceDestroyed(SurfaceHolder holder) {
    //Release the camera resources when destroy

Here to elaborate on the setCamera method, without this method can also be a normal preview, but this method to set the camera’s preview size, return to the picture size, in particular, note that the preview size returns the size of the picture and the size of the surfaceView ratio Must be the same, or else the program crashes, see the code below

     * Setting
    private void setupCamera(Camera camera) {
        Camera.Parameters parameters = camera.getParameters();

        List<String> focusModes = parameters.getSupportedFocusModes();
        if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) {
            // Autofocus mode is supported auto focus

        //Where the third parameter is the smallest size getPropPreviewSize Method will remove the minimum size of all supported sizes from the minimum size to the ascending order
        Camera.Size previewSize = CameraUtil.getInstance().getPropPreviewSize(parameters.getSupportedPreviewSizes(), 1000);
        parameters.setPreviewSize(previewSize.width, previewSize.height);

        Camera.Size pictrueSize = CameraUtil.getInstance().getPropPictureSize(parameters.getSupportedPictureSizes(), 1000);
        parameters.setPictureSize(pictrueSize.width, pictrueSize.height);


        Log.d("previewSize.width===", previewSize.width + "");
        Log.d("previewSize.height===", previewSize.height + "");

         * Set the size of the surfaceView Because the camera default is horizontal screen, so get the support size are also horizontal screen size
         * We in the startPreview method inside it correction over, but here we set the size of the surfaceView to pay attention to the time previewSize.height<previewSize.width
         * The general camera is the width of the screen here set to the screen width height adaptive you can also set the size you want
        FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(screenWidth, screenWidth * previewSize.width/previewSize.height);
        //Here, of course, you can set the camera position, such as the center of the top here
        //params.gravity = Gravity.CENTER;

There is also a place to pay special attention to is that you set the preview size and return the image size must be the current size of the phone support.

Here through getPropPictureSize and getPropPreviewSize get all the phone support the size of .the default camera preview are horizontal screen.
Here you can set a minimum preview width of 1000, as long as more than 1000 support size can be. Here is the last step is also very important, that is, Camera’s life cycle must be bound with the current Activity, so we do so:

    protected void onResume() {
        if(mCamera == null){
            mCamera = getCamera(mCameraId);
            if(mHolder != null){
                startPreview(mCamera, mHolder);

    protected void onPause() {

Here in the Activity to display when you start the camera preview, in the Activity onPause when the release of the camera resources on it. It can now preview can also take pictures wow, then take a camera method.

private void captrue() {
        mCamera.takePicture(null, null, new Camera.PictureCallback() {
            public void onPictureTaken(byte[] data, Camera camera) {
                //Convert data to a bitmap or you can save it directly as a file FileOutputStream
                //Here I believe that most of the other useful to add a watermark, such as follow-up and then explain
               Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length);
               CameraUtil.getInstance().setTakePicktrueOrientation(mCameraId, bitmap);
                //Where you can see the width of the print CameraUtil.getInstance().getPropPictureSize(parameters.getSupportedPictureSizes(), 200);
                // The minimum width of this setting affects the size of the returned picture so it is generally here that is around 1000
                Log.d("bitmapWidth==", bitmap.getWidth() + "");
                Log.d("bitmapHeight==", bitmap.getHeight() + "");

Here we have a place to say that the above we have made a preview of the preview, that generated the picture we have to correct back ah, the implementation of CameraUtil.getInstance (). SetTakePicktrueOrientation (mCameraId, bitmap); of course, through Android.hardware.Camera. CameraInfo to operate.

The following is the source address:

  1. […] the introduction of an Android custom Camera (一), we have been able to complete a camera based on the demo, of course, there are a lot of attention […]



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: