Android Open Source - android-camera-example Camera Surface






From Project

Back to project page android-camera-example.

License

The source code is released under:

GNU General Public License

If you think the Android project android-camera-example listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
* Copyright 2013 Devaholic, Inc.  All Rights Reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*//from w  ww  .  j  a v  a  2  s  .c  o  m
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*/
package com.devaholic.android.cameraControls;

import android.content.Context;
import android.hardware.Camera;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

import java.io.IOException;

/**
 * <h1>CameraSurface</h1>
 *
 * <p>
 * Class that will handle the communication between our app and the device's camera
 * </p>
 *
 * @author M. Rabie Hayoun
 * @version 1
 * @since 1
 */
public class CameraSurface extends SurfaceView implements SurfaceHolder.Callback{

    /**
     * Reference to holder that will be listening to any change on the Camera
     */
    private SurfaceHolder callBackHolder = null;

    /**
     * Reference ta a camera instance
     */
    private Camera camera = null;

    public CameraSurface(Context context) {
        super(context);
        this.callBackHolder = getHolder();
        this.callBackHolder.addCallback(this);
    }


    /**
     * If the camera is already in use, we should released it
     * otherwise android will throw
     */
    private void releaseCameraAndPreview() {

        if (this.camera != null) {
            this.camera.stopPreview();
            this.camera.release();
            this.camera = null;
        }
    }

    /**
     * This is called immediately after the surface is first created.
     * Implementations of this should start up whatever rendering code
     * they desire.  Note that only one thread can ever draw into
     * a {@link android.view.Surface}, so you should not draw into the Surface here
     * if your normal rendering will be in another thread.
     *
     * @param holder The SurfaceHolder whose surface is being created.
     */
    @Override
    public void surfaceCreated(SurfaceHolder holder) {
        releaseCameraAndPreview();
        try {
            this.camera = Camera.open();
            this.camera.setPreviewDisplay(this.callBackHolder);
        } catch (IOException e) {
            Log.e("Camera Example",String.format("Cannot create the Camera. Because of: %s",e.getMessage()));
        }

    }

    /**
     * This is called immediately after any structural changes (format or
     * size) have been made to the surface.  You should at this point update
     * the imagery in the surface.  This method is always called at least
     * once, after {@link #surfaceCreated}.
     *
     * @param holder The SurfaceHolder whose surface has changed.
     * @param format The new PixelFormat of the surface.
     * @param width The new width of the surface.
     * @param height The new height of the surface.
     */
    @Override
    public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
        if (this.callBackHolder.getSurface() == null){
            Log.e("Camera Example","The preview area is not set.");
            return;
        }

        try {

            // stop preview before making changes
            this.camera.stopPreview();
            Camera.Parameters param = this.camera.getParameters();
            param.setPreviewSize(width, height);
            this.camera.setParameters(param);

        } catch (Exception e){
            Log.e("Camera Example",String.format("Cannot start preview. Because of: %s",e.getMessage()));
        }
    }

    /**
     * This is called immediately before a surface is being destroyed. After
     * returning from this call, you should no longer try to access this
     * surface.  If you have a rendering thread that directly accesses
     * the surface, you must ensure that thread is no longer touching the
     * Surface before returning from this function.
     *
     * @param holder The SurfaceHolder whose surface is being destroyed.
     */
    @Override
    public void surfaceDestroyed(SurfaceHolder holder) {
        releaseCameraAndPreview();
    }

    /**
     * Take a picture
     * @param jpegHandler handle that will manage what we do with the taken picture
     */
    public void takePicture(Camera.PictureCallback jpegHandler){
        this.camera.takePicture(null,null,jpegHandler);
    }
}




Java Source Code List

com.devaholic.android.activities.HomeActivity.java
com.devaholic.android.activities.SplashScreenActivity.java
com.devaholic.android.activities.camera.CameraThroughAPI.java
com.devaholic.android.activities.camera.CameraThroughIntent.java
com.devaholic.android.cameraControls.CameraSurface.java