diff options
| -rw-r--r-- | ref/CameraActivity.java | 282 | ||||
| -rw-r--r-- | ref/Preview.java | 160 | ||||
| -rw-r--r-- | ref/main.xml | 37 |
3 files changed, 479 insertions, 0 deletions
diff --git a/ref/CameraActivity.java b/ref/CameraActivity.java new file mode 100644 index 0000000..3f15b08 --- /dev/null +++ b/ref/CameraActivity.java @@ -0,0 +1,282 @@ +package com.example.rotorm; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.sql.Date; +import java.text.SimpleDateFormat; + +import android.app.Activity; +import android.content.Context; +import android.hardware.Camera; +import android.hardware.Camera.AutoFocusCallback; +import android.hardware.Camera.PictureCallback; +import android.hardware.Camera.ShutterCallback; +import android.media.CamcorderProfile; +import android.media.MediaRecorder; +import android.net.Uri; +import android.os.Bundle; +import android.os.Environment; +import android.util.Log; +import android.view.SurfaceView; +import android.view.View; +import android.view.View.OnClickListener; +import android.view.View.OnLongClickListener; +import android.view.ViewGroup.LayoutParams; +import android.view.Window; +import android.view.WindowManager; +import android.widget.Button; +import android.widget.FrameLayout; + + +//why is it so hard to put an eclipse project into git +//http://developer.android.com/guide/topics/media/camera.html + +public class CameraActivity extends Activity { + public static final String TAG = "rotorM"; + public static final int MEDIA_TYPE_IMAGE = 1; + public static final int MEDIA_TYPE_VIDEO = 2; + Preview preview; + Button buttonClick; + Camera camera; + String fileName; + Activity act; + Context ctx; + boolean isRecording; + MediaRecorder mMediaRecorder; + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + ctx = this; + act = this; + requestWindowFeature(Window.FEATURE_NO_TITLE); + getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); + + setContentView(R.layout.main); + + preview = new Preview(this, (SurfaceView)findViewById(R.id.surfaceView)); + preview.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); + ((FrameLayout) findViewById(R.id.preview)).addView(preview); + preview.setKeepScreenOn(true); + + buttonClick = (Button) findViewById(R.id.buttonClick); + + buttonClick.setOnClickListener(new OnClickListener() { + public void onClick(View v) { + // preview.camera.takePicture(shutterCallback, rawCallback, jpegCallback); + //camera.takePicture(shutterCallback, rawCallback, jpegCallback); + if (isRecording) { + // stop recording and release camera + mMediaRecorder.stop(); // stop the recording + releaseMediaRecorder(); // release the MediaRecorder object + camera.lock(); // take camera access back from MediaRecorder + + // inform the user that recording has stopped + //setCaptureButtonText("Capture"); + isRecording = false; + } else { + // initialize video camera + if (prepareVideoRecorder()) { + // Camera is available and unlocked, MediaRecorder is prepared, + // now you can start recording + mMediaRecorder.start(); + + // inform the user that recording has started + //setCaptureButtonText("Stop"); + isRecording = true; + } else { + // prepare didn't work, release the camera + releaseMediaRecorder(); + // inform user + } + } + } + }); + + buttonClick.setOnLongClickListener(new OnLongClickListener(){ + @Override + public boolean onLongClick(View arg0) { + camera.autoFocus(new AutoFocusCallback(){ + @Override + public void onAutoFocus(boolean arg0, Camera arg1) { + //camera.takePicture(shutterCallback, rawCallback, jpegCallback); + } + }); + return true; + } + }); + } + + @Override + protected void onResume() { + super.onResume(); + // preview.camera = Camera.open(); + camera = Camera.open(); + camera.startPreview(); + preview.setCamera(camera); + } +/* + @Override + protected void onPause() { + if(camera != null) { + camera.stopPreview(); + preview.setCamera(null); + camera.release(); + camera = null; + } + super.onPause(); + } + */ + + @Override + protected void onPause() { + super.onPause(); + releaseMediaRecorder(); // if you are using MediaRecorder, release it first + releaseCamera(); // release the camera immediately on pause event + } + + private void releaseMediaRecorder(){ + if (mMediaRecorder != null) { + mMediaRecorder.reset(); // clear recorder configuration + mMediaRecorder.release(); // release the recorder object + mMediaRecorder = null; + camera.lock(); // lock camera for later use + } + } + + private void releaseCamera(){ + if (camera != null){ + camera.release(); // release the camera for other applications + camera = null; + } + } + + private void resetCam() { + camera.startPreview(); + preview.setCamera(camera); + } +/* + ShutterCallback shutterCallback = new ShutterCallback() { + public void onShutter() { + // Log.d(TAG, "onShutter'd"); + } + }; + + PictureCallback rawCallback = new PictureCallback() { + public void onPictureTaken(byte[] data, Camera camera) { + // Log.d(TAG, "onPictureTaken - raw"); + } + }; + + PictureCallback jpegCallback = new PictureCallback() { + public void onPictureTaken(byte[] data, Camera camera) { + FileOutputStream outStream = null; + try { + // Write to SD Card + fileName = String.format("/sdcard/camtest/%d.jpg", System.currentTimeMillis()); + outStream = new FileOutputStream(fileName); + outStream.write(data); + outStream.close(); + Log.d(TAG, "onPictureTaken - wrote bytes: " + data.length); + + resetCam(); + + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + } + Log.d(TAG, "onPictureTaken - jpeg"); + } + }; + */ + private boolean prepareVideoRecorder(){ + + camera = getCameraInstance(); + mMediaRecorder = new MediaRecorder(); + + // Step 1: Unlock and set camera to MediaRecorder + camera.unlock(); + mMediaRecorder.setCamera(camera); + + // Step 2: Set sources + mMediaRecorder.setAudioSource(MediaRecorder.AudioSource.CAMCORDER); + mMediaRecorder.setVideoSource(MediaRecorder.VideoSource.CAMERA); + + // Step 3: Set a CamcorderProfile (requires API Level 8 or higher) + mMediaRecorder.setProfile(CamcorderProfile.get(CamcorderProfile.QUALITY_HIGH)); + + // Step 4: Set output file + mMediaRecorder.setOutputFile(getOutputMediaFile(MEDIA_TYPE_VIDEO).toString()); + + // Step 5: Set the preview output + mMediaRecorder.setPreviewDisplay(preview.mHolder.getSurface()); + + // Step 6: Prepare configured MediaRecorder + try { + mMediaRecorder.prepare(); + } catch (IllegalStateException e) { + Log.d(TAG, "IllegalStateException preparing MediaRecorder: " + e.getMessage()); + releaseMediaRecorder(); + return false; + } catch (IOException e) { + Log.d(TAG, "IOException preparing MediaRecorder: " + e.getMessage()); + releaseMediaRecorder(); + return false; + } + return true; + } + /** Create a file Uri for saving an image or video */ + private static Uri getOutputMediaFileUri(int type){ + return Uri.fromFile(getOutputMediaFile(type)); + } + + /** Create a File for saving an image or video */ + private static File getOutputMediaFile(int type){ + // To be safe, you should check that the SDCard is mounted + // using Environment.getExternalStorageState() before doing this. + + File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory( + Environment.DIRECTORY_PICTURES), "MyCameraApp"); + // This location works best if you want the created images to be shared + // between applications and persist after your app has been uninstalled. + + // Create the storage directory if it does not exist + if (! mediaStorageDir.exists()){ + if (! mediaStorageDir.mkdirs()){ + Log.d("MyCameraApp", "failed to create directory"); + return null; + } + } + + // Create a media file name + String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date(type)); + File mediaFile; + if (type == MEDIA_TYPE_IMAGE){ + mediaFile = new File(mediaStorageDir.getPath() + File.separator + + "IMG_"+ timeStamp + ".jpg"); + } else if(type == MEDIA_TYPE_VIDEO) { + mediaFile = new File(mediaStorageDir.getPath() + File.separator + + "VID_"+ timeStamp + ".mp4"); + } else { + return null; + } + + return mediaFile; + } + + private Camera getCameraInstance(){ + Camera c = null; + try { + c = Camera.open(); // attempt to get a Camera instance + //c = this.open(); // attempt to get a Camera instance + } + catch (Exception e){ + // Camera is not available (in use or does not exist) + } + return c; // returns null if camera is unavailable + } +} diff --git a/ref/Preview.java b/ref/Preview.java new file mode 100644 index 0000000..5fdea5a --- /dev/null +++ b/ref/Preview.java @@ -0,0 +1,160 @@ +package com.example.rotorm; + +import java.io.IOException; +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; +import android.view.View; +import android.view.ViewGroup; + +class Preview extends ViewGroup implements SurfaceHolder.Callback { + private final String TAG = "Preview"; + + SurfaceView mSurfaceView; + SurfaceHolder mHolder; + Size mPreviewSize; + List<Size> mSupportedPreviewSizes; + Camera mCamera; + + Preview(Context context, SurfaceView sv) { + super(context); + + mSurfaceView = sv; +// addView(mSurfaceView); + + mHolder = mSurfaceView.getHolder(); + mHolder.addCallback(this); + mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); + } + + public void setCamera(Camera camera) { + mCamera = camera; + if (mCamera != null) { + mSupportedPreviewSizes = mCamera.getParameters().getSupportedPreviewSizes(); + requestLayout(); + + // get Camera parameters + Camera.Parameters params = mCamera.getParameters(); + + List<String> focusModes = params.getSupportedFocusModes(); + if (focusModes.contains(Camera.Parameters.FOCUS_MODE_AUTO)) { + // set the focus mode + params.setFocusMode(Camera.Parameters.FOCUS_MODE_AUTO); + // set Camera parameters + mCamera.setParameters(params); + } + } + } + + @Override + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + // We purposely disregard child measurements because act as a + // wrapper to a SurfaceView that centers the camera preview instead + // of stretching it. + final int width = resolveSize(getSuggestedMinimumWidth(), widthMeasureSpec); + final int height = resolveSize(getSuggestedMinimumHeight(), heightMeasureSpec); + setMeasuredDimension(width, height); + + if (mSupportedPreviewSizes != null) { + mPreviewSize = getOptimalPreviewSize(mSupportedPreviewSizes, width, height); + } + } + + @Override + protected void onLayout(boolean changed, int l, int t, int r, int b) { + if (changed && getChildCount() > 0) { + final View child = getChildAt(0); + + final int width = r - l; + final int height = b - t; + + int previewWidth = width; + int previewHeight = height; + if (mPreviewSize != null) { + previewWidth = mPreviewSize.width; + previewHeight = mPreviewSize.height; + } + + // Center the child SurfaceView within the parent. + if (width * previewHeight > height * previewWidth) { + final int scaledChildWidth = previewWidth * height / previewHeight; + child.layout((width - scaledChildWidth) / 2, 0, + (width + scaledChildWidth) / 2, height); + } else { + final int scaledChildHeight = previewHeight * width / previewWidth; + child.layout(0, (height - scaledChildHeight) / 2, + width, (height + scaledChildHeight) / 2); + } + } + } + + public void surfaceCreated(SurfaceHolder holder) { + // The Surface has been created, acquire the camera and tell it where + // to draw. + try { + if (mCamera != null) { + mCamera.setPreviewDisplay(holder); + } + } catch (IOException exception) { + Log.e(TAG, "IOException caused by setPreviewDisplay()", exception); + } + } + + public void surfaceDestroyed(SurfaceHolder holder) { + // Surface will be destroyed when we return, so stop the preview. + if (mCamera != null) { + mCamera.stopPreview(); + } + } + + + private Size getOptimalPreviewSize(List<Size> sizes, int w, int h) { + final double ASPECT_TOLERANCE = 0.1; + double targetRatio = (double) w / h; + if (sizes == null) return null; + + Size optimalSize = null; + double minDiff = Double.MAX_VALUE; + + int targetHeight = h; + + // Try to find an size match aspect ratio and size + for (Size size : sizes) { + double ratio = (double) size.width / size.height; + if (Math.abs(ratio - targetRatio) > ASPECT_TOLERANCE) continue; + if (Math.abs(size.height - targetHeight) < minDiff) { + optimalSize = size; + minDiff = Math.abs(size.height - targetHeight); + } + } + + // Cannot find the one match the aspect ratio, ignore the requirement + if (optimalSize == null) { + minDiff = Double.MAX_VALUE; + for (Size size : sizes) { + if (Math.abs(size.height - targetHeight) < minDiff) { + optimalSize = size; + minDiff = Math.abs(size.height - targetHeight); + } + } + } + return optimalSize; + } + + public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { + if(mCamera != null) { + Camera.Parameters parameters = mCamera.getParameters(); + parameters.setPreviewSize(mPreviewSize.width, mPreviewSize.height); + requestLayout(); + + mCamera.setParameters(parameters); + mCamera.startPreview(); + } + } + +} diff --git a/ref/main.xml b/ref/main.xml new file mode 100644 index 0000000..be086c3 --- /dev/null +++ b/ref/main.xml @@ -0,0 +1,37 @@ +<?xml version="1.0" encoding="utf-8"?> +<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" + android:id="@+id/layout" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:orientation="vertical" > + + <FrameLayout + android:id="@+id/preview" + android:layout_width="fill_parent" + android:layout_height="fill_parent" + android:layout_weight="1" > + + <SurfaceView + android:id="@+id/surfaceView" + android:layout_width="wrap_content" + android:layout_height="wrap_content" /> + + <LinearLayout + android:layout_width="fill_parent" + android:layout_height="fill_parent" >" + <LinearLayout + android:layout_width="fill_parent" + android:layout_height="2px" + android:layout_gravity="center_vertical" + android:background="#ff0000" + android:layout_weight=".80" /> + <Button + android:id="@+id/buttonClick" + android:layout_width="wrap_content" + android:layout_height="wrap_content" + android:layout_gravity="center_vertical" + android:text="Click" /> + </LinearLayout> + </FrameLayout> + +</LinearLayout>
\ No newline at end of file |
