diff options
author | Wei-Ta Chen <weita@google.com> | 2011-08-02 17:47:53 -0700 |
---|---|---|
committer | Wei-Ta Chen <weita@google.com> | 2011-08-03 20:24:50 -0700 |
commit | 98f41918daf64318a7da4ec94712eeb8d6697f31 (patch) | |
tree | 1ca2f7bb28512fcf1d83f32fc8dc7f8685a82b13 /src | |
parent | bed7386688cd7810cba23732ab71f370fe3222ff (diff) | |
download | LegacyCamera-98f41918daf64318a7da4ec94712eeb8d6697f31.zip LegacyCamera-98f41918daf64318a7da4ec94712eeb8d6697f31.tar.gz LegacyCamera-98f41918daf64318a7da4ec94712eeb8d6697f31.tar.bz2 |
Make Panorama run on phone.
Change-Id: I41ff1c35918794e81d4f397ed72f438dadccde4a
Diffstat (limited to 'src')
-rw-r--r-- | src/com/android/camera/panorama/PanoramaActivity.java | 97 | ||||
-rw-r--r-- | src/com/android/camera/panorama/Preview.java | 59 |
2 files changed, 85 insertions, 71 deletions
diff --git a/src/com/android/camera/panorama/PanoramaActivity.java b/src/com/android/camera/panorama/PanoramaActivity.java index 770ba51..03aaf24 100644 --- a/src/com/android/camera/panorama/PanoramaActivity.java +++ b/src/com/android/camera/panorama/PanoramaActivity.java @@ -31,13 +31,13 @@ import android.hardware.Sensor; import android.hardware.SensorEvent; import android.hardware.SensorEventListener; import android.hardware.SensorManager; -import android.media.MediaScannerConnection; -import android.media.MediaScannerConnection.MediaScannerConnectionClient; import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.util.Log; +import android.view.SurfaceHolder; +import android.view.SurfaceView; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; @@ -52,12 +52,11 @@ import com.android.camera.ShutterButton; import com.android.camera.Storage; import com.android.camera.Util; -import java.io.File; import java.io.ByteArrayOutputStream; -import java.util.ArrayList; import java.util.List; -public class PanoramaActivity extends Activity implements ModePicker.OnModeChangeListener { +public class PanoramaActivity extends Activity implements + ModePicker.OnModeChangeListener, SurfaceHolder.Callback { public static final int DEFAULT_SWEEP_ANGLE = 60; public static final int DEFAULT_BLEND_MODE = Mosaic.BLENDTYPE_HORIZONTAL; public static final int DEFAULT_CAPTURE_PIXELS = 960 * 720; @@ -65,9 +64,14 @@ public class PanoramaActivity extends Activity implements ModePicker.OnModeChang private static final int MSG_FINAL_MOSAIC_READY = 1; private static final String TAG = "PanoramaActivity"; - private static final float NS2S = 1.0f / 1000000000.0f; // TODO: commit for - // this constant. - private Preview mPreview; + + // Ratio of nanosecond to second + private static final float NS2S = 1.0f / 1000000000.0f; + + private static final int PREVIEW_STOPPED = 0; + private static final int PREVIEW_ACTIVE = 1; + + private SurfaceView mPreview; private ImageView mReview; private CaptureView mCaptureView; @@ -75,6 +79,7 @@ public class PanoramaActivity extends Activity implements ModePicker.OnModeChang private int mPreviewWidth; private int mPreviewHeight; private Camera mCameraDevice; + private int mCameraState; private SensorManager mSensorManager; private Sensor mSensor; private ModePicker mModePicker; @@ -82,6 +87,7 @@ public class PanoramaActivity extends Activity implements ModePicker.OnModeChang private String mCurrentImagePath = null; private long mTimeTaken; private Handler mMainHandler; + private SurfaceHolder mSurfaceHolder; @Override public void onCreate(Bundle icicle) { @@ -119,9 +125,9 @@ public class PanoramaActivity extends Activity implements ModePicker.OnModeChang private void releaseCamera() { if (mCameraDevice != null) { - mCameraDevice.stopPreview(); CameraHolder.instance().release(); mCameraDevice = null; + mCameraState = PREVIEW_STOPPED; } } @@ -297,7 +303,9 @@ public class PanoramaActivity extends Activity implements ModePicker.OnModeChang private void createContentView() { setContentView(R.layout.panorama); - mPreview = (Preview) findViewById(R.id.pano_preview); + mPreview = (SurfaceView) findViewById(R.id.pano_preview); + mPreview.getHolder().addCallback(this); + mCaptureView = (CaptureView) findViewById(R.id.pano_capture_view); mCaptureView.setStartAngle(-DEFAULT_SWEEP_ANGLE / 2); mCaptureView.setVisibility(View.INVISIBLE); @@ -348,8 +356,7 @@ public class PanoramaActivity extends Activity implements ModePicker.OnModeChang mSensorManager.registerListener(mListener, mSensor, SensorManager.SENSOR_DELAY_UI); setupCamera(); - mPreview.setCameraDevice(mCameraDevice); - mCameraDevice.startPreview(); + startPreview(); if (mMosaicFrameProcessor == null) { // Start the activity for the first time. @@ -433,4 +440,70 @@ public class PanoramaActivity extends Activity implements ModePicker.OnModeChang System.gc(); } + @Override + public void surfaceCreated(SurfaceHolder holder) { + } + + @Override + public void surfaceDestroyed(SurfaceHolder holder) { + } + + @Override + public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { + mSurfaceHolder = holder; + + if (mCameraDevice == null) return; + + // Set preview display if the surface is being created. Preview was + // already started. Also restart the preview if display rotation has + // changed. Sometimes this happens when the device is held in portrait + // and camera app is opened. Rotation animation takes some time and + // display rotation in onCreate may not be what we want. + if (holder.isCreating()) { + // Set preview display if the surface is being created and preview + // was already started. That means preview display was set to null + // and we need to set it now. + setPreviewDisplay(holder); + } else { + // 1. Restart the preview if the size of surface was changed. The + // framework may not support changing preview display on the fly. + // 2. Start the preview now if surface was destroyed and preview + // stopped. + startPreview(); + } + } + + private void setPreviewDisplay(SurfaceHolder holder) { + try { + mCameraDevice.setPreviewDisplay(holder); + } catch (Throwable ex) { + releaseCamera(); + throw new RuntimeException("setPreviewDisplay failed", ex); + } + } + + private void startPreview() { + // If we're previewing already, stop the preview first (this will blank + // the screen). + if (mCameraState != PREVIEW_STOPPED) stopPreview(); + + setPreviewDisplay(mSurfaceHolder); + + try { + Log.v(TAG, "startPreview"); + mCameraDevice.startPreview(); + } catch (Throwable ex) { + releaseCamera(); + throw new RuntimeException("startPreview failed", ex); + } + mCameraState = PREVIEW_ACTIVE; + } + + private void stopPreview() { + if (mCameraDevice != null && mCameraState != PREVIEW_STOPPED) { + Log.v(TAG, "stopPreview"); + mCameraDevice.stopPreview(); + } + mCameraState = PREVIEW_STOPPED; + } } diff --git a/src/com/android/camera/panorama/Preview.java b/src/com/android/camera/panorama/Preview.java deleted file mode 100644 index 4063e67..0000000 --- a/src/com/android/camera/panorama/Preview.java +++ /dev/null @@ -1,59 +0,0 @@ -/* - * Copyright (C) 2011 The Android Open Source Project - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package com.android.camera.panorama; - -import android.content.Context; -import android.hardware.Camera; -import android.util.AttributeSet; -import android.view.SurfaceHolder; -import android.view.SurfaceView; - -class Preview extends SurfaceView implements SurfaceHolder.Callback { - private static final String TAG = "Preview"; - private android.hardware.Camera mCameraDevice; - - public Preview(Context context, AttributeSet attrs) { - super(context, attrs); - - // Install a SurfaceHolder.Callback so we get notified when the - // underlying surface is created and destroyed. - getHolder().addCallback(this); - } - - @Override - public void surfaceCreated(SurfaceHolder holder) { - } - - @Override - public void surfaceDestroyed(SurfaceHolder holder) { - } - - public void setCameraDevice(android.hardware.Camera camera) { - mCameraDevice = camera; - } - - @Override - public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) { - if (mCameraDevice != null) { - try { - mCameraDevice.setPreviewDisplay(holder); - } catch (Throwable ex) { - throw new RuntimeException("setPreviewDisplay failed", ex); - } - } - } -} |