Android Open Source - PhotoStrips Camera Preview






From Project

Back to project page PhotoStrips.

License

The source code is released under:

MIT License

If you think the Android project PhotoStrips 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 com.ahmetkizilay.image.photostrips;
//from   ww  w  .j a v  a2s  .  c o  m
import java.io.IOException;

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

/***
 * Another piece of code I found online to prepare camera preview
 * @author ahmetkizilay
 *
 */
public class CameraPreview extends SurfaceView implements SurfaceHolder.Callback {
  private SurfaceHolder mHolder;
  private Camera mCamera;
  public CameraPreview(Context context, Camera camera) {
    super(context);
    mCamera = camera;

    mHolder = getHolder();
    mHolder.addCallback(this);

    // deprecated setting, but required on Android versions prior to 3.0
    mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
  }

  public void surfaceCreated(SurfaceHolder holder) {
    try {
      mCamera.setPreviewDisplay(holder);
      mCamera.startPreview();
    } catch (IOException e) {
      Log.d("CamPrev", "Error setting camera preview: " + e.getMessage());
    } catch (NullPointerException npe) {
      Log.d("CamPrev", "Error setting camera prev:" + npe.getMessage());
    }
  }

  public void surfaceDestroyed(SurfaceHolder holder) {

    if (mCamera != null) {
      try {
        mCamera.stopPreview();
        mCamera.setPreviewCallback(null);
        mCamera.release();
      } catch (Exception exp) {
        Log.d("CamPrev", "Error Surface Destroyed:" + exp.getMessage());
      }
      mCamera = null;
    }
  }

  public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
    if (mHolder.getSurface() == null) {
      return;
    }

    try {
      mCamera.stopPreview();
    } catch (Exception e) {
    }


    try {
      mCamera.setPreviewDisplay(mHolder);
      mCamera.startPreview();

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

    public void forceSurfaceDestroyed() {
        this.surfaceDestroyed(this.mHolder);
    }
}




Java Source Code List

com.ahmetkizilay.image.photostrips.AltGalleryActivity.java
com.ahmetkizilay.image.photostrips.CameraPreview.java
com.ahmetkizilay.image.photostrips.PhotoBoothActivity.java
com.ahmetkizilay.image.photostrips.PhotoCreator.java
com.ahmetkizilay.image.photostrips.ViewImageActivity.java
com.ahmetkizilay.image.photostrips.dialogs.AboutMeDialogFragment.java
com.ahmetkizilay.image.photostrips.dialogs.CompletionDialogFragment.java
com.ahmetkizilay.image.photostrips.dialogs.ConfirmDialogFragment.java
com.ahmetkizilay.image.photostrips.dialogs.PhotoCreationDialogFragment.java
com.ahmetkizilay.image.photostrips.utils.CustomHorizontalScrollView.java
com.ahmetkizilay.image.photostrips.utils.GalleryItemAdapter.java
com.ahmetkizilay.image.photostrips.utils.OnDoubleTapListener.java
com.ahmetkizilay.image.photostrips.utils.OnMultiTapListener.java
com.ahmetkizilay.image.photostrips.utils.TouchImageView.java
com.ahmetkizilay.image.photostrips.utils.TransportViewGroup.java