Android Open Source - Sunset Camera Preview

From Project

Back to project page Sunset.


The source code is released under:

GNU General Public License

If you think the Android project Sunset 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

package edu.rosehulman.mobilesunset;
//from   w w  w  .  j  av  a 2 s . c  o  m
import java.util.List;

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

/** A basic Camera preview class */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {
    private SurfaceHolder mHolder;
    private Camera mCamera;

    public CameraPreview(Context context, Camera camera) {
        mCamera = camera;
        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        // deprecated setting, but required on Android versions prior to 3.0

  private void setImageSize() {
        Camera.Parameters cp = mCamera.getParameters();
//        List<Size> pictureSizesList = cp.getSupportedPictureSizes();
        //cp.setPictureSize(640, 480);

    public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
        } catch (IOException e) {
            Log.d(MobileSunset.TAG, "Error setting camera preview: " + e.getMessage());

    public void surfaceDestroyed(SurfaceHolder holder) {
        // empty. Take care of releasing the Camera preview in your activity.

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        // If your preview can change or rotate, take care of those events here.
        // Make sure to stop the preview before resizing or reformatting it.

        if (mHolder.getSurface() == null){
          // preview surface does not exist

        // stop preview before making changes
        try {
        } catch (Exception e){
          // ignore: tried to stop a non-existent preview

        // set preview size and make any resize, rotate or
        // reformatting changes here

        // start preview with new settings
        try {

        } catch (Exception e){
            Log.d(MobileSunset.TAG, "Error starting camera preview: " + e.getMessage());

  public void onPreviewFrame(byte[] data, Camera camera) {
    // TODO Auto-generated method stub

Java Source Code List