package com.example.rotorm; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; 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.MediaRecorder; import android.os.Bundle; 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"; 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"); } }; */ }