From 4aa03d4b2bd7cfe3acd399eb30db955bcfe3120b Mon Sep 17 00:00:00 2001 From: Andrey Kamaev Date: Thu, 23 Aug 2012 19:17:51 +0400 Subject: [PATCH] Fix/workaround bogus error in Android samples running on Android 4.1.1 --- .../opencv/samples/tutorial0/SampleViewBase.java | 16 +++++++++------- .../opencv/samples/tutorial1/SampleViewBase.java | 13 ++++++++----- .../opencv/samples/tutorial3/SampleViewBase.java | 13 ++++++++----- .../opencv/samples/tutorial4/SampleViewBase.java | 13 ++++++++----- 4 files changed, 33 insertions(+), 22 deletions(-) diff --git a/samples/android/tutorial-0-androidcamera/src/org/opencv/samples/tutorial0/SampleViewBase.java b/samples/android/tutorial-0-androidcamera/src/org/opencv/samples/tutorial0/SampleViewBase.java index 147393a839..376c86adb1 100644 --- a/samples/android/tutorial-0-androidcamera/src/org/opencv/samples/tutorial0/SampleViewBase.java +++ b/samples/android/tutorial-0-androidcamera/src/org/opencv/samples/tutorial0/SampleViewBase.java @@ -25,6 +25,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde private byte[] mFrame; private boolean mThreadRun; private byte[] mBuffer; + private SurfaceTexture mSf; public SampleViewBase(Context context) { @@ -43,16 +44,17 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde } public void setPreview() throws IOException { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) - mCamera.setPreviewTexture( new SurfaceTexture(10) ); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + mSf = new SurfaceTexture(10); + mCamera.setPreviewTexture( mSf ); + } else - mCamera.setPreviewDisplay(null); + mCamera.setPreviewDisplay(null); } public boolean openCamera() { Log.i(TAG, "openCamera"); - releaseCamera(); mCamera = Camera.open(); if(mCamera == null) { Log.e(TAG, "Can't open camera!"); @@ -125,15 +127,15 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde mFrame = new byte [size]; mCamera.addCallbackBuffer(mBuffer); + /* Notify that the preview is about to be started and deliver preview size */ + onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height); + try { setPreview(); } catch (IOException e) { Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e); } - /* Notify that the preview is about to be started and deliver preview size */ - onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height); - /* Now we can start a preview */ mCamera.startPreview(); } diff --git a/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/SampleViewBase.java b/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/SampleViewBase.java index d479215a7b..6151eb0957 100644 --- a/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/SampleViewBase.java +++ b/samples/android/tutorial-1-addopencv/src/org/opencv/samples/tutorial1/SampleViewBase.java @@ -25,6 +25,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde private byte[] mFrame; private boolean mThreadRun; private byte[] mBuffer; + private SurfaceTexture mSf; public SampleViewBase(Context context) { @@ -43,8 +44,10 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde } public void setPreview() throws IOException { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) - mCamera.setPreviewTexture( new SurfaceTexture(10) ); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + mSf = new SurfaceTexture(10); + mCamera.setPreviewTexture( mSf ); + } else mCamera.setPreviewDisplay(null); } @@ -124,15 +127,15 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde mFrame = new byte [size]; mCamera.addCallbackBuffer(mBuffer); + /* Notify that the preview is about to be started and deliver preview size */ + onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height); + try { setPreview(); } catch (IOException e) { Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e); } - /* Notify that the preview is about to be started and deliver preview size */ - onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height); - /* Now we can start a preview */ mCamera.startPreview(); } diff --git a/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/SampleViewBase.java b/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/SampleViewBase.java index ac1762a8a0..f4666ed390 100644 --- a/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/SampleViewBase.java +++ b/samples/android/tutorial-3-native/src/org/opencv/samples/tutorial3/SampleViewBase.java @@ -24,6 +24,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde private byte[] mFrame; private boolean mThreadRun; private byte[] mBuffer; + private SurfaceTexture mSf; public SampleViewBase(Context context) { @@ -42,8 +43,10 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde } public void setPreview() throws IOException { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) - mCamera.setPreviewTexture( new SurfaceTexture(10) ); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + mSf = new SurfaceTexture(10); + mCamera.setPreviewTexture( mSf ); + } else mCamera.setPreviewDisplay(null); } @@ -123,15 +126,15 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde mFrame = new byte [size]; mCamera.addCallbackBuffer(mBuffer); + /* Notify that the preview is about to be started and deliver preview size */ + onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height); + try { setPreview(); } catch (IOException e) { Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e); } - /* Notify that the preview is about to be started and deliver preview size */ - onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height); - /* Now we can start a preview */ mCamera.startPreview(); } diff --git a/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/SampleViewBase.java b/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/SampleViewBase.java index 4dafeddd69..4991e3033d 100644 --- a/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/SampleViewBase.java +++ b/samples/android/tutorial-4-mixed/src/org/opencv/samples/tutorial4/SampleViewBase.java @@ -24,6 +24,7 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde private byte[] mFrame; private boolean mThreadRun; private byte[] mBuffer; + private SurfaceTexture mSf; public SampleViewBase(Context context) { @@ -42,8 +43,10 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde } public void setPreview() throws IOException { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) - mCamera.setPreviewTexture( new SurfaceTexture(10) ); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + mSf = new SurfaceTexture(10); + mCamera.setPreviewTexture( mSf ); + } else mCamera.setPreviewDisplay(null); } @@ -123,15 +126,15 @@ public abstract class SampleViewBase extends SurfaceView implements SurfaceHolde mFrame = new byte [size]; mCamera.addCallbackBuffer(mBuffer); + /* Notify that the preview is about to be started and deliver preview size */ + onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height); + try { setPreview(); } catch (IOException e) { Log.e(TAG, "mCamera.setPreviewDisplay/setPreviewTexture fails: " + e); } - /* Notify that the preview is about to be started and deliver preview size */ - onPreviewStarted(params.getPreviewSize().width, params.getPreviewSize().height); - /* Now we can start a preview */ mCamera.startPreview(); }