Android Open Source - compCellScope Acquire Surface View






From Project

Back to project page compCellScope.

License

The source code is released under:

MIT License

If you think the Android project compCellScope 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.wallerlab.compcellscope;
/*from  www .j a  v a 2s .co  m*/
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.ImageFormat;
import android.graphics.Matrix;
import android.graphics.Rect;
import android.graphics.YuvImage;
import android.hardware.Camera;
import android.hardware.Camera.PictureCallback;
import android.hardware.Camera.PreviewCallback;
import android.hardware.Camera.ShutterCallback;
import android.os.Environment;
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.SurfaceView;

/** A basic Camera preview class */
public class AcquireSurfaceView extends SurfaceView implements SurfaceHolder.Callback {

  private SurfaceHolder mHolder;
    private Camera mCamera;
    
    PictureCallback rawCallback;
    ShutterCallback shutterCallback;
    PictureCallback jpegCallback;
    public String fileName = "default";
    
    // Debugging
    private static final String TAG = "CellScopeAcquireActivity";
  
  public void setFileName(String fName)
  {
    fileName = fName;
  }

    @SuppressWarnings("deprecation")
  public AcquireSurfaceView(Context context, Camera camera) {
        super(context);
        mCamera = camera;

        // Install a SurfaceHolder.Callback so we get notified when the
        // underlying surface is created and destroyed.
        mHolder = getHolder();
        mHolder.addCallback(this);
        
        // deprecated setting, but required on Android versions prior to 3.0
        mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
    }

    @Override
  public void surfaceCreated(SurfaceHolder holder) {
        // The Surface has been created, now tell the camera where to draw the preview.
        try {
            mCamera.setPreviewDisplay(holder);
            mCamera.startPreview();
        } catch (IOException e) {
            Log.d(TAG, "Error setting camera preview: " + e.getMessage());
        }
        /** Handles data for jpeg picture */
        shutterCallback = new ShutterCallback() {
            @Override
      public void onShutter() {
                Log.i("Log", "onShutter'd");
            }
        };
        jpegCallback = new PictureCallback() {
            @Override
      public void onPictureTaken(byte[] data, Camera camera) 
            {
                FileOutputStream outStream = null;
                   try {
                     String imageFileName = fileName + ".jpeg";
                     File storageDir = Environment.getExternalStorageDirectory();
                     String path = storageDir+imageFileName;
                     outStream = new FileOutputStream(String.format(path));
                       outStream.write(data);
                       outStream.close();
                       Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length + " to path: " + path);
                   } catch (FileNotFoundException e) {
                     Log.d(TAG, "onPictureTaken - jpeg - directory not found");
                       e.printStackTrace();
                   } catch (IOException e) {
                       e.printStackTrace();
                       Log.d(TAG, "onPictureTaken - jpeg - IO Exception");
                   } finally {
                   }
                   Log.d(TAG, "onPictureTaken - jpeg");
                   camera.startPreview();
            }
        };
    }

    @Override
  public void surfaceDestroyed(SurfaceHolder holder) {
      mCamera.stopPreview();
      mCamera.release();
    }
    
    

    @Override
  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
          return;
        }

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

        // set preview size and make any resize, rotate or
        // reformatting changes here
        mCamera.setDisplayOrientation(90);
        
        // start preview with new settings
        try {
            mCamera.setPreviewDisplay(mHolder);
            mCamera.startPreview();

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

            mCamera.takePicture(shutterCallback, rawCallback, jpegCallback);
    }
    
    //Burst Mode Stuff
    
  PreviewCallback previewCallback = new PreviewCallback() {
    public void onPreviewFrame(byte[] data, Camera camera) {
      Camera.Parameters parameters = camera.getParameters();
      int w = parameters.getPreviewSize().width;
      int h = parameters.getPreviewSize().height;

      FileOutputStream outStream = null;
      String path = Environment
          .getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES)
          + File.separator + "cellMicroBurst";
      File outputDir = new File(path);
      outputDir.mkdirs();
      
      // Get the YuV image
      YuvImage yuv_image = new YuvImage(data, ImageFormat.NV21, w, h, null);
      Rect rect = new Rect(0, 0, w, h);
      Bitmap image = rotateBitmap(yuv_image, 90, rect);
      
      
      try {
        outStream = new FileOutputStream(String.format(path
            + "/preview%d.jpeg", System.currentTimeMillis()));
        image.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
        //outStream.write(byt);
        outStream.close();
      } catch (FileNotFoundException e) {
        Log.d(TAG, "File not created");
        e.printStackTrace();
      } catch (IOException e) {
        e.printStackTrace();
      } finally {
      }
    }
  };
    
    
    private Bitmap rotateBitmap(YuvImage yuvImage, int orientation, Rect rectangle)
    {
      ByteArrayOutputStream os = new ByteArrayOutputStream();
      yuvImage.compressToJpeg(rectangle, 100, os);
  
      Matrix matrix = new Matrix();
      matrix.postRotate(orientation);
      byte[] bytes = os.toByteArray();
      Bitmap bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
      return Bitmap.createBitmap(bitmap, 0 , 0, bitmap.getWidth(), bitmap.getHeight(), matrix, true);
    }
    
    public void previewCallback(){
      mCamera.setOneShotPreviewCallback(previewCallback);
    }
    
    
}




Java Source Code List

com.wallerlab.compcellscope.AcquireActivity.java
com.wallerlab.compcellscope.AcquireActivity.java
com.wallerlab.compcellscope.AcquireSettings.java
com.wallerlab.compcellscope.AcquireSurfaceView.java
com.wallerlab.compcellscope.AcquireSurfaceView.java
com.wallerlab.compcellscope.BluetoothActivity.java
com.wallerlab.compcellscope.BluetoothApplicationClass.java
com.wallerlab.compcellscope.BluetoothApplicationClass.java
com.wallerlab.compcellscope.BluetoothDeviceListActivity.java
com.wallerlab.compcellscope.BluetoothDeviceListActivity.java
com.wallerlab.compcellscope.BluetoothService.java
com.wallerlab.compcellscope.BluetoothService.java
com.wallerlab.compcellscope.BurstModeActivity.java
com.wallerlab.compcellscope.DirectoryChooserDialog.java
com.wallerlab.compcellscope.Folder_Chooser.java
com.wallerlab.compcellscope.ImageProgressTask.java
com.wallerlab.compcellscope.ImageProgressTask.java
com.wallerlab.compcellscope.Image_Gallery.java
com.wallerlab.compcellscope.MainActivity.java
com.wallerlab.compcellscope.MainActivity.java
com.wallerlab.compcellscope.MultiModeViewActivity.java
com.wallerlab.compcellscope.MultiModeView.java
com.wallerlab.compcellscope.PortraitCameraView.java