Android Open Source - android-camera-example Camera Through A P I






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  2s .co 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.activities.camera;

import android.content.Intent;
import android.graphics.BitmapFactory;
import android.hardware.Camera;
import android.os.Bundle;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.Toast;

import com.devaholic.android.R;
import com.devaholic.android.cameraControls.CameraSurface;

import roboguice.activity.RoboActivity;
import roboguice.inject.ContentView;
import roboguice.inject.InjectResource;
import roboguice.inject.InjectView;

/**
 * <h1>CameraThroughAPI</h1>
 *
 * <p>
 *  Activity that will handle the API Camera Directly
 * </p>
 *
 * @author M. Rabie Hayoun
 * @version 1
 * @since 1
 */
@ContentView(R.layout.camera_throught_api_activity)
public class CameraThroughAPI extends RoboActivity {

    /**
     * Reference to the service that will manage the camera
     */
    private CameraSurface cameraSurface = null;

    /**
     * Injecting the element where we are going to display the picture
     */
    @InjectView(R.id.imgPreviewApi)
    private ImageView imgView;

    /**
     * Check if the preview mode is started
     */
    private boolean isCameraStarted = false;

    /**
     * Injecting the frame where we are going to display the picture
     */
    @InjectView(R.id.cameraFrame)
    private FrameLayout frame;

    @InjectResource(R.string.startPreviewMode)
    private String startPreviewModeMessage;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        cameraSurface = new CameraSurface(getApplicationContext());
        frame.addView(cameraSurface);
        frame.bringChildToFront(imgView);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    /**
     * Method on charge to start the camera preview
     * @param view
     */
    public void startPreview(View view){
        frame.bringChildToFront(cameraSurface);
        imgView.setImageBitmap(null);
        isCameraStarted = true;
    }

    /**
     * Method that handles click event on button
     * @param view reference to the view that triggered the event
     */
    public void takePicture(View view){
        if(isCameraStarted){
            this.cameraSurface.takePicture(new Camera.PictureCallback(){
                @Override
                public void onPictureTaken(byte[] data, Camera camera) {
                    imgView.setImageBitmap(BitmapFactory.decodeByteArray(data,0,data.length));
                    frame.bringChildToFront(imgView);
                    isCameraStarted = false;
                }
            });
        }else{
            Toast toast = Toast.makeText(getApplicationContext(),startPreviewModeMessage, Toast.LENGTH_SHORT);
            toast.show();
        }
    }
}




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