diff options
| author | Tim Redfern <tim@eclectronics.org> | 2013-09-09 12:38:17 +0100 |
|---|---|---|
| committer | Tim Redfern <tim@eclectronics.org> | 2013-09-09 12:38:17 +0100 |
| commit | a2e3dedbe4e09c4299143110bf938956c74ffe6c (patch) | |
| tree | df3804c8d7f144628a8d659d74f1f72e1f4fda18 | |
| parent | 312a9dcf702f662ffbbd96690bbc57b33af3445d (diff) | |
integrated working example
| -rw-r--r-- | AndroidManifest.xml | 28 | ||||
| -rw-r--r-- | bin/AndroidManifest.xml | 28 | ||||
| -rw-r--r-- | bin/classes/com/example/rotorm/MainActivity.class | bin | 7812 -> 0 bytes | |||
| -rw-r--r-- | bin/classes/com/example/rotorm/R$id.class | bin | 431 -> 524 bytes | |||
| -rw-r--r-- | bin/classes/com/example/rotorm/R$layout.class | bin | 431 -> 394 bytes | |||
| -rw-r--r-- | bin/classes/com/example/rotorm/R$menu.class | bin | 388 -> 388 bytes | |||
| -rw-r--r-- | bin/classes/com/example/rotorm/R$string.class | bin | 472 -> 472 bytes | |||
| -rw-r--r-- | bin/classes/com/example/rotorm/R$style.class | bin | 431 -> 431 bytes | |||
| -rw-r--r-- | gen/com/example/rotorm/R.java | 10 | ||||
| -rw-r--r-- | ref/MainActivity.java (renamed from src/com/example/rotorm/MainActivity.java) | 19 | ||||
| -rw-r--r-- | res/layout/activity_main.xml | 16 | ||||
| -rw-r--r-- | res/layout/main.xml | 40 | ||||
| -rw-r--r-- | src/com/example/rotorm/CameraActivity.java | 134 | ||||
| -rw-r--r-- | src/com/example/rotorm/Preview.java | 160 |
14 files changed, 381 insertions, 54 deletions
diff --git a/AndroidManifest.xml b/AndroidManifest.xml index 114166c..40bfce5 100644 --- a/AndroidManifest.xml +++ b/AndroidManifest.xml @@ -5,28 +5,34 @@ android:versionName="1.0" > <uses-sdk - android:minSdkVersion="17" - android:targetSdkVersion="17" /> + android:minSdkVersion="18" + android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> + <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > - <activity - android:name="com.example.rotorm.MainActivity" - android:label="@string/app_name" > - <intent-filter> - <action android:name="android.intent.action.MAIN" /> - - <category android:name="android.intent.category.LAUNCHER" /> - </intent-filter> - </activity> + <activity android:name=".CameraActivity" + android:label="@string/app_name" + + android:screenOrientation="landscape"> + <!-- configure this activity to use landscape orientation --> + + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> </application> + + </manifest> diff --git a/bin/AndroidManifest.xml b/bin/AndroidManifest.xml index 114166c..40bfce5 100644 --- a/bin/AndroidManifest.xml +++ b/bin/AndroidManifest.xml @@ -5,28 +5,34 @@ android:versionName="1.0" > <uses-sdk - android:minSdkVersion="17" - android:targetSdkVersion="17" /> + android:minSdkVersion="18" + android:targetSdkVersion="18" /> <uses-permission android:name="android.permission.CAMERA" /> <uses-feature android:name="android.hardware.camera" /> <uses-feature android:name="android.hardware.camera.autofocus" /> + <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> + <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > - <activity - android:name="com.example.rotorm.MainActivity" - android:label="@string/app_name" > - <intent-filter> - <action android:name="android.intent.action.MAIN" /> - - <category android:name="android.intent.category.LAUNCHER" /> - </intent-filter> - </activity> + <activity android:name=".CameraActivity" + android:label="@string/app_name" + + android:screenOrientation="landscape"> + <!-- configure this activity to use landscape orientation --> + + <intent-filter> + <action android:name="android.intent.action.MAIN" /> + <category android:name="android.intent.category.LAUNCHER" /> + </intent-filter> + </activity> </application> + + </manifest> diff --git a/bin/classes/com/example/rotorm/MainActivity.class b/bin/classes/com/example/rotorm/MainActivity.class Binary files differdeleted file mode 100644 index 149638b..0000000 --- a/bin/classes/com/example/rotorm/MainActivity.class +++ /dev/null diff --git a/bin/classes/com/example/rotorm/R$id.class b/bin/classes/com/example/rotorm/R$id.class Binary files differindex 9c7852d..3a3dc17 100644 --- a/bin/classes/com/example/rotorm/R$id.class +++ b/bin/classes/com/example/rotorm/R$id.class diff --git a/bin/classes/com/example/rotorm/R$layout.class b/bin/classes/com/example/rotorm/R$layout.class Binary files differindex 2ba895d..d5f016a 100644 --- a/bin/classes/com/example/rotorm/R$layout.class +++ b/bin/classes/com/example/rotorm/R$layout.class diff --git a/bin/classes/com/example/rotorm/R$menu.class b/bin/classes/com/example/rotorm/R$menu.class Binary files differindex 8fd5581..88bf13a 100644 --- a/bin/classes/com/example/rotorm/R$menu.class +++ b/bin/classes/com/example/rotorm/R$menu.class diff --git a/bin/classes/com/example/rotorm/R$string.class b/bin/classes/com/example/rotorm/R$string.class Binary files differindex 868990e..bff6cf5 100644 --- a/bin/classes/com/example/rotorm/R$string.class +++ b/bin/classes/com/example/rotorm/R$string.class diff --git a/bin/classes/com/example/rotorm/R$style.class b/bin/classes/com/example/rotorm/R$style.class Binary files differindex 9b41eed..54c540c 100644 --- a/bin/classes/com/example/rotorm/R$style.class +++ b/bin/classes/com/example/rotorm/R$style.class diff --git a/gen/com/example/rotorm/R.java b/gen/com/example/rotorm/R.java index f070dbb..3371583 100644 --- a/gen/com/example/rotorm/R.java +++ b/gen/com/example/rotorm/R.java @@ -24,12 +24,14 @@ public final class R { public static final int ic_launcher=0x7f020000; } public static final class id { - public static final int action_settings=0x7f080001; - public static final int surface_camera=0x7f080000; + public static final int action_settings=0x7f080004; + public static final int buttonClick=0x7f080003; + public static final int layout=0x7f080000; + public static final int preview=0x7f080001; + public static final int surfaceView=0x7f080002; } public static final class layout { - public static final int activity_main=0x7f030000; - public static final int main=0x7f030001; + public static final int main=0x7f030000; } public static final class menu { public static final int main=0x7f070000; diff --git a/src/com/example/rotorm/MainActivity.java b/ref/MainActivity.java index 9ed025f..a49f299 100644 --- a/src/com/example/rotorm/MainActivity.java +++ b/ref/MainActivity.java @@ -6,6 +6,8 @@ import java.io.IOException; import java.util.Date; import java.util.List; +import com.example.rotorm.R; + import android.hardware.Camera; import android.hardware.Camera.Parameters; import android.media.MediaPlayer; @@ -23,8 +25,6 @@ import android.view.SurfaceView; import android.view.SurfaceHolder.Callback; import android.widget.Toast; -//why is it so hard to put an eclipse project into git - public class MainActivity extends Activity implements Callback { @Override @@ -98,8 +98,11 @@ public class MainActivity extends Activity implements Callback { protected void startPreview() throws IOException { + + //http://stackoverflow.com/questions/12098298/android-camera-app-passed-null-surface //http://developer.android.com/training/camera/cameradirect.html + //http://developer.android.com/guide/topics/media/camera.html#custom-camera if (mCamera!=null){ Log.e("Rotor", "Nulling camera"); @@ -111,7 +114,7 @@ public class MainActivity extends Activity implements Callback { if(mCamera == null){ mCamera = Camera.open(); - Log.e("Rotor", "Setting up camera"); + Log.i("Rotor", "Setting up camera"); /* Camera.Parameters parameters = mCamera.getParameters(); @@ -128,8 +131,8 @@ public class MainActivity extends Activity implements Callback { List<String> focus = parameters.getSupportedFocusModes(); parameters.setFocusMode(focus.get(0)); - */ + @@ -147,9 +150,9 @@ public class MainActivity extends Activity implements Callback { mCamera.setParameters(parameters); Log.e("Rotor", "Camera configured"); } + /* - - if(mCamera==null) mCamera = Camera.open(); + //if(mCamera==null) mCamera = Camera.open(); setContentView(R.layout.main); @@ -170,6 +173,8 @@ public class MainActivity extends Activity implements Callback { //mCamera.setPreviewCallback(new cam_PreviewCallback()); mCamera.setPreviewDisplay(surfaceHolder); mCamera.startPreview(); + + */ } @@ -303,12 +308,14 @@ public class MainActivity extends Activity implements Callback { //this.getIntent().getStringExtra(temp); //uriSound=Uri.parse(temp); //uriSound=Uri.parse("/Music/stick.mp3"); + try { startPreview(); } catch (IOException e) { Log.e("Preview","Error"); e.printStackTrace(); } + //} } diff --git a/res/layout/activity_main.xml b/res/layout/activity_main.xml deleted file mode 100644 index 168c9b8..0000000 --- a/res/layout/activity_main.xml +++ /dev/null @@ -1,16 +0,0 @@ -<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" - xmlns:tools="http://schemas.android.com/tools" - android:layout_width="match_parent" - android:layout_height="match_parent" - android:paddingBottom="@dimen/activity_vertical_margin" - android:paddingLeft="@dimen/activity_horizontal_margin" - android:paddingRight="@dimen/activity_horizontal_margin" - android:paddingTop="@dimen/activity_vertical_margin" - tools:context=".MainActivity" > - - <TextView - android:layout_width="wrap_content" - android:layout_height="wrap_content" - android:text="@string/hello_world" /> - -</RelativeLayout> diff --git a/res/layout/main.xml b/res/layout/main.xml index 40bfa59..be086c3 100644 --- a/res/layout/main.xml +++ b/res/layout/main.xml @@ -1,9 +1,37 @@ <?xml version="1.0" encoding="utf-8"?> -<SurfaceView android:id="@+id/surface_camera" - xmlns:android="http://schemas.android.com/apk/res/android" +<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:layout_centerInParent="true" - android:layout_weight="1" - > -</SurfaceView>
\ No newline at end of file + 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 diff --git a/src/com/example/rotorm/CameraActivity.java b/src/com/example/rotorm/CameraActivity.java new file mode 100644 index 0000000..6b35995 --- /dev/null +++ b/src/com/example/rotorm/CameraActivity.java @@ -0,0 +1,134 @@ +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.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 + +public class CameraActivity extends Activity { + public static final String TAG = "rotorM"; + Preview preview; + Button buttonClick; + Camera camera; + String fileName; + Activity act; + Context ctx; + + @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); + } + }); + + 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(); + } + + 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"); + } + }; +} diff --git a/src/com/example/rotorm/Preview.java b/src/com/example/rotorm/Preview.java new file mode 100644 index 0000000..5fdea5a --- /dev/null +++ b/src/com/example/rotorm/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(); + } + } + +} |
